Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'imagecolorat' and transparency

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.

like image 758
Roman Avatar asked Apr 18 '11 12:04

Roman


3 Answers

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!

like image 163
shadyyx Avatar answered Nov 05 '22 12:11

shadyyx


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.

like image 38
Stefan Gehrig Avatar answered Nov 05 '22 12:11

Stefan Gehrig


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.

like image 40
GordonM Avatar answered Nov 05 '22 14:11

GordonM