Why does this transparent PNG draw borders when merged using GD?

I am trying to create an image from another image using PHP. Here is my code:

<?php $width = 109; $height = 109; $image = imagecreatetruecolor($width, $height); $source_under = imagecreatefrompng('ecloid_under.png'); $black = imagecolorallocate($image, 0x00, 0x00, 0x00); imagecolortransparent($image, $black); imagecopy($image, $source_under, 0, 0, 0, 0, $width, $height); header('Content-type: image/png'); imagepng($image); imagedestroy($image); ?> 

So, I upload this image to $source_under

enter image description here

and copy it to the transparent blank canvas image. Here is the result of this operation:

enter image description here

As you can see, around the entire source image there is a kind of black frame. I think this is due to the fact that initially the image "canvas" is all black. Thus, something is wrong with the transparency and smoothing of the image.

This is not the first time I have a similar problem, but the last time the reason was the original image. This time, opening it in Photoshop, it does not show any potential problems.

Does anyone know how to fix this?

+4
source share
2 answers

Can you try to enable alpha blending on $ image before copying the original to it:

 imagealphablending($image, true); 

The second attempt is to create a transparent color and fill $ image with this color before the copy.

 $transparent = imagecolorallocatealpha($image, 0, 0, 0, 127); imagefill($image, 0, 0, $transparent); imagealphablending($image, true); 
+6
source

You have partial transparency around the edges of the original image. This makes it blend with the black canvas image (which you usually don’t see because it is 100% transparent), giving the results you see. You can avoid this by making sure that your entire alpha channel in the source image is 100% or 0%, or by choosing a more suitable base color for your canvas (for example, one that matches the color scheme of your site).

Comment by Fabio Anselmo will help that GIFs do not have a real alpha channel - GIF transparency is all or nothing - so save money, since everyone will make a 100% -0% solution. If you are not very careful, it will also give you a β€œborder” directly in the original image β€” consisting of any background color that you have or select in GIF conversion β€” as a result of your smoothing of the image. (However, the interlaced part does not matter.)

+3
source

All Articles