如何在PHP中使用Imagick检查图像是否具有透明度

你可能会想验证图像是否具有透明度的第一件事就是验证文件的格式。支持透明的栅格文件格式为GIF, PNG, BMP, TIFF和JPEG 2000, 可通过透明颜色或alpha通道进行。但是, 就像它们可以具有透明度一样, 它们可能也没有。因此, 通过图像格式进行验证的方法不足以验证你需要的内容。

在本文中, 我们将向你展示如何使用PHP中的Imagick正确验证图像是否透明。

验证透明度

若要检查图像是否具有透明度, 可以使用Imagick的getImageAlphaChannel方法。此方法返回一个整数, 该整数标识为图像的色彩空间常数(有关更多信息, 请检查图像的色彩空间常数)。

对我们来说, 有趣的是Undefined Colorspace常量, 这意味着图像没有透明度。如果图像没有透明度, 则此常量的值为0, 因此可以使用if语句轻松检查它:

<?php

// Create instance of an image
$image = new Imagick();

$image->readImage("your_image.png");

// 0 = No transparency
// 1 = Has transparency
$hasTransparency = $image->getImageAlphaChannel();

if($hasTransparency){
    echo "The image has transparency :)";
}else{
    echo "The image has no transparency :(";
}

如果要使用常数来验证透明度, 则可以轻松地做到这一点:

<?php

// Create instance of the Watermark image
$image = new Imagick();

$image->readImage("your_image.png");

// 0 = No transparency
// 1 = Has transparency
$hasTransparency = $image->getImageAlphaChannel();

if($image->getImageAlphaChannel() == Imagick::COLORSPACE_UNDEFINED){
    echo "The image has no transparency :(";
}else{
    echo "The image has transparency !";
}

如果Alpha通道的结果不是0或1, 则图像具有另一个色彩空间, 你可以通过以下细分来识别该色彩空间:

Constants:
0 - UndefinedColorspace    
1 - RGBColorspace    
2 - GRAYColorspace    
3 - TransparentColorspace    
4 - OHTAColorspace    
5 - LABColorspace    
6 - XYZColorspace    
7 - YCbCrColorspace    
8 - YCCColorspace    
9 - YIQColorspace    
10 - YPbPrColorspace    
11 - YUVColorspace    
12 - CMYKColorspace    
13 - sRGBColorspace    
14 - HSBColorspace    
15 - HSLColorspace    
16 - HWBColorspace

编码愉快!

来源:

https://www.srcmini02.com/60004.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37867.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?