Thursday, March 4, 2010

Function to create GD thumbnail using Php

function thumbnail($src,$destimagename,$dimension)
{
$src_path=explode("/",$src);
$name=$src_path[count($src_path)-1];
array_pop($src_path);
$oldpath=implode("/",$src_path);
$newpath=$oldpath;
$newpath=$newpath."/".$destimagename;

if(copy($src,$newpath))
{
$name_extension=substr($name,strrpos($name,".")+1);
if (preg_match("/gif/i",$name_extension))
{
$src_img=imagecreatefromgif($newpath);
}
if (preg_match("/jpg|jpeg/i",$name_extension))
{
$src_img=imagecreatefromjpeg($newpath);
}
if (preg_match("/png/i",$name_extension))
{
$src_img=imagecreatefrompng($newpath);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$thumb_w = $dimension;
$thumb_h = floor(($old_y/$old_x) * $dimension);

$dst_img=@ImageCreateTrueColor($thumb_w,$thumb_h);
@imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

if(preg_match("/png/i",$name_extension))
{
@imagepng($dst_img,$newpath);
}
if(preg_match("/jpg|jpeg/i",$name_extension))
{
@imagejpeg($dst_img,$newpath);
}
if(preg_match("/gif/i",$name_extension))
{

@imagegif($dst_img,$newpath);
}
@imagedestroy($dst_img);
@imagedestroy($src_img);

}

}

No comments: