1

I am building one kind of system, which simply creates dynamic image from other image. I use: imagecreatefromstring(file_get_contents("clown_avatar.png")); to create image and output it successfully; but, it messes all the colors on the transparent area.

Check out the original image:

Click

And, here is the result from PHP file:

Click

Here is the source, just few rows:

<?php

$im = imagecreatefromstring(file_get_contents("clown_avatar.png"));
//$bg = imagecolorallocate($im,0,0,0); doesn't effect
Header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>

I tried to add background color with imagecolorallocate; but, it did not effect at all.

16ctt1x
  • 321
  • 6
  • 21
Martti Laine
  • 12,655
  • 22
  • 68
  • 102

1 Answers1

0

The reason for this is that you are discarding the alpha layer. By doing so, the otherwise invisible parts of the image now become visible.

What you instead want to do, is load the original PNG image with alpha, and copy it onto a desired image (that is solely the background color) using imagecopymerged.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Well, how to do this still using imagecreatefromstring() because I have to create images from different extensions and this is the easiest way. – Martti Laine Dec 28 '09 at 14:58
  • Instead, resolve the MIME type and handle each one separately. If it's too much programming work, handle PNG and GIF individually. – Paul Lammertsma Dec 28 '09 at 15:18