我有一个PHP脚本,可以将.jpg,.gif和.png文件调整到边界框。
$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);该脚本在jpeg和gif下工作正常,但在png上运行时文件将被损坏。
使用png时需要使用什么特别的东西?我从来没有在PHP中使用过这种东西,所以我对它不是很熟悉。