Friday, October 29, 2010

Transparent gif with smooth edge using Imagick

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); //or use $im->borderImage($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; ?>

 

No comments:

Post a Comment