Search code examples
phpmysqlimageresizecrop

Trying to resize and crop image for thumbnail


What I'm trying to do is resize and crop the center of an uploaded image. So far I have it to where it resizes the image and that's all. I know the imagecopy function is what I want, I'm just having trouble getting it to work with my function.

This is what I have so far.

/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);

/* find the "desired height" of this thumbnail, relative to the desired width  */
$desired_height = floor($height*($desired_width/$width));

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);

/* copy source image at a resized size */

imagecopyresampled($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);

/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);

I just need to know where and how to incorporate the imagecopy function.

Thank you.


Solution

  • Take a look at this class. It incorporates GD2, like the example you provided, that also has other resize algorithms for thumbnail generation.