Woshiadai Dev Notebook

April 9, 2009

Transparent gif with smooth edge using Imagick

Filed under: PHP, Image Processing

If you have a PNG with transparency and then you want to convert it to a GIF with transparency preserved. Since GIF has only 1-bit transparency, the edge will look jacky. However, if you know a color that will be used as solid background color or an average color for a gradient background, you can still achieve a smooth edge for the transparent gif.

Here is how (assuming the original PNG is circle.png, the edge antialias color is RED):

< ?php
$red = new ImagickPixel('red');
$transparent = new ImagickPixel('transparent');
$im = new Imagick('circle.png');
$im->setImageBorderColor($red);
$im->frameImage($red, 0, 0, 0, 0);
$im->paintOpaqueImage($red, $transparent, 0.0); //replacing RED color with transparent color
$im->setImageFormat(’gif’);
header(’Content-Type: image/gif’);
echo $im;
?>

Comments »

The URI to TrackBack this entry is: http://woshiadai.blogsome.com/2009/04/09/transparent-gif-with-smooth-edge-using-imagick/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>























Get free blog up and running in minutes with Blogsome
Theme designed by Ben de Groot