2

我对 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;

任何想法 ?它可能与服务器配置有关吗?

4

2 回答 2

1

替换第一行

$image = imagecreatetruecolor($width, $height);

$image = imagecreate($width, $height);

透明度现在应该可以工作了,但是调色板不会正确显示一些真实的颜色。

任何白色像素现在都是透明的。

要从白色更改透明度颜色,请使用:

$r = *red colour value (0 to 255)*;
$g = *green colour value (0 to 255)*;
$b = *blue colour value (0 to 255)*;
$color = imagecolorallocatealpha($image, $r, $g, $b, 127);
于 2014-08-11T09:40:26.667 回答
-1

如果这就是您用于图像文件的所有$width代码,那么您还没有为和$height变量定义任何值,并且脚本会引发错误。

作为一般规则,当您要调试图像时,请删除 Content-Type 标头。这样,您可以通过在浏览器中访问脚本来查看任何潜在的错误。

于 2012-10-18T10:33:37.433 回答