我有这个制作比例缩略图的代码:
function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
if (strtolower(substr($imgSrc, -3)) == "jpg") {
$myImage = imagecreatefromjpeg($imgSrc);
} else if (strtolower(substr($imgSrc, -3)) == "jpeg") {
$myImage = imagecreatefromjpeg($imgSrc);
} else if (strtolower(substr($imgSrc, -3)) == "png") {
$myImage = imagecreatefrompng($imgSrc);
} else if (strtolower(substr($imgSrc, -3)) == "gif") {
$myImage = imagecreatefromgif($imgSrc);
}
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
imagedestroy($process);
imagedestroy($myImage);
return $thumb;
}
效果良好
但是图像的质量有点低
我应该做些什么来提高创建的图片质量
保存此函数的结果时,您是否设置了图像质量?
例如,此函数允许您设置jpeg压缩,默认值非常低。
http://www.php.net/manual/en/function.imagejpeg.php
如果不是这样的话,我可能会建议完全放弃PHP GD。它并不是最好的图像处理库,只是默认情况下可用。ImageMagick很棒,但你需要安装它。使用它给了我缩略图,它的文件大小更小,但质量更好,比PHP GD的缩略图。
http://www.php.net/manual/en/book.imagick.php
如果您希望在调整大小期间使用双三次插值,请考虑使用ImageMagick而不是GD,因为它非常占用CPU。例如,这里有一篇俄语帖子(但你可以看到代码有多复杂)
您不能更改gif的质量,但如果您使用其他图像格式,请记住将质量设置为100。例如:imagepng($imageResource,NULL,100);