【问题标题】:Preserve PNG transparency while resizing with out the black background在没有黑色背景的情况下调整大小时保留 PNG 透明度
【发布时间】:2016-07-17 01:21:03
【问题描述】:

我有一个处理图像处理的小类。

我使用以下来调整图像的大小

$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 & Sub 对应

main http://www.freeimagehosting.net/newuploads/820a0.pngsub http://www.freeimagehosting.net/newuploads/30526.png

【问题讨论】:

标签: php png transparency image-manipulation gd


【解决方案1】:

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

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

在创建 $new_image 之后立即添加。

【讨论】:

  • $new 包含什么内容。这适用于所有类型,还是只适用于 pngs
【解决方案2】:

imagecopyresampled(...)之前添加这个

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

【讨论】:

  • 但我仍然得到黑色 bgs,但数量比以前少了
  • 好的,您可以查看更新,这是使用您的代码后的结果。
猜你喜欢
  • 2013-01-08
  • 2014-06-03
  • 2012-08-30
  • 1970-01-01
  • 2015-04-20
  • 2013-01-02
  • 2011-08-07
  • 2012-10-15
相关资源
最近更新 更多