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
$image="mm_travel_photo1.jpg";
$src_img=imagecreatefromjpeg($image);
// get old widht and height
$old_w=imagesx($src_img);
$old_h=imagesy($src_img);
// 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
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
$image="mm_travel_photo1.jpg";
$src_img=imagecreatefromjpeg($image);
// get old widht and height
$old_w=imagesx($src_img);
$old_h=imagesy($src_img);
// 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
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
ReplyDelete