I have a PHP script that resizes .jpg, .gif, and .png files to a bounding box.
$max_width = 500;
$max_height = 600;
$filetype = $_FILES["file"]["type"];
$source_pic = "img/" . $idnum;
if($filetype == "image/jpeg")
{
$src = imagecreatefromjpeg($source_pic);
} else if($filetype == "image/png")
{
$src = imagecreatefrompng($source_pic);
} else if($filetype == "image/gif")
{
$src = imagecreatefromgif($source_pic);
}
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) )
{
$tn_width = $width;
$tn_height = $height;
} else if (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
$destination_pic = "img/thumbs/" . $idnum . "thumb";
if($filetype == "image/jpeg")
{
imagejpeg($tmp,$destination_pic,80);
} else if($filetype == "image/png")
{
imagepng($tmp,$destination_pic,80);
} else if($filetype == "image/gif")
{
imagegif($tmp,$destination_pic,80);
}
imagedestroy($src);
imagedestroy($tmp);
The script works fine with jpeg and gif but when running on a png the file will be corrupted.
Is there anything special I need to use when working with a png? I have never worked with this sort of thing in PHP so I'm not very familiar with it.