繁体   English   中英

php imagecopyresampled质量差

[英]php imagecopyresampled poor quality

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

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

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;

非常感谢

PS [从服务器中删除照片]

这是上传代码的其余部分:

// 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。

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

imagejpeg  ( $new_image, null, 99);

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

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

快速肮脏的技巧是在imagecopyresized()使缩略图1000 x 1000像素 (或更多),然后在imagejpeg($img, $savePath, 20);上将JPEG质量设置为20或更低imagejpeg($img, $savePath, 20); 输出通常小于100 kb

让客户端CSS进行调整大小,当缩放到缩略图大小时,图片将快速加载并在现代浏览器中看起来完美无瑕。

好吧,php.net文档说如果你想避免只使用255个调色板但你已经这样做了,你应该为你的dest_image设置一个imagecreatetruecolor()图像。

我想另一种方法是使用诸如imagemagick之类的外部工具和system()调用。

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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM