调整大小时保留 PNG 透明度,不使用黑色背景

发布于 2024-12-10 06:45:59 字数 823 浏览 1 评论 0原文

我有一个处理图像操作的小班。

我使用以下方法调整图像大小

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-12-17 06:45:59

最新评论发布于 5 月 8 日,在 imagecopyresampled 的手册页上,告诉您如何执行此操作。

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

将其放在创建 $new_image 之后。

The newest comment, posted on the 8th of May, on the manual page for imagecopyresampled, tells you how to do this.

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.

风筝有风,海豚有海 2024-12-17 06:45:59

imagecopyresampled(...) 之前添加此内容

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

add this before the imagecopyresampled(...)

// preserve transparency
imagecolortransparent($new_image , imagecolorallocatealpha($new_image , 0, 0, 0, 127));
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文