detecting transparent gif

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
woshiadai

detecting transparent gif

Post by woshiadai »

Using Imagick, how can I detect if a gif is transparent or not?

I tried the following using getImageChannelDepth(imagick::ALPHA_CHANNEL), but did not work for me:

if (result is zero or null) // API doc only says "TRUE on success" which I think should be the number of bits for that channel and API does not say the return value if that channel does not exist
{
this is NOT a transparent gif
}
else
{
this is a transparent gif //has 1 bit alpha channel
}

However, I tried this on a transparent and a non-transparent gif. Both return 1 bit depth for alpha_channel (both return types are int using is_int to check). Using the command line identify, only the transparent gif has alpha channel info that shows 1 bit though. Could this be a bug with Imagick?

But anyways, I just need to know how to detect the transparent gif. I am using Imagick 2.2.2RC1 and ImageMagick 6.2.9 12/17/07 Q16
woshiadai

Re: detecting transparent gif

Post by woshiadai »

Just went through the API doc again and found this function called getImageChannelStatistics, seems like I can use it to do the trick:

$stats = $im->getImageChannelStatistics();
$alphastat = $stats[imagick::CHANNEL_ALPHA];
if($alphastat['mean'] == 0)
{
non-transparent;
}
else
{
transparent;
}

I tested with transparent and non-transparent gifs, seems like working. I also noticed that for all gifs, the depth value for alpha channel stat is always 1. This is probably why getImageChannelDepth(imagick::CHANNEL_ALPHA) always returns 1, even for jpg images.

If there are other better ways, please let me know. Thanks!
Post Reply