2011-11-05 79 views
1

上傳並調整照片大小後,我注意到它的質量變得稍微模糊......爲什麼會發生這種情況?我有足夠的空間,那麼還有什麼問題呢?這幅圖片在調整大小後變得模糊

原來,

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

嘗試提高質量水平。 – regality

回答

1

Jpeg是針對壓縮照片進行優化的格式。在您展示的圖像中,工件比正常情況更加明顯。我建議你改變這一行的質量等級:

imagejpeg($newImage,$image,100); 

另一種選擇是將圖像保存爲PNG。

+0

謝謝Lurkit。 png現在完美工作! :-) – laukok