Thursday, March 6, 2008

Create Image Thumbnail on the fly

Today im not in the mood to explain more about this function but if you have any question, dont hesitate, just ask me in the comment section. Thanks.


function image_create_thumb($filename, $width, $height)
{
list($image["Width"], $image["Height"], $image["Type"]) = getimagesize($filename);


switch($image["Type"])
{
case IMG_JPG:
case IMG_JPEG:
$im_image = ImageCreateFromJPEG($filename);
break;
case IMG_GIF:
$im_image = ImageCreateFromGIF($filename);
break;
case IMG_PNG:
$im_image = ImageCreateFromPNG($filename);
break;
}

if($image["Width"] > $image["Height"])
{
$scale = $width / $image["Width"];
$thumb_width = $width;
$thumb_height = floor($image["Height"]*$scale);
}
else
{
$scale = $height / $image["Height"];
$thumb_height = $height;
$thumb_width = floor($image["Width"]*$scale);
}

$im_thumb = @ImageCreateTrueColor($thumb_width, $thumb_height) or die("Cannot Initialize new GD image stream");

ImageCopyResized($im_thumb, $im_image, 0, 0, 0, 0, $thumb_width, $thumb_height, $image["Width"], $image["Height"]);

ImageDestroy($im_image);
ImagePNG($im_thumb, $filename);
ImageDestroy($im_thumb);

return true;
}//image_create_thumb($filename, $width, $height)
?>

0 comments: