Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP imagick detect transparency

I want to be able to detect whether an image is transparent or not using the Imagick PHP extension.

So far, the only luck I've been having is to run the exec() / some other command, and use the ImageMagick command line tool to achieve this. Here's what I mean:

exec("identify -verbose example_transparent_image.png | grep \"Alpha\"", $output);
$is_transparent = !empty($output) ? true : false;

The logic is simple. Do a verbose check on the image in question: if the output contains any alpha information, that means it uses transparency.

It seems that the PHP imagick extension should have this as one of its commands, but the lack of documentation is killing me. It seems silly to have to run this kind of check each time.

like image 256
Scott Avatar asked Jul 19 '11 05:07

Scott


1 Answers

Ahhh, solved (I think). Imagick has a function getImageAlphaChannel() which returns true if it contains any alpha information and false if it doesn't.

Make sure you have ImageMagick 6.4.0 or newer.

http://www.php.net/manual/en/function.imagick-getimagealphachannel.php

like image 82
Scott Avatar answered Sep 21 '22 03:09

Scott