1

我尝试使用 php gdlib 在现有图像中将灰色(rgb:235,235,240)变为透明。

这是我使用的代码:

<?php
header("Content-type:image/png");
$picture = imagecreatefrompng("test.png");
$grey = imagecolorallocate($picture, 235, 235, 240);
imagecolortransparent($picture, $grey);
imagepng($picture);
imagedestroy($picture, "newpicture.png");
?>

当 test.png 上有很多不同的颜色时,此代码将不起作用。否则,当 test.png 上只有少量颜色时,此代码可以完美运行。为什么?

4

1 回答 1

2

它不起作用,因为您没有将修改后的图片保存到磁盘。
您当前的代码:

imagepng($picture);

会将修改后的图片发送到浏览器,但您也在输出 HTML 代码:

<img src="mytest.png" />

以这种方式修改您的代码:

imagepng($picture, 'mytest.png'); // save the picture to disk

然后,您的 HTML 代码将显示修改后的图片。

检查文档:imagepng

您必须使用此行将灰色存储到$grey

$grey = imagecolorallocate($picture, 235, 235, 240);

imagecolorresolve做了完全不同的事情。

于 2012-09-02T16:12:15.410 回答