【发布时间】:2014-10-24 17:29:10
【问题描述】:
我有一个 php 脚本来生成图片上传的缩略图,使用 PHP GD 库。
缩略图的高度是固定的(在本例中为240px),其宽度将根据原始图像的纵横比计算。 前任。
$new_height = $thumbHeight;
$new_width = intval($thumbHeight * $width / $height);
但在某些图像中,输出的缩略图图像像素失真。下面的图片清楚地解决了我的问题。
生成缩略图后输出图像(左),但我想要输出图像在右
我的代码:
$file = "pic.jpg";
$thumbHeight = 240;
$progressive = false;
$img;
if(preg_match('/[.](jpg)$/', $file)) {
$img = imagecreatefromjpeg($file);
} else if (preg_match('/[.](gif)$/', $file)) {
$img = imagecreatefromgif($file);
} else if (preg_match('/[.](png)$/', $file)) {
$img = imagecreatefrompng($file);
} else if(preg_match('/[.](jpeg)$/', $file)) {
$img = imagecreatefromjpeg($file);
}
$arr_image_details = getimagesize($file);
$width = $arr_image_details[0]; // width of input image
$height = $arr_image_details[1]; // height of input image
$new_height = $thumbHeight; // new thumbnail height
$new_width = intval($thumbHeight * $width / $height); // new thumbnail width
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
if($progressive) imageinterlace($tmp_img, 1);
imagejpeg( $tmp_img, "lag-$file",100 );
imagedestroy($img);
imagedestroy($tmp_img);
【问题讨论】:
-
你试过用
imagecopyresampled代替imagecopyresized吗? -
不,我没有尝试过..但是
imagecopyresized不同..? -
相比
imagecopyresized,通常效果更好,图像更流畅
标签: php image-processing gd thumbnails