Saturday, February 18, 2012

Create thumbnail images using PHP

How to Create thumbnail images using PHP

By the following code you can understand how to create a thumbnail of a image

To create a thumbnail, we read in the file using the imagecreatefromjpeg() function and calculate the new thumbnail size. imagesx() and imagesy() functions return the width and height of the image respectively. Then we create a new image using the imagecreatetruecolor(). and Finally, we copy and resize the original file with the imagecopyresized() function and save thumbnail with imagejpeg()

$thumbWidth=70; // fix your widht
// get old widht and height
// calculate new widht and height;
$new_width = $thumbWidth;
$new_height = floor( $old_h * ( $thumbWidth / $old_w ) );
$tmp_image=imagecreatetruecolor($new_width,$new_height); // craete tmp images
imagecopyresampled( $tmp_image,$src_img, 0, 0, 0, 0, $new_width, $new_height, $old_w, $old_h);
imagejpeg( $tmp_image, "abc.jpeg" ); // get  the image and name it as abc.jpeg
imagedestroy( $tmp_image );

This process  can support for png and gif also

1 comment:

  1. Nice tutorial. Remeber that if your image size is big, you can incrase time limit in PHP to handle this. I wrote a note about max execution time it on my blog


I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off