为什么解决 GD 的 png 透明度问题的解决方案不起作用?
Why solution to fix png transparency issue of GD does not work?
我在渲染 png 上有这段代码(简化):
$this->image = imagecreatefrompng($this->file);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);
我的背景变黑后,我找了一些解决办法,但没有用。更简单的一个:
$this->image = imagecreatefrompng($this->file);
imagealphablending($targetImage, false);
imagesavealpha($targetImage, true);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);exit();
别人都说好用,但我还是黑色背景,所以我试了另一个:
$this->image = imagecreatefrompng($this->file);
$targetImage = imagecreatetruecolor($this->imageInfo[0], $this->imageInfo[1]);
imagealphablending($targetImage, false);
$color = imagecolorallocatealpha($targetImage, 0, 0, 0, 127);
imagefill($targetImage, 0, 0, $color);
imagecolortransparent($targetImage, $color);
imagesavealpha($targetImage, true);
imagecopyresampled($targetImage, $this->image, 0, 0, 0, 0, $this->imageInfo[0], $this->imageInfo[1], $this->imageInfo[0], $this->imageInfo[1]);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);exit();
结果在所有现代浏览器中都是一样的。怎么可能,知道吗?
代码是 class 的一部分,它适用于所有类型的图像,并且所有功能都能正常工作。
您似乎想按原样发送 png
文件,那么为什么要先使用 GD 转换它呢?我只会使用 readfile()
并输出文件:
header("Content-Type: {$this->imageInfo['mime']}");
readfile($this->file);
exit();
对于您的其他测试:
你想在最后输出 $targetImage
而不是 $this->image
,否则不会发生什么奇怪的事情。此外,我认为您需要在 imagecopyresampled
之前启用 alpha 混合而不是禁用它,以避免出现黑色边框。
$this->image = imagecreatefrompng($this->file);
$targetImage = imagecreatetruecolor($this->imageInfo[0], $this->imageInfo[1]);
$color = imagecolorallocatealpha($targetImage, 0, 0, 0, 127);
imagefill($targetImage, 0, 0, $color);
imagecolortransparent($targetImage, $color);
imagealphablending($targetImage, true);
imagecopyresampled($targetImage, $this->image, 0, 0, 0, 0, $this->imageInfo[0], $this->imageInfo[1], $this->imageInfo[0], $this->imageInfo[1]);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($targetImage);
exit();
我在渲染 png 上有这段代码(简化):
$this->image = imagecreatefrompng($this->file);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);
我的背景变黑后,我找了一些解决办法,但没有用。更简单的一个:
$this->image = imagecreatefrompng($this->file);
imagealphablending($targetImage, false);
imagesavealpha($targetImage, true);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);exit();
别人都说好用,但我还是黑色背景,所以我试了另一个:
$this->image = imagecreatefrompng($this->file);
$targetImage = imagecreatetruecolor($this->imageInfo[0], $this->imageInfo[1]);
imagealphablending($targetImage, false);
$color = imagecolorallocatealpha($targetImage, 0, 0, 0, 127);
imagefill($targetImage, 0, 0, $color);
imagecolortransparent($targetImage, $color);
imagesavealpha($targetImage, true);
imagecopyresampled($targetImage, $this->image, 0, 0, 0, 0, $this->imageInfo[0], $this->imageInfo[1], $this->imageInfo[0], $this->imageInfo[1]);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($this->image);exit();
结果在所有现代浏览器中都是一样的。怎么可能,知道吗? 代码是 class 的一部分,它适用于所有类型的图像,并且所有功能都能正常工作。
您似乎想按原样发送 png
文件,那么为什么要先使用 GD 转换它呢?我只会使用 readfile()
并输出文件:
header("Content-Type: {$this->imageInfo['mime']}");
readfile($this->file);
exit();
对于您的其他测试:
你想在最后输出 $targetImage
而不是 $this->image
,否则不会发生什么奇怪的事情。此外,我认为您需要在 imagecopyresampled
之前启用 alpha 混合而不是禁用它,以避免出现黑色边框。
$this->image = imagecreatefrompng($this->file);
$targetImage = imagecreatetruecolor($this->imageInfo[0], $this->imageInfo[1]);
$color = imagecolorallocatealpha($targetImage, 0, 0, 0, 127);
imagefill($targetImage, 0, 0, $color);
imagecolortransparent($targetImage, $color);
imagealphablending($targetImage, true);
imagecopyresampled($targetImage, $this->image, 0, 0, 0, 0, $this->imageInfo[0], $this->imageInfo[1], $this->imageInfo[0], $this->imageInfo[1]);
header("Content-Type: {$this->imageInfo['mime']}");
imagepng($targetImage);
exit();