"imagecolorat" and transparency

How can I get the pixel transparency value in an image ?

'imagecolorat' selects only the pixel color index at the specified location in the image. With this index, I can get RGB values, but not transparent.

I hope you will understand in advance in advance.

+6
source share
3 answers

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

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

Transparency is an integer from 0 to 127, so we need to hide the first 8 bits of a 32-bit integer color.

+6
source

The solution may be as follows:

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

which 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 the total opaque and 127 is completely transparent)

Enjoy it!

+10
source

According to the PHP manual, imagecolorat returns the color index in the specified X / Y coordinates (I assume this is for GIF and / or PNG-8).

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

imagecolortransparent may be worth a look, imagecolorsforindex can also be useful.

+1
source

Source: https://habr.com/ru/post/886141/


All Articles