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

imagecolorresolve()函数—用法及示例

「 获取图像资源中与指定的红、绿、蓝三个颜色分量最接近的颜色的索引值 」


函数名:imagecolorresolve()

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

用法:imagecolorresolve() 函数用于获取图像资源中与指定的红、绿、蓝三个颜色分量最接近的颜色的索引值。

语法:int imagecolorresolve(resource $image, int $red, int $green, int $blue)

参数:

  • $image:图像资源,由imagecreate()或imagecreatetruecolor()等函数创建。
  • $red:红色分量值,取值范围为0-255。
  • $green:绿色分量值,取值范围为0-255。
  • $blue:蓝色分量值,取值范围为0-255。

返回值:返回一个颜色的索引值。

示例:

// 创建一个宽度为200,高度为100的真彩色图像资源
$image = imagecreatetruecolor(200, 100);

// 将红色分量值设为255,绿色分量值设为0,蓝色分量值设为0
$color = imagecolorresolve($image, 255, 0, 0);

// 使用获取到的颜色索引值将图像填充为红色
imagefill($image, 0, 0, $color);

// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);

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

上述示例中,首先创建一个宽度为200,高度为100的真彩色图像资源。然后使用imagecolorresolve()函数获取与红色最接近的颜色的索引值。接着使用imagefill()函数将整个图像填充为红色。最后使用imagepng()函数将图像输出到浏览器,并通过imagedestroy()函数销毁图像资源。

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