Martti Laine Martti Laine - 1 year ago 67
PHP Question

How to create image from transparent image mangles color?

I am building one kind of system, which simply creates dynamic image from other image. I use:

to create image and output it successfully; but, it messes all the colors on the transparent area.

Check out the original image:


And, here is the result from PHP file:


Here is the source, just few rows:


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


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download