2011-01-13 100 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

我有同样的问题。在将EPS文件生成为透明PNG文件(稍后用作掩码图像)时,iMagick将PNG文件创建为灰度。当GD读取灰度PNG时,它会将它们水平加倍,并在其中包含垂直线。

我的解决办法的面面面处理,以迫使它写的PNG为RGBA:

前:

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

后:

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

我不知道你的灰度PNG来自哪里,但如果你正在生成它们,确保它们是创建RGBA。否则,也许有一种方法可以正确读取GD,指定灰度来源。

0

我有同样的问题,仍然没有发现任何解决方案。看起来GD不喜欢灰度PNG8图像,并且正在解释为彩色。

我可能必须使用exec()“convert”将图像转换回24位,然后删除图像,但它远非最佳。

+0

您是否正在运行检查以查看图像是灰度还是PNG8?看起来这可能是一个很好的方法来解决这个问题,所以如果不需要的话,并不是每个PNG8都会被转换。 – stwhite 2011-01-27 08:28:17