0

I'm going crazy and I need some help. I have a transparent image string in the variable $imageString, which I got by using file_get_contents('transparent_image.png'). However, as soon as I use the createimagefromstring() function the transparency is removed.

This works fine and keeps the transparency:

echo '<img src="data:image/png;base64,'.base64_encode($imageString).'">'; exit;

This removes the transparency and gives me a white background:

$image = imagecreatefromstring($imageString);
header("Content-Type: image/png");
imagepng($image);
exit;

Can someone please explain why this happens and how I keep the transparency after imagecreatefromstring()? I need to resize the transparent image but I'm stuck at the beginning.

I have even tried to "turn it around" by using the base64 method back into the png function but it didn't work, unfortunately;

$image = imagecreatefrompng('data:image/png;base64,'.base64_encode($imageString));

I've read all other questions regarding this topic but no one seems to have this problem with the basic function imagecreatefromstring() actually removing the transparency.

PHP: 7.4.11
GD: 2.2.5

Any ideas?

Mad Marvin
  • 3,309
  • 3
  • 18
  • 18
  • 2
    Have you seen this? https://stackoverflow.com/questions/12181573/php-gd-imagecreatefromstring-discards-transparency – KIKO Software Oct 24 '20 at 13:58
  • It turns out my battle was not with PHP but with Firefox... The transparency worked great in Chrome. Nevertheless, this was the solution (even though I didn't think it worked earlier today). Thank you for your comment! – Mad Marvin Oct 24 '20 at 14:21

0 Answers0