PHP Thumbnail question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bos45430
Forum Newbie
Posts: 12
Joined: Sun Jul 08, 2007 11:11 am

PHP Thumbnail question.

Post 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]
tapas_bahirbag
Forum Newbie
Posts: 14
Joined: Sun Aug 06, 2006 6:54 am
Contact:

Post 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]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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().
bos45430
Forum Newbie
Posts: 12
Joined: Sun Jul 08, 2007 11:11 am

Post 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
bos45430
Forum Newbie
Posts: 12
Joined: Sun Jul 08, 2007 11:11 am

Post 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");
Post Reply