PHP GD imagecreatefromstring discards transparency
Asked Answered
W

3

13

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?

Washable answered 29/8, 2012 at 15:37 Comment(5)
Can't reproduce the issue on 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 darkCrandall
I tested this on PHP 5.3.16-1~dotdeb.0 (with Suhosin) with GD 2.0.34 (2.0.34 compatible) on my Debian box, and when I ran imagepng the transparent parts were black. This happened when I used imagecreatefromstring or imagecreatefrompng.Bacchanalia
I'm in PHP 5.3.10 / GD (2.0.34 compatible).Washable
Edit: I didn't test this with imagealphablending or imagesavealpha.Bacchanalia
Found this, not sure if it helps: theolagendijk.com/2009/10/24/…Bacchanalia
W
6

Blech, this turned out to ultimately be due to a totally separate GD call which was validating the image uploads. I forgot to add imagealphablending and imagesavealpha to THAT code, and it was creating a new image that then got passed to the resizing code. Which should probably be changed anyway. Thanks very much to goldenparrot for the excellent method of converting a string into a filename.

Washable answered 29/8, 2012 at 16:41 Comment(0)
F
7

you can use this code :

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

// preserve transparency

imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));

imagealphablending($new, false);

imagesavealpha($new, true);

imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

imagepng($new);

imagedestroy($new);

It will make a transparent image for you. Good Luck !

Folliculin answered 9/12, 2013 at 4:5 Comment(0)
W
6

Blech, this turned out to ultimately be due to a totally separate GD call which was validating the image uploads. I forgot to add imagealphablending and imagesavealpha to THAT code, and it was creating a new image that then got passed to the resizing code. Which should probably be changed anyway. Thanks very much to goldenparrot for the excellent method of converting a string into a filename.

Washable answered 29/8, 2012 at 16:41 Comment(0)
U
2

Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)?

No.

Documentation says:

You can use data:// protocol from php v5.2.0

Example:

// prints "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
Unbonnet answered 29/8, 2012 at 15:43 Comment(1)
You should be able to do $img = imagecreatefrompng('data://image/png;base64,'.$base64Image);Bacchanalia

© 2022 - 2024 — McMap. All rights reserved.