Page 1 of 1

PHP Thumbnail question.

Posted: Tue Jul 10, 2007 11:55 pm
by bos45430
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have put together a little script thats supposed to take a picture and make it 100 pixels wide and stay in proportion on height.  It makes the image the correct size but its only showing a piece of the picture.  Please help me with this.  Thanks.

Code: Select all

function createthumbnail($imagedirectory, $imagename, $thumbdirectory, $thumbwidth, $thumbname)
      {
        $srcimg = imagecreatefromjpeg("$imagedirectory/$imagename");
        $origwidth = imagesx($srcimg);
        $origheight = imagesy($srcimg);
        
        $ratio = $origwidth / $thumbwidth;
        $thumbheight = $origheight / $ratio;
        
        $thumbimg = imagecreate($thumbwidth, $thumbheight);
        imagecopyresized($thumbimg, $srcimg, 0, 0, 0, 0, $thumbwidth, $thumbheight, imagesx($thumbimg), imagesy($thumbimg));
        
        imagejpeg($thumbimg, "$thumbdirectory/$thumbname");
      }
      createthumbnail("$imagedirectory", "$imagename", "$thumbdirectory", 100, "$thumbname");

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 11, 2007 2:52 am
by tapas_bahirbag
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You can try this (something will have to modify)

Code: Select all

imagejpeg($i,"$imagedirectory/$imagename",80);

//Specify the size of the thumbnail
$dest_x = 150;
$dest_y = 150;

//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    //Is the width of the original bigger than the height?
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    //Using the original dimensions
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

//Generate a new image at the size of the thumbnail
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);

//Copy the original image data to it using resampling
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));

//Save the thumbnail
imagejpeg($thumb, “images/thumbnail.jpg”, 80);

--
Tapos Pal


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 11, 2007 3:26 am
by onion2k

Code: Select all

imagecopyresized($thumbimg, $srcimg, 0, 0, 0, 0, $thumbwidth, $thumbheight, imagesx($thumbimg), imagesy($thumbimg));
The parameter list for imagecopyresized() is

destination image
source image
destination x
destination y
source x
source y
destination width
destination height
source width
source height

In your code you're using destination width and destination height for the last 2 values, consequently you're only copying a small area of the source image. It's a really useful feature because you can clip images at the same time as copying them, but generally you'll want to use the dimensions of the source image there.

You ought to use imagecopyresampled() by the way. It's slower, but makes much nicer images because it uses bicubic resampling instead of nearest neighbour like imagecopyresized().

Posted: Wed Jul 11, 2007 12:43 pm
by bos45430
im not familiar with GD at all. What variables need to be changed to what in order to make it where it copies the whole image?

thanks

john

Posted: Wed Jul 11, 2007 1:04 pm
by bos45430
wait i think i got what your saying. i changed the last 2 parameters to the size of the original image :) here is my code. im very proud.

Code: Select all

$imagedirectory = "$currentdir/users/$_COOKIE[username]/albums/$_POST[albumname]";
			$thumbdirectory = "$currentdir/users/$_COOKIE[username]/albums/$_POST[albumname]/thumbs";
			$imagename = "".$savename.".jpg";
			$thumbname = "".$savename."t.jpg";
			
      function createthumbnail($imagedirectory, $imagename, $thumbdirectory, $thumbwidth, $thumbname)
      {
        $srcimg = imagecreatefromjpeg("$imagedirectory/$imagename");
        $origwidth = imagesx($srcimg);
        $origheight = imagesy($srcimg);
        
        $ratio = $origwidth / $thumbwidth;
        $thumbheight = $origheight / $ratio;
        
        $thumbimg = imagecreate($thumbwidth, $thumbheight);
        imagecopyresized($thumbimg, $srcimg, 0, 0, 0, 0, $thumbwidth, $thumbheight, $origwidth, $origheight);
        imagejpeg($thumbimg, "$thumbdirectory/$thumbname");
      }
      createthumbnail("$imagedirectory", "$imagename", "$thumbdirectory", 100, "$thumbname");