I've been trying to get transparency to work with my application (which dynamically resizes images before storing them) and I think I've finally narrowed down the problem after much misdirection about imagealphablending
and imagesavealpha
. The source image is never loaded with proper transparency!
// 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);
It would be some serious architectural difficulty to load the image from a file; this code is being used with a JSON API that gets queried from an iPhone app, and it's easier in this case (and more consistent) to upload images as base64-encoded strings in the POST data. Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)? Is there maybe a way to create a Stream from $fileData
that can be passed to imagecreatefrompng
?
PHP 5.3.10 / GD 2.0
, Working fine. What versions are you using ? Might come from something else.. Corrupted PNG entry ? Like iPhone app uploading a bad quality image ? Just shots in the dark – CrandallPHP 5.3.16-1~dotdeb.0
(with Suhosin) withGD 2.0.34
(2.0.34 compatible) on my Debian box, and when I ranimagepng
the transparent parts were black. This happened when I usedimagecreatefromstring
orimagecreatefrompng
. – Bacchanaliaimagealphablending
orimagesavealpha
. – Bacchanalia