我尝试使用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上只有少量颜色时,此代码可以完美地工作。为什么?
发布于 2012-09-03 00:12:15
它不起作用,因为您没有将修改后的图片保存到磁盘。
您当前的代码:
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做的是完全不同的事情。
https://stackoverflow.com/questions/12236105
复制相似问题