How to create thumbnails in PHP

Posted in: PHP
By dePoPo
Mar 9, 2009 - 3:32:30 PM

The code snippet below is based on original code from http://alt-php-faq.org/local/105/ with a fix for the scaling bug for calculation of the correct (new) height of the thumbnail.

function thumbnail($image_path,$thumb_path,$image_name,$thumb_width)
{
$src_img = imagecreatefromjpeg("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$origh/$diff;
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
 
imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}


Visitor Comments