【问题标题】:Fix png transparency problem修复png透明问题
【发布时间】:2010-10-29 15:34:41
【问题描述】:

我使用这个类在我的服务器上上传图像。问题是,当我上传透明 png 图像时,透明部分变黑。有没有办法保持 png 图像的透明度?

class SimpleImage {

   var $image;
   var $image_type;

   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);
      }

   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   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;   
   }      
}

【问题讨论】:

标签: php png transparency


【解决方案1】:

我不相信这个类可以处理透明的 PNG。 imagecopyresampled 上的 PHP 文档解释了这一点。

您可能需要扩展该类以使用 imagecolorallocatealpha。

阅读参考:http://php.net/manual/en/function.imagecreatetruecolor.php

【讨论】:

    【解决方案2】:

    我用这个来保持 png 或 gif 图像的透明度。

    imagecolortransparent($this->imageResized, imagecolorallocatealpha($this->imageResized, 0, 0, 0, 127));
    imagealphablending($this->imageResized, false);
    imagesavealpha($this->imageResized, true);
    

    【讨论】:

      【解决方案3】:

      您可能需要使用 imagealphablending($this->image, false); imagesavealpha($this->image, true); 在图像png之前

      【讨论】:

        【解决方案4】:

        可以在这里找到解决方案: PNG Transparency Resize with SimpleImage.php Class

        它修改了一些类,以便在 PNG 和 GIF 图像的情况下允许透明。

        【讨论】:

          猜你喜欢
          • 2012-07-27
          • 2012-08-10
          • 2013-08-14
          • 1970-01-01
          • 2010-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-21
          相关资源
          最近更新 更多