Page 1 of 1

creating thumbnails

Posted: Sun May 14, 2006 1:22 pm
by psychotomus
how can I create thumbnails for .jpg and .png files?

Posted: Sun May 14, 2006 1:47 pm
by $phpNut

Code: Select all

$destimg = imagecreatetruecolor ($newWidth, $newHeight) 
    or die ("Problem In Creating image"); // Create thumbnail canvas

// Use one or use conditional to select i.e. if JPEG
$srcimg = imagecreatefromjpeg ("path to full sized") 
    or die ("Problem In opening Source Image");  // For JPEG

// else PNG
$srcimg = imagecreatefrompng ("path to full sized") 
    or die ("Problem In opening Source Image");  // For PNG

imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg

// Use one or use conditional to select i.e. if JPEG
imagejpeg ($destimg, "path to thumb") or die("Problem In saving");  // Save JPG

// else PNG
imagepng ($destimg, "path to thumb") or die("Problem In saving");  // Save PNG

Posted: Sun May 14, 2006 3:00 pm
by psychotomus
I got this and its not saving the file to the server.

Code: Select all

<?php
$destimg = imagecreatetruecolor (250, 250) 
    or die ("Problem In Creating image"); // Create thumbnail canvas 

// Use one or use conditional to select i.e. if JPEG 
$srcimg = imagecreatefromjpeg ("uploads/boo.jpg") 
    or die ("Problem In opening Source Image");  // For JPEG 



imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, 250, 250, 494, 733) 
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg 

// Use one or use conditional to select i.e. if JPEG 
imagejpeg ($destimg, "uploads/tester.jpg") or die("Problem In saving");  // Save JPG 

?>
[/url]

Posted: Sun May 14, 2006 3:49 pm
by $phpNut
is your upload folder chmod 0777?

put it this way, it works for me on mine http://www.saxonian.co.uk/imagehost/ , also try full paths so /home/you/public_html/ .... /uploads/ thats what i use.



Edit: found your problem you should use relative paths properly :D don't forget the ./ before the path, either that or use full paths.

Code: Select all

<?php
$destimg = imagecreatetruecolor (250, 250)
    or die ("Problem In Creating image"); // Create thumbnail canvas

// Use one or use conditional to select i.e. if JPEG
$srcimg = imagecreatefromjpeg ("./uploads/boo.jpg")
    or die ("Problem In opening Source Image");  // For JPEG



imagecopyresampled ($destimg, $srcimg, 0, 0, 0, 0, 250, 250, 494, 733)
     or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg

// Use one or use conditional to select i.e. if JPEG
imagejpeg ($destimg, "./uploads/tester.jpg") or die("Problem In saving");  // Save JPG

?>

Posted: Sun May 14, 2006 4:38 pm
by psychotomus
how would i get the width and height from $srcimg??

Posted: Sun May 14, 2006 4:39 pm
by John Cartwright

Posted: Sun May 14, 2006 5:07 pm
by $phpNut

Code: Select all

list ($width, $height) = getimagesize("./uploads/boo.jpg");
// $width file width
// $height file height
and your not thumbnailing them to a square are you? ie distorting images and generally making them look horrible :P

Posted: Sun May 14, 2006 8:26 pm
by psychotomus
yes... what should i thumbnail them to?

I would like to catch and resize photos from file input stat

Posted: Wed May 17, 2006 10:38 am
by mparker1113
from file input status, before i move them to a different directory using moveuploadedfile() or other means.

I think that the photos directly from the camera are to big to be moved, and would like to resize them immediately, so that I can move them.

would the code for this be similar to the posted, using imagecreatefromjpeg, and makejpeg() ?

Would I be able to get this to work, (without storing the file first)?

Thanks for input.

Mike

Posted: Wed May 17, 2006 10:03 pm
by aerodromoi
psychotomus wrote:yes... what should i thumbnail them to?
As always: The beauty lies in the eye of the beholder ;) 100x100 perhaps?
If you really want square shaped thumbnails, you'll have to resize them to a certain size, which is somewhat bigger than the final size. You can then crop them by using ImageCopy();

aerodromoi

Posted: Sun May 21, 2006 11:42 am
by psychotomus
I get this error when trying to resize an image after uploading it.

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mounted-storage/home16a/sub002/sc18478-RGIJ/www/admin_wallpapers.php on line 196
Problem In resizing

Code: Select all

$loc = $row['id'] + 1; 
          
         if (move_uploaded_file($_FILES["file"]["tmp_name"], "./wallpapers/" .  $loc . '.jpg')) 
         { 


            //SAVE THUMBNAIL 
            $srcimg = imagecreatefromjpeg ("./wallpapers/" .  $loc . '.jpg') 
               or die ("Problem In opening Source Image");  // For JPEG 

            imagecopyresampled ($destimg,  $srcimg, 0, 0, 0, 0, 150, 150, imagesx($srcimg) , imagesy($srcimg)) 
                or die ("Problem In resizing");  // Resample the image from $srcimg to the $destimg 
             
            imagejpeg ($destimg, "./wallpapers_thumbs/" . $loc . ".jpg") or die("Problem In saving");  // Save JPG

Posted: Sun May 21, 2006 12:24 pm
by jayshields
$phpNut wrote:

Code: Select all

list ($width, $height) = getimagesize("./uploads/boo.jpg");
// $width file width
// $height file height
and your not thumbnailing them to a square are you? ie distorting images and generally making them look horrible :P
Nice code. First time I've seen "list" used well.

Posted: Sun May 21, 2006 4:21 pm
by psychotomus
jayshields, im trying to get help. don't hijack my thread about complimenting other coders. I know there good, thats why I ask them for help =)

Re: creating thumbnails

Posted: Mon May 22, 2006 6:02 am
by xpgeek
psychotomus wrote:how can I create thumbnails for .jpg and .png files?
use http://phpthumb.sourceforge.net/.

Posted: Mon May 22, 2006 10:18 am
by psychotomus
my code is fine. its almost exactly how coppermine is done but without a class.

since im using HTTP POST to get the filename, how can i replace this and set $srcimg to the $_FILES['file'] which is allready in memory instead of loading the file and using up twice as much memory. maybe I can get it to work that way.


$srcimg = imagecreatefromjpeg ("./wallpapers/" . $loc . '.jpg') or die ("Problem In opening Source Image"); // For JPEG