【问题标题】:Help fixing the image library帮助修复图像库
【发布时间】:2010-10-18 11:52:14
【问题描述】:

我的图片库有问题。我想我知道问题所在,但我对图像知之甚少,希望有人能告诉我到底出了什么问题。

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

我认为问题出在 resize 函数中的 imagecreatetruecolor 函数上。文档建议这会返回黑色图像。我不认为这是我所追求的。

有人能多管闲事,告诉我问题是否确实出在调整大小功能上,以及应该如何解决。

谢谢。

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

【问题讨论】:

    标签: php png transparency


    【解决方案1】:

    尝试使用Primage class。检查example

    【讨论】:

      【解决方案2】:

      尝试使用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;
          }
      

      【讨论】:

        猜你喜欢
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多