Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this transparent PNG cause borders when combined using GD?

I am trying to create an image from an 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 am loading this image in $source_under

enter image description here

and copying it over a transparent blank "canvas" image. Here is the result of that operation:

enter image description here

As can be seen, there is a sort of black border around the whole initial image. I think this is due to the fact that initially, the "canvas" image is all black. So there is something wrong with the transparency and the anti-aliasing of the image.

This isn't the first time I have a similar problem, but last time the source image was the cause. This time around, opening it in Photoshop does not show any potential problems with it.

Does anyone know how to fix this?

like image 711
Alex Turpin Avatar asked Jul 23 '12 21:07

Alex Turpin


People also ask

Why are my transparent PNGs not transparent?

There are a few reasons why your PNG might not be transparent in Photoshop. One reason is that you are using the wrong file format. You should be using a PNG-24 file format to ensure transparency. Another reason is that your image might have an alpha channel, but it is not set as the transparency channel.

Why does PNG Show checkered background?

The checkered pattern indicates transparency in the image's background. You only see it in Photoshop. If you don't want to see it while working in Photoshop, go to Photoshop's Preferences, then “Transparency & Gamut”. You can eliminate the pattern or change size and color of the blocks.

Does PNG support full transparency?

A Portable Network Graphic (PNG) file is a raster image that uses lossless compression. That means that PNGs retain all their original file data when compressed, so they can contain a great deal of image detail. They also support opacity and transparency.


1 Answers

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

imagealphablending($image, true); 

Second try would be to create a transparent color and to fill $image with that color before the copy.

$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
imagealphablending($image, true); 
like image 150
huysentruitw Avatar answered Sep 24 '22 01:09

huysentruitw