简体   繁体   English

保留PNG透明度,同时调整黑色背景的大小

[英]Preserve PNG transparency while resizing with out the black background

I have a small class which handles image manipulation. 我有一小节课处理图像处理。

I use following to resize a image 我使用以下命令调整图像大小

$this->image = imagecreatefrompng($filename);
....
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
...
$this->image = $new_image; 
imagepng($this->image,$filename)) { return true; }

But the resized image is not preserving transparency, instead black is comming, how can i preserve the transparency. 但是调整大小后的图像不能保留透明度,而是黑色的来临,我如何保留透明度。

Update 更新

After, using @Manuel's code, black portion has decreased, but still black background are still present. 之后,使用@Manuel的代码,黑色部分减少了,但仍然存在黑色背景。 The source image and the resulting image are 源图像和结果图像是

Source & Sub corresponding 源与子对应

main http://www.freeimagehosting.net/newuploads/820a0.png sub http://www.freeimagehosting.net/newuploads/30526.png 主要http://www.freeimagehosting.net/newuploads/820a0.png 子http://www.freeimagehosting.net/newuploads/30526.png

The newest comment, posted on the 8th of May, on the manual page for imagecopyresampled , tells you how to do this. 最新的评论(于5月8日发布在imagecopyresampled的手册页上)告诉您如何执行此操作。

imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);

Put that right after creating $new_image . 在创建$new_image之后将其正确$new_image

add this before the imagecopyresampled(...) imagecopyresampled(...)之前添加它

// preserve transparency
imagecolortransparent($new_image , imagecolorallocatealpha($new_image , 0, 0, 0, 127));
imagealphablending($new_image , false);
imagesavealpha($new_image , true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM