2011-04-18 79 views
6

如何才能獲得透明度圖片上的像素值'imagecolorat'和透明度

'imagecolorat'只挑選圖像中指定位置像素顏色的索引。有了這個索引,我可以得到RGB值,但不是透明的。

希望你能理解,並提前致謝。

回答

6

據我所知,透明度值由函數imagecolorat返回。你可以嘗試:

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

透明度是一個整數0到127之間,所以我們需要屏蔽32位顏色整數的第8位。

+0

哦,謝謝! 我不知道imagecolorat實際上包含alpha值。 – Roman 2011-04-18 13:01:50

10

的解決方案,可以如下:

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

,將打印出類似這樣:

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

其中,α是你透明度值...(從0到127,其中0表示完全以不透明和127完全透明)

享受!

+0

是的,我同意。謝謝你的回答。 – Roman 2011-04-18 13:04:12

+0

嗯,我認爲阿爾法值是從0到255,0意味着透明和255意味着不透明 – user889030 2017-08-16 11:42:17

1

根據PHP手冊imagecolorat返回在指定的X/Y座標(我假設這是GIF和/或PNG-8)的顏色索引。

如果您知道索引,那麼問題是確定文件中的哪個索引是透明索引。

imagecolortransparent可能值得一看,imagecolorsforindex也可能有幫助。