PHP imagick detect transparency

人走茶凉 提交于 2019-12-05 01:33:52

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

What's about this?

substr((new Imagick($FILE))->identifyImage()['type'], 0, -5) == 'Alpha'

look at the documentation of identifyImage. You will notice the missing documentation of the functions output. It's just a parsed version of

identify -verbose $FILE (from the imagick package)

where type identifies the image's type (compare source). You can see that imagick returns the value from some MagickTypeOptions array which is defined here. This array contains an -Alpha and -Matte version for every image type if it's color palette contains alpha.

Theoretically you could save an image with such palette without using it, but every decent programm should swith to the non-alpha version in this case. But false positives are possible but should be rare.

Also I don't check for the -Matte image types because in the array is defined in a way that for every image type constant there are two entries with different names (-Alpha and -Matte), but as -Alpha comes first this name will be returned for that image type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!