调整大小后,PHP Png有黑色背景

时间:2016-01-23 14:33:11

标签: php png transparency transparent alphablending

当我尝试调整PNG的大小时 与

    function resize($width,$height){
    $new_image = imagecreatetruecolor($width, $height);

    if($this->image_type == IMAGETYPE_PNG || $this->type == 'image/png')
    {
        imagealphablending($new_image, false);
        imagesavealpha($new_image,true);
        $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
        imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);

    }

    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image; 
}
function output($image_type=IMAGETYPE_JPEG, $compression=100){
    if($image_type == IMAGETYPE_JPEG){
        imagejpeg($this->image, null, $compression);
    }elseif($image_type == IMAGETYPE_GIF){
        imagegif($this->image);         
    }elseif($image_type == IMAGETYPE_PNG){
        imagepng($this->image);
    }   
}

然后调整大小的图像的背景是黑色的,我尝试了一些不同的解决方案总线nohting工作了。

0 个答案:

没有答案