调整大小时保留 PNG 透明度,不使用黑色背景
我有一个处理图像操作的小班。
我使用以下方法调整图像大小
$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; }
,但是调整大小的图像不保留透明度,而是出现黑色,我如何保留透明度。
更新
后,使用@Manuel的代码,黑色部分减少了,但黑色背景仍然存在。源图像和结果图像是
Source &子对应
main http://www.freeimagehosting.net/newuploads/820a0.png 子http://www.freeimagehosting.net/newuploads/30526.png
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. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最新评论发布于 5 月 8 日,在
imagecopyresampled
的手册页上,告诉您如何执行此操作。将其放在创建
$new_image
之后。The newest comment, posted on the 8th of May, on the manual page for
imagecopyresampled
, tells you how to do this.Put that right after creating
$new_image
.在
imagecopyresampled(...)
之前添加此内容add this before the
imagecopyresampled(...)