1

上传这张照片并调整大小后,我注意到它的质量变得有点模糊......为什么会发生这种情况?我有足够的空间,所以还有什么问题?

原来的,

在此处输入图像描述

调整大小后,

在此处输入图像描述

但是我上传的其他图片完全没有问题。为什么只发生在这张照片上?

会不会是下面的调整大小功能造成的?

function resize_image($image,$width,$height,$scale) 
{
    # Calculating proportionality
    list($imagewidth, $imageheight, $imageType) = getimagesize($image);
    $imageType = image_type_to_mime_type($imageType);
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

    # Loading image to memory according to type
    switch($imageType) 
    {
        case "image/gif":
            $source = imagecreatefromgif($image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            $source = imagecreatefromjpeg($image); 
            break;
        case "image/png":
        case "image/x-png":
            $source = imagecreatefrompng($image); 
            break;
    }

    # This is the resizing/resampling/transparency-preserving magic
    if ( ($imageType == "image/gif") || ($imageType == "image/png") ) 
    {
        $transIndex = imagecolortransparent($source);

        if($transIndex >= 0) 
        {
            $transcol = imagecolorsforindex($source, $transIndex);
            $transIndex = imagecolorallocatealpha($newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
            imagefill($newImage, 0, 0, $transIndex);
            imagecolortransparent($newImage, $transIndex);
        }

        elseif ($imageType == "image/png" || $imageType == "image/x-png") 
        {
            imagealphablending($newImage, false);
            $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
            imagefill($newImage, 0, 0, $color);
            imagesavealpha($newImage, true);
        }
    }

    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

    # Writing image according to type to the output destination
    switch($imageType) 
    {
        case "image/gif":
            imagegif($newImage,$image); 
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            imagejpeg($newImage,$image,90); 
            break;
        case "image/png":
        case "image/x-png":
            imagepng($newImage,$image);  
            break;
    }

    chmod($image, 0777);
    return $image;
}
4

1 回答 1

1

Jpeg 是一种针对压缩照片进行了优化的格式。在您展示的图像中,伪影比正常情况要明显得多。我建议您更改此行的质量级别:

imagejpeg($newImage,$image,100);

另一种选择是将图像保存在 png 中。

于 2011-11-05T01:26:08.470 回答