2

I am working on a service that can do conversions from gif to mp4 files (with ffmpeg).

My problem is some of the gifs have visible transparent areas which end up as white color when I convert them to mp4 video. To avoid that problem I am trying detect if a gif has visible transparent areas so I will avoid converting them to mp4.

I tried to use getImageAlphaChannel() function from imagick.

   if ($imagick->getImageAlphaChannel()) {
      echo 'transparent image';
   } else {
      echo 'not transparent image';   
   }

This function works correctly reports transparent for images like below; which has obvious visible transparent areas.

transparent gif 1

But it also reports transparent for images like below;

false transparent gif 1

false transparent gif 2

This result is probably correct for imagick , probably above images are transparent , but according to my eyes there are no visible transparent areas.

My question is how can I correctly identify, if a gif file that has visible transparent areas or it is even possible with imagick or any other tool ?

knpercinel
  • 91
  • 1
  • 5
  • Possible duplicate of [PHP imagick detect transparency](https://stackoverflow.com/questions/6742718/php-imagick-detect-transparency) – BlackNetworkBit Sep 11 '18 at 21:46
  • Dump the output of `$imagick->getImageAlphaChannel()`. Do you get an other values besides 0 or 1? If so, you might need to check the colour space. Here's an article that kind of explains what I'm talking about https://ourcodeworld.com/articles/read/473/how-to-check-if-an-image-has-transparency-using-imagick-in-php – Brett Gregson Sep 11 '18 at 23:14
  • @BlackNetworkBit thank you for the help , it is my mistake not to emphasize enough **visible** keyword. I read the question you sent ,before sending this question,but unfortunately it does not help me since I am trying to detect not just transparency also for visible transparency, which is harder I believe. – knpercinel Sep 12 '18 at 14:40
  • @BrettGregson thanks for the help, I also read the article you sent, before asking that question, but it didn't make a difference , same results for me. I don't get any value other than 0 or 1. I was using that code before ; ```if($image->getImageAlphaChannel() == Imagick::COLORSPACE_UNDEFINED){ echo "The image has no transparency :("; }else{ echo "The image has transparency !"; }``` But it didn't make a difference , so I have returned the code that I sent in the question. – knpercinel Sep 12 '18 at 14:44

1 Answers1

0

You can use the Imagick::getImageChannelRange to evaluate the min/max of values used by a specific color channel.

$alphaRange = $imagick->getImageChannelRange(Imagick::CHANNEL_ALPHA);

You can then check if there is any transparency with...

$hasTransparency = $alphaRange['minima'] < $alphaRange['maxima'];
  • If the channel is defined, and has any transparent areas across any frame, then maxima will always be greater then minima.

  • If the channel is NOT defined, then minima will be Inf placeholder, and maxima will be -Inf placeholder, so the above check will still work.

  • If the whole image has a consistent alpha-value (i.e. full transparency, or no data variation), this solution would not work. A fallback check could be something like... minima == maxima AND minima > 0

Another great benefit from evaluating ranges is that you can check the distance between the two min/max values against a threshold, so a "little semi-transparency" can be identified & isolated.

$threshold = $imagick->getQuantum() * 0.1; // < 10% is okay.
$hasTransparency = $alphaRange['minima'] < $alphaRange['maxima']
                 && ($alphaRange['maxima'] - $alphaRange['minima']) < $threshold;
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Hey @emcconville, thanks for helping me , I am totally newbie for imagick, please fix me if I am saying nonsense. gif but afaik gif does not support alpha transparency only index transparency, so – knpercinel Sep 12 '18 at 16:22
  • When I execute and collect values of `$imagick->getImageChannelRange(Imagick::CHANNEL_ALPHA);` I see the same numbers for many transparent and non-transparent gifs , which does not help me , I get the same results for those gifs – knpercinel Sep 12 '18 at 18:12