PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to create image from transparent image mangles color?

 October 10, 2022     colors, gd, image, php, transparent     No comments   

Issue

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.


Solution

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.



Answered By - Paul Lammertsma
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing