imagecolorat

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolorat取得某像素的颜色索引值

说明

imagecolorat(GdImage $image, int $x, int $y): int|false

返回 image 所指定的图像中指定位置像素的颜色索引值。

如果图像是真彩色图像,此函数以整数返回该点的 RGB 值。用位移加掩码来取得红,绿,蓝各自的值:

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

x

点的 x 坐标。

y

点的 y 坐标。

返回值

返回颜色的索引 或者在失败时返回 false

警告

此函数可能返回布尔值 false,但也可能返回等同于 false 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource

范例

示例 #1 访问不同的 RGB 值

<?php
$im
= imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

var_dump($r, $g, $b);
?>

以上例程的输出类似于:

int(119)
int(123)
int(180)

示例 #2 使用 imagecolorsforindex() 获取可读的 RGB 值

<?php
$im
= imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);

$colors = imagecolorsforindex($im, $rgb);

var_dump($colors);
?>

以上例程的输出类似于:

array(4) {
  ["red"]=>
  int(119)
  ["green"]=>
  int(123)
  ["blue"]=>
  int(180)
  ["alpha"]=>
  int(127)
}

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top