imagecolorat()函数是PHP中的内置函数,用于获取像素颜色的索引。此函数返回指定位置的像素值。
用法:
int imagecolorat( $image, $x, $y )
参数:此函数接受上述和以下所述的三个参数:
$image:imagecreatetruecolor()函数用于创建给定尺寸的图像。
$x:此参数用于保存点的x坐标。
$y:此参数用于保存点的y坐标。
返回值:此函数在失败时返回颜色索引(颜色像素值)或FALSE。
下面的程序演示了PHP中的imagecolorat()函数。
注意:下面给出的图像在以下程序中使用。
示例1:
// store the image in variable
$image = imagecreatefrompng("gfg.png");
// Calculate rgb pixel value at perticular point.
$rgb = imagecolorat($image, 30, 25);
$red = ($rgb >> 16) & 255;
$green = ($rgb >> 8) & 255;
$blue = $rgb & 255;
var_dump($red, $green, $blue);
?>
输出:
int(34)
int(170)
int(66)
示例2:
// store the image in variable.
$image = imagecreatefrompng("gfg.png");
// Calculate rgb pixel value at perticular point.
$rgb = imagecolorat($image, 30, 25);
// Assign color name and its value.
$colors = imagecolorsforindex($image, $rgb);
var_dump($colors);
?>
输出:
array(4) {
["red"]=> int(34)
["green"]=> int(170)
["blue"]=> int(66)
["alpha"]=> int(0)
}