[PHP] Losing alpha transparency

echo8era

New Member
Messages
3
Reaction score
0
Points
0
I can't seem to keep semi-transparent areas of my images semi-transparent. Unless I'm misunderstanding the documentation for imagealphablending() and imagesavealpha(), this should be working:
PHP:
  $BG = imagecreatefrompng('BackgroundImages/MrsMitch1.png');
  $Width = imagesx($BG);
  $Height = imagesy($BG);
 
  $Image = imagecreatetruecolor($Width, $Height);
  imagealphablending($Image, false);
  imagesavealpha($Image, true);
 
  imagecopymerge($Image, $BG, 0, 0, 0, 0, $Width, $Height, 100);
  // Code to draw on $Image here
 
  // Cache it to reduce need to redraw image each request
  imagepng($BG, 'Cache/MrsMitch.png', 0);
  imagedestroy($Image);
  imagedestroy($BG);
 
Last edited:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
It looks like you need to explicitly call imagesavealpha($BG) as well, doesn't it? You only have single-bit transparency on the copy of the image you merged into the final image. Remember that the image object you created from the original PNG isn't a PNG anymore, it's an image object. (Yes, it may be reasonable to expect that it would inherit the "include entire alpha channel" info from the PNG. Your experiment proves that it won't export with the alpha channel unless you force it to.)
 

echo8era

New Member
Messages
3
Reaction score
0
Points
0
Whoops, one of the lines in the original example should have been
PHP:
  //Cache it to reduce need to redraw image each request
  imagepng($Image, 'Cache/MrsMitch.png', 0);
Teaches me to proofread after simplifying example code.

It looks like you need to explicitly call imagesavealpha($BG) as well, doesn't it? You only have single-bit transparency on the copy of the image you merged into the final image. Remember that the image object you created from the original PNG isn't a PNG anymore, it's an image object. (Yes, it may be reasonable to expect that it would inherit the "include entire alpha channel" info from the PNG. Your experiment proves that it won't export with the alpha channel unless you force it to.)
What do you know, that works. Thanks.

Aarrgghh!!! I kind of wish ASP.Net worked - at least I know what I'm doing there instead of the guessing game my attempt at coding in PHP currently is.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
I feel your pain -- PHP is kind of new to me as well*, although I have Gandalf skillz on platforms I can't afford to use for personal projects. Cruising forums like this helps. For some reason, other people's problems are less frustrating than my own (probably because there's no cost to me, either monetary or in terms of personal pride, if I can't figure it out). I wouldn't have known about the imagesavealpha() thing if I hadn't read your question and tried to figure it out, but now I have a tool I didn't have in the box a couple of days ago.

*I did use it back when it was still an abbreviation for "Personal Home Page Tools", Rasmus was still in the Toronto area, and it wasn't much good for anything but forms processing. Things have changed just a little bit since then.
 
Top