php imagecopyresampled质量差 [英] php imagecopyresampled poor quality

查看:167
本文介绍了php imagecopyresampled质量差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本保存原始图像,然后调整大小 - 一个缩略图和一个较大的图像供Web查看。除了一些图像质量很差外,这种方法效果很好。它似乎是用非常低的颜色托盘保存的。您可以在 http://kalpaitch.com/index.php?filter=white <上查看结果/ a> - 点击标题为白色白色白色的第一个缩略图

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'

以下是用于图像重新采样的代码:

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;

非常感谢

P.S。
[从服务器中删除照片]

P.S. [photos removed from server]

以下是其余的上传代码:

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);
        }


推荐答案

质量下降不是 imagecopyresampled(),而是JPEG压缩。不幸的是,GD的压缩算法与Photoshop不匹配 - 事实上,很少有。但是你可以改善结果:GD默认的JPG压缩级别是75的100。

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.

你可以使用第三个参数来提高质量 imagejpeg() (我假设你用于最终输出):

You can raise the quality using the third parameter to imagejpeg() (which I assume you are using for the final output):

imagejpeg  ( $new_image, null, 99);

在90-100范围内玩游戏。文件大小将比原始图像大 - 这将是您支付的价格。但是应该可以达到相当的质量。

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.

或者,正如John Himmelman在评论中已经说过的那样,尝试使用 imagepng()为了更好的质量 - 当然还要以明显更大的文件大小为代价。

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.

这篇关于php imagecopyresampled质量差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆