【发布时间】:2012-08-24 06:40:30
【问题描述】:
我一直在尝试让我的应用程序具有透明度(它会在存储图像之前动态调整图像大小),我认为在对imagealphablending 和imagesavealpha 进行了很多误导之后,我终于缩小了问题的范围。源图像永远不会以适当的透明度加载!
// With this line, the output image has no transparency (where it should be
// transparent, colors bleed out randomly or it's completely black, depending
// on the image)
$img = imagecreatefromstring($fileData);
// With this line, it works as expected.
$img = imagecreatefrompng($fileName);
// Blah blah blah, lots of image resize code into $img2 goes here; I finally
// tried just outputting $img instead.
header('Content-Type: image/png');
imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);
imagepng($img);
imagedestroy($img);
从文件加载图像会是一些严重的架构困难;此代码与从 iPhone 应用程序查询的 JSON API 一起使用,在这种情况下更容易(并且更一致)将图像作为 POST 数据中的 base64 编码字符串上传。我是否绝对需要以某种方式将图像存储为文件(以便 PHP 可以再次将其加载到内存中)?有没有办法从$fileData 创建一个可以传递给imagecreatefrompng 的流?
【问题讨论】:
-
无法在
PHP 5.3.10 / GD 2.0上重现该问题,工作正常。你用的是什么版本?可能来自其他东西..损坏的PNG条目?像 iPhone 应用程序上传质量差的图像?只是在黑暗中拍摄 -
我在我的 Debian 机器上使用
GD 2.0.34(兼容 2.0.34)在PHP 5.3.16-1~dotdeb.0(使用 Suhosin)上测试了这个,当我运行imagepng时,透明部分是黑色的。这发生在我使用imagecreatefromstring或imagecreatefrompng时。 -
我使用的是 PHP 5.3.10 / GD(2.0.34 兼容)。
-
编辑:我没有用
imagealphablending或imagesavealpha测试这个。 -
找到了这个,不确定是否有帮助:theolagendijk.com/2009/10/24/…