'imagecolorat' and transparency

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 12:11:19

问题


How it's possible to get the transparency value of a pixel on an image?

'imagecolorat' picks only the index of the color of the pixel at the specified location in the image. With that index I can get the RGB values but not the transparent one.

Hope you understand, and thank you in advance.


回答1:


As far as I know, the transparency value is returned by the function imagecolorat. Could you try:

$color        = imagecolorat($image, $x, $y);
$transparency = ($color >> 24) & 0x7F;

The transparency is a integer between 0 and 127 so we need to mask the first 8 bits of the 32bit color integer.




回答2:


the solution could be following:

$colorIndex = imagecolorat($img, $x, $y);
$colorInfo = imagecolorsforindex($img, $colorIndex);
print_r($colorInfo);

that will print something like:

Array
(
   [red] => 226
   [green] => 222
   [blue] => 252
   [alpha] => 0
)

where [alpha] is Your transparency value... (from 0 to 127 where 0 is totaly opaque and 127 is totaly transparent)

Enjoy!




回答3:


According to the PHP manual imagecolorat returns the index of the colour at the specified X/Y coordinates (i'm assuming this is for GIF and/or PNG-8).

If you know the index, then the problem is determining which index in the file is the transparent one.

imagecolortransparent might be worth looking at, imagecolorsforindex may also be helpful.



来源:https://stackoverflow.com/questions/5702953/imagecolorat-and-transparency

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