我希望能够使用 Imagick PHP 扩展来检测图像是否透明。
到目前为止,我唯一的幸运是运行 exec() /其他一些命令,并使用 ImageMagick 命令行工具来实现这一点。我的意思是:
exec("identify -verbose example_transparent_image.png | grep \"Alpha\"", $output);
$is_transparent = !empty($output) ? true : false;
逻辑很简单。对相关图像进行详细检查:如果输出包含任何 Alpha 信息,则意味着它使用透明度。
似乎 PHP imagick 扩展应该将此作为其命令之一,但缺乏文档让我很沮丧。每次都必须运行这种检查似乎很愚蠢。
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.
发布评论
评论(3)
啊哈,解决了(我想)。 Imagick 有一个函数 getImageAlphaChannel() 如果包含任何 alpha 信息则返回 true,如果不包含则返回 false。
确保您有 ImageMagick 6.4.0 或更高版本。
http://www.php.net/manual/en/function.imagick -getimagealphachannel.php
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
也许这个
http://ru.php.net/manual/en/function .imagick-identifyimage.php
Maybe this
http://ru.php.net/manual/en/function.imagick-identifyimage.php
这是怎么回事?
查看identifyImage 文档。您会注意到函数输出缺少文档。它只是
type
标识图像类型的解析版本(比较 imagick 返回以下值:一些定义为MagickTypeOptions数组href="http://git.imagemagick.org/repos/ImageMagick/blob/master/MagickCore/option.c#L1703" rel="nofollow">此处。如果调色板包含 Alpha,则此数组包含每个图像类型的 -Alpha 和 -Matte 版本。理论上,您可以使用这样的调色板保存图像而不使用它,但在这种情况下,每个像样的程序都应该切换到非 Alpha 版本。但误报是可能的,但应该很少见。
另外,我不检查 -Matte 图像类型,因为在数组中定义的方式是,对于每个图像类型常量,都有两个具有不同名称的条目(-Alpha 和 -Matte),但当 -Alpha 首先出现时,将为该图像类型返回该名称。
What's about this?
look at the documentation of identifyImage. You will notice the missing documentation of the functions output. It's just a parsed version of
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.