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

imagecolorsforindex()函数—用法及示例

「 获取图像指定索引的颜色信息 」


函数名:imagecolorsforindex()

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

用法:imagecolorsforindex() 函数用于获取图像指定索引的颜色信息。

语法:bool imagecolorsforindex ( resource $image , int $index , array &$color )

参数:

  • $image:一个图像资源,通过函数 imagecreate() 或 imagecreatefromXXX() 创建。
  • $index:指定要获取颜色信息的调色板索引,范围从 0 到 imagecolorstotal($image) - 1。
  • $color:一个包含 RGB 颜色信息的数组,用于存储获取到的颜色信息。

返回值:成功时返回 true,失败时返回 false。

示例:

// 创建一个图像资源
$image = imagecreatefromjpeg('image.jpg');

// 获取索引为 10 的颜色信息
$index = 10;
$color = array();
if (imagecolorsforindex($image, $index, $color)) {
    // 输出颜色信息
    echo "Red: " . $color['red'] . "<br>";
    echo "Green: " . $color['green'] . "<br>";
    echo "Blue: " . $color['blue'] . "<br>";
} else {
    echo "获取颜色信息失败";
}

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

注意事项:

  • 在调用 imagecolorsforindex() 函数之前,需要先创建一个图像资源。
  • 调色板索引范围从 0 到 imagecolorstotal($image) - 1,可以通过函数 imagecolorstotal() 获取图像中使用的调色板颜色总数。
  • 颜色信息将存储在传递给函数的 $color 数组中,包括 'red'(红色)、'green'(绿色)和 'blue'(蓝色)三个键。
补充纠错
上一个函数: imagecolorstotal()函数
下一个函数: imagecolorset()函数
热门PHP函数
分享链接