English | 简体中文 | 繁體中文
查询

imagecolorat()函数—用法及示例

「 返回指定坐标处的像素的颜色索引值 」


函数名称:imagecolorat()

适用版本:PHP 4, PHP 5, PHP 7

函数说明:imagecolorat() 函数返回指定坐标处的像素的颜色索引值。

语法:int imagecolorat ( resource $image , int $x , int $y )

参数:

  • image: 要获取颜色信息的图像资源。
  • x: 指定像素的 x 坐标。
  • y: 指定像素的 y 坐标。

返回值:返回指定坐标的像素的颜色索引值。

示例:

// 创建一个 200x200 的画布
$image = imagecreate(200, 200);

// 设置背景颜色为白色
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

// 在画布上绘制一个红色的矩形
$red = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image, 50, 50, 150, 150, $red);

// 获取指定坐标处的像素颜色索引值
$colorIndex = imagecolorat($image, 100, 100);

// 将颜色索引值转换为 RGB 值
$rgb = imagecolorsforindex($image, $colorIndex);

// 打印输出颜色信息
echo "Red: " . $rgb['red'] . ", Green: " . $rgb['green'] . ", Blue: " . $rgb['blue'];

// 销毁图像资源
imagedestroy($image);

输出结果: Red: 255, Green: 0, Blue: 0

补充纠错
上一个函数: imagecolorclosest()函数
下一个函数: imagecolorexactalpha()函数
热门PHP函数
分享链接