【发布时间】:2012-10-08 13:59:17
【问题描述】:
我对 PHP 中的 imagecolorallocatealpha 有疑问。将不透明度设置为 127 时,我得到的是白色图像而不是透明图像。
这是我的代码
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $color);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit;
我也试过这个,但我得到了相同的结果
$image = imagecreatetruecolor($width, $height);
$x = imagecolorat($image, 0,0);
imagecolortransparent($image, $x);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit;
有什么想法吗?会不会和服务器配置有关?
【问题讨论】:
-
您的代码(第一个示例)适用于我。我知道这个问题很荒谬,但你确定你没有将图像覆盖在白色背景上吗?因为当直接在浏览器中查看或在白色背景上查看时,图像自然会呈现为白色,因为您可以看到它背后的白色。我只是问这个,因为您的示例对我来说非常有效。以下是在 Google Chrome 开发工具的网络选项卡中查看图像时的外观:i.imgur.com/rohsM.png
-
不幸的是,它是白色的,不是背景(在 Firefox 中,考虑到图像背景是某种深灰色,这很明显)。然而,在谷歌浏览器的开发工具的网络标签中查看时,它看起来很奇怪,就好像它没有被解释为 png 一样。 d.pr/i/1Fte(我用一个只包含上面代码的php文件来测试它)。
标签: php png transparency gd