2011-01-13 10 views
2

一部のpng画像のサイズを変更すると、画像が伸びて表示され、垂直方向のぼかしのように見えます。問題がどこにあるのかは分かりませんが、イメージはグレースケールで始まり、別のカラープロファイルを持つ必要があるため、そのイメージを考えるようになり始めています。グレースケールPNGのサイズを変更するとき白い線が表示される

何か助力や提案をいただければ幸いです。

function createImageSize($sourcefile, $setNewName, $maxwidth, $maxheight, $quality){ 

    $fileInfoArray = @getimagesize($sourcefile); 
    $imagetype = $fileInfoArray['mime']; 

    list($width, $height, $attr) = getimagesize($sourcefile); 

    switch($imagetype){ 
     case 'image/jpeg': 
      $img = imagecreatefromjpeg($sourcefile); 
      break; 

     case 'image/gif': 
      $img = imagecreatefromgif($sourcefile); 
      break; 

     case 'image/png': 
      $img = imagecreatefrompng($sourcefile); 
      break; 

     case 'image/x-png': 
      $img = imagecreatefrompng($sourcefile); 
      break; 
    } 

    if ($width > $maxwidth || $height > $maxheight){ 
     if ($width > $height){ 
      $newwidth = $maxwidth; 
      $ratio = $maxwidth/$width; 
      $newheight = floor($height * $ratio); 

      if ($newheight > $maxheight){ 
       $newheight = $maxheight; 
       $ratio = $maxheight/$height; 
       $newwidth = floor($width * $ratio); 
      } 
     }else{ 
      $newheight = $maxheight; 
      $ratio = $maxheight/$height; 
      $newwidth = floor($width * $ratio); 

      if ($newwidth > $maxwidth){ 
       $newwidth = $maxwidth; 
       $ratio = $maxwidth/$width; 
       $newheight = floor($height * $ratio); 
      } 
     } 
    }else{ 
     $newwidth = $width; 
     $newheight = $height; 
    } 

    $tmpimg = imagecreatetruecolor($newwidth, $newheight); 

    if($imagetype == 'image/png'||$imagetype == 'image/x-png'){ 
     imagealphablending($tmpimg, false); 
     imagesavealpha($tmpimg, true); 

     if($imagetype == 'image/gif'){ 
      $transparent = imagecolorallocatealpha($tmpimg, 0, 0, 0, 127); 
      imagecolortransparent($tmpimg, $transparent); 
     } 

     imagefilledrectangle($tmpimg, 0, 0, $newwidth, $newheight, $transparent); 
    } 

    imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    switch($imagetype){ 
     case 'image/jpeg': 
      imagejpeg($tmpimg, $setNewName, $quality); 
      break; 

     case 'image/gif': 
      imagegif($tmpimg, $setNewName); 
      break; 

     case 'image/png': 
      imagepng($tmpimg, $setNewName, 3); 
      break; 

     case 'image/x-png': 
      imagepng($tmpimg, $setNewName, 3); 
      break; 
    } 
    imagedestroy($tmpimg); 
    imagedestroy($img); 
} 

答えて

2

私は同じ問題を抱えていました。透明なPNGファイルにEPSファイルを生成すると(後でマスク画像として使用するため)、iMagickはPNGファイルをグレースケールで作成しました。 GDがグレースケールのPNGを読み取るとき、GDはそれらを縦線で水平に倍増します。

私のソリューションは、RGBAとしてPNG画像を書くためにそれを強制することで、iMagic側で処理された

前:

$image->setImageFileName("image.png"); 

後:

$image->setImageFileName("png32:image.png"); 

私にはわかりませんあなたのグレースケールのPNGはどこから来ているのですか?それを生成する場合は、RGBAが作成されていることを確認してください。それ以外の場合は、グレースケールソースを指定して、GDでそれらを正しく読み取る方法があります。

0

私は同じ問題を持っているし、まだ任意の解決策を発見していません。 GDはグレースケールのPNG8イメージを好まず、色付きであると解釈されます。

イメージを「変換」を使用してexec()を使用してイメージに戻してからイメージを削除する必要がありますが、最適ではありません。

+0

イメージがグレースケールでPNG8であるかどうかをチェックしていますか?このようにするのは良い方法かもしれないので、すべてのPNG8を変換する必要はありません。 – stwhite

関連する問題