php imagecopyresampled 质量差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2345605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php imagecopyresampled poor quality
提问by kalpaitch
I have a php script which saves the original image, then resizes it - one thumbnail and one larger image for web viewing. This works well except with some images the quality is terrible. It seems to be saved with a very low colour pallet. You can see the result at http://kalpaitch.com/index.php?filter=white- click on the first thumbnail with the title 'white white white'
我有一个 php 脚本,它保存原始图像,然后调整它的大小 - 一个缩略图和一个更大的图像用于网络查看。这很好用,除了一些图像质量很差。它似乎用非常低的调色板保存。您可以在http://kalpaitch.com/index.php?filter=white上查看结果- 单击标题为“white white white”的第一个缩略图
Below is the code used for the image resampling:
下面是用于图像重采样的代码:
function resizeImg($name, $extension, $size1, $size2) {
if (preg_match('/jpg|jpeg|JPG|JPEG/',$extension)){
$image = imagecreatefromjpeg($name);
}
if (preg_match('/gif|GIF/',$extension)){
$image = imagecreatefromgif($name);
}
$old_width = imageSX($image);
$old_height = imageSY($image);
$old_aspect_ratio = $old_width/$old_height;
if($size2 == 0){
$new_aspect_ratio = $old_aspect_ratio;
if($old_width > $old_height){
$new_width = $size1;
$new_height = $new_width / $old_aspect_ratio;
} else {
$new_height = $size1;
$new_width = $new_height * $old_aspect_ratio;
}
} elseif($size2 > 0){
$new_aspect_ratio = $size1/$size2;
//for landscape potographs
if($old_aspect_ratio >= $new_aspect_ratio) {
$x1 = round(($old_width - ($old_width * ($new_aspect_ratio/$old_aspect_ratio)))/2);
$old_width = round($old_width * ($new_aspect_ratio/$old_aspect_ratio));
$y1 = 0;
$new_width = $size1;
$new_height = $size2;
//for portrait photographs
} else{
$x1 = 0;
$y1 = 0;
$old_height = round($old_width/$new_aspect_ratio);
$new_width = $size1;
$new_height = $size2;
}
}
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, $x1, $y1, $new_width, $new_height, $old_width, $old_height);
return $new_image;
Many Thanks
非常感谢
P.S. [photos removed from server]
PS [从服务器中删除的照片]
And here is the rest of the upload code:
这是上传代码的其余部分:
// Move the original to the right place
$result = @move_uploaded_file($image['tmp_name'], $origlocation);
// Resize the image and save the thumbnail
$new_image = resizeImg($origlocation, $extension, 500, 0);
if (preg_match("/gif/",$extension)){
imagegif($new_image, $normallocation);
} else {
imagejpeg($new_image, $normallocation);
}
// Resize the image and save the thumbnail
$new_image = resizeImg($origlocation, $extension, 190, 120);
if (preg_match("/gif/",$extension)){
imagegif($new_image, $thumblocation);
} else {
imagejpeg($new_image, $thumblocation);
}
回答by Pekka
The loss in quality is down not to imagecopyresampled(), but to the JPEG compression. Unfortunately, GD's compression algorithms are no match to Photoshop's - in fact, very few are. But you can improve the result: GD's default JPG compression level is 75 of 100.
质量损失不在于imagecopyresampled(),而在于 JPEG 压缩。不幸的是,GD 的压缩算法与 Photoshop 的不匹配——事实上,很少有人能做到。但是您可以改进结果:GD 的默认 JPG 压缩级别是 75 of 100。
You can raise the quality using the third parameter to imagejpeg()(which I assume you are using for the final output):
您可以使用imagejpeg()的第三个参数提高质量(我假设您将其用于最终输出):
imagejpeg ( $new_image, null, 99);
Play around in the 90-100 range. The image will become larger in file size than the original - that is going to be the price you pay. But it should be possible to achieve comparable quality.
在 90-100 范围内玩。图像的文件大小将比原始文件大 - 这将是您支付的价格。但是应该可以达到可比的质量。
Alternatively, as John Himmelman already says in the comments, try using imagepng()for better quality - also at the price of a notably larger file size, of course.
或者,正如 John Himmelman 已经在评论中所说的那样,尝试使用imagepng()以获得更好的质量 - 当然也是以明显更大的文件大小为代价。
回答by Ecker00
The quick an dirty trick is to make the thumbnails 1000 x 1000 pixels(or more) on imagecopyresized()then set the JPEG quality to 20or less on imagejpeg($img, $savePath, 20);. The output will usually be smaller than 100 kb.
快速的肮脏技巧是将缩略图设置为 1000 x 1000 像素(或更多),imagecopyresized()然后将JPEG 质量设置为 20或更低imagejpeg($img, $savePath, 20);。输出通常小于100 kb。
Let the client CSS do the resizing and the pictures will be fast to load and look flawless in modern browsers when scaled to thumbnail size.
让客户端 CSS 来调整大小,当缩放到缩略图大小时,图片将可以快速加载并且在现代浏览器中看起来完美无缺。
回答by Mathieu
well, php.net documentation says you should have a imagecreatetruecolor() image for your dest_image if you want to avoid using only a 255 color palette but you already do that.
好吧,php.net 文档说,如果您想避免仅使用 255 色的调色板,您应该为 dest_image 设置一个 imagecreatetruecolor() 图像,但您已经这样做了。
I guess an alternative would be to use an external tools such as imagemagickwith a system() call.
我想另一种方法是使用外部工具,例如带有 system() 调用的imagemagick。
回答by Hadiyal Rakesh
function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight = 0 )
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = getimagesize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
$x = imagesx($imorig);
$y = imagesy($imorig);
$woh = (!$maxisheight)? $gis[0] : $gis[1] ;
if($woh <= $size)
{
$aw = $x;
$ah = $y;
}
else
{
if(!$maxisheight)
{
$aw = $size;
$ah = $size * $y / $x;
}
else
{
$aw = $size * $x / $y;
$ah = $size;
}
}
$im = imagecreatetruecolor($aw,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y))
if (imagejpeg($im, $save_dir.$save_name))
return true;
else
return false;
}

