2010-10-18 69 views
2

我有一个图像库的问题。我想我知道这个问题,但我对图像知之甚少,并希望有人能告诉我究竟出了什么问题。帮助修复图像库

我想要做的是调整.png大小并保留透明度。当我调整大小并保存.png图像时,它会失去透明度并变黑。

我认为问题在于函数中的调整大小函数。文档建议这返回一个黑色的图像。我不认为这是我所追求的。

有人可以有一个香水,并告诉我,如果问题确实存在与调整大小功能,以及这应该如何解决。

谢谢。

class ResizeImage { 

    // Load Image 
    function load($filename) { 
     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 

     if($this->image_type == IMAGETYPE_JPEG) { 
      $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 
      $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 
      $this->image = imagecreatefrompng($filename); 
      imagealphablending($this->image, true); 
      imagesavealpha($this->image, true); 
     } 
    } 

     // Resize the image 
     function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 

     // Save the image 
    function save($filename, $image_type='', $compression=100, $permissions=null) { 
     if ($image_type != '') { 
      $this->image_type = $image_type; 
     } 

     if($this->image_type == IMAGETYPE_JPEG) { 
      imagejpeg($this->image,$filename,$compression); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 
      imagegif($this->image,$filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 
      imagepng($this->image,$filename); 
     } 
     if($permissions != null) { 
      chmod($filename,$permissions); 
     } 
    } 

回答

0

尝试使用imagesavealpha,例如:

function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagesavealpha($new_image, true); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    }