调整大小后,此图片变得模糊

时间:2011-11-05 00:57:43

标签: php gd image-uploading imagecreatefrompng

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

原,

enter image description here

调整大小后,

enter image description here

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

是否会由下面的调整大小功能引起?

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

1 个答案:

答案 0 :(得分:1)

Jpeg是一种针对压缩照片进行优化的格式。在您展示的图像中,工件比正常情况更明显。我建议你改变这一行的质量水平:

imagejpeg($newImage,$image,100);

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