Thursday, August 16, 2007

Image resize in php

createthumb($name)
{

$system=explode(".",$name);
if(preg_match("/jpg|jpeg|JPG|JPEG/",$system[1]))
{
$src_img=imagecreatefromjpeg($name);
}

else if (preg_match("/gif|GIF/",$system[1]))
{
$src_img=imagecreatefromgif($name);
}
else if (preg_match("/png|PNG/",$system[1]))
{
$src_img=imagecreatefrompng($name);
}



$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$x=$old_x;
$y=$old_y;
if($old_x>500)
{
$new_x=500;
$old_y=500*($old_y/$old_x);
$old_x=$new_x;
}
if($old_y>500)
{
$new_y=500;
$old_x=500*$old_x/$old_y;
$old_y=$new_y;
}


$dst_img=ImageCreateTrueColor($old_x,$old_y);

imagecopyresampled($dst_img,$src_img,0,0,0,0,$old_x,$old_y,$x,$y);




if (preg_match("/gif|GIF/",$system[1]))
{

imagegif($dst_img,$rootPath.$filename);

}
else if (preg_match("/png|PNG/",$system[1]))
{
$src_img=imagecreatefrompng($rootPath.$name);
}

else
{

imagejpeg($dst_img,$name);

chmod($name,0777);

}

imagedestroy($dst_img);

imagedestroy($src_img);

}

No comments:

Post a Comment