本文实例讲述了PHP实现按指定大小等比缩放生成上传图片缩略图的方法。分享给大家供大家参考。具体实现方法如下:
list($width,$height,$type,$attr) = getimagesize($srcImage);
if($width < $maxWidth || $height < $maxHeight) return ;
switch ($type) {
case 1: $img = imagecreatefromgif($srcImage); break;
case 2: $img = imagecreatefromjpeg($srcImage); break;
case 3: $img = imagecreatefrompng($srcImage); break;
}
$scale = min($maxWidth/$width,$maxHeight/$height); //求出绽放比例
if($scale < 1) {
$newWidth = floor($scale$width);
$newHeight = floor($scale$height);
$newImg = imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($newImg,$img,$newWidth,$newHeight,$width,$height);
$newName = "";
$toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);
switch($type) {
case 1: if(imagegif($newImg,"$toFile$newName.gif",$imgQuality))
return "$newName.gif"; break;
case 2: if(imagejpeg($newImg,"$toFile$newName.jpg",$imgQuality))
return "$newName.jpg"; break;
case 3: if(imagepng($newImg,"$toFile$newName.png",$imgQuality))
return "$newName.png"; break;
default: if(imagejpeg($newImg,$imgQuality))
return "$newName.jpg"; break;
}
imagedestroy($newImg);
}
imagedestroy($img);
return false;
}
希望本文所述对大家的PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/23112.html