如何获取图像中像素的透明度值?
'imagecolorat'只能选择图像中指定位置像素的颜色索引。通过该索引,我可以获取RGB值,但无法获取透明度值。
希望您能理解,谢谢。
如何获取图像中像素的透明度值?
'imagecolorat'只能选择图像中指定位置像素的颜色索引。通过该索引,我可以获取RGB值,但无法获取透明度值。
希望您能理解,谢谢。
解决方案可能如下:
$colorIndex = imagecolorat($img, $x, $y);
$colorInfo = imagecolorsforindex($img, $colorIndex);
print_r($colorInfo);
这将会打印出类似下面的东西:
Array
(
[red] => 226
[green] => 222
[blue] => 252
[alpha] => 0
)
其中[alpha]是您的透明度值...(从0到127,其中0是完全不透明,127是完全透明)
享受吧!
imagecolorat
返回的。您可以尝试:$color = imagecolorat($image, $x, $y);
$transparency = ($color >> 24) & 0x7F;
透明度是0到127之间的整数,因此我们需要屏蔽32位颜色整数的前8个比特。
0
表示完全不透明,而 127
表示完全透明。在其他环境中,这可能会被反转,例如在 CSS 中,其中 0.0
表示完全透明,而 1.0
表示完全不透明。 - Marten Koetsierimagecolorat
函数返回了16777215。 - Edward