帮助修复图像库

时间:2010-10-18 11:52:14

标签: php png transparency

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

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

我认为问题出在调整大小功能中的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);
        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用Primage class。检查example

答案 1 :(得分: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;
    }
相关问题