2010-10-18 80 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; 
    }