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?

meustrus
  • 6,637
  • 5
  • 42
  • 53
  • 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 dark – Touki Aug 29 '12 at 15:52
  • 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`. – gen_Eric Aug 29 '12 at 15:55
  • I'm in PHP 5.3.10 / GD (2.0.34 compatible). – meustrus Aug 29 '12 at 15:56
  • Edit: I didn't test this with `imagealphablending` or `imagesavealpha`. – gen_Eric Aug 29 '12 at 15:59
  • Found this, not sure if it helps: http://theolagendijk.com/2009/10/24/php-imagepng-transparant-turns-black/ – gen_Eric Aug 29 '12 at 16:00

3 Answers3

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 !

Sirko
  • 72,589
  • 19
  • 149
  • 183
papyrut
  • 71
  • 1
  • 2
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.

meustrus
  • 6,637
  • 5
  • 42
  • 53
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=');
Prasanth
  • 5,230
  • 2
  • 29
  • 61