php imagecolorat,PHP imagecolorat()用法及代码示例

本文介绍了PHP内置函数imagecolorat(),用于获取图像中指定位置像素的颜色索引。通过该函数,可以获取RGB像素值,并转换为红色、绿色和蓝色分量。示例代码展示了如何使用该函数并输出像素的RGB值,以及通过imagecolorsforindex()函数获取颜色名称及其对应值。
摘要由CSDN通过智能技术生成

imagecolorat()函数是PHP中的内置函数,用于获取像素颜色的索引。此函数返回指定位置的像素值。

用法:

int imagecolorat( $image, $x, $y )

参数:此函数接受上述和以下所述的三个参数:

$image:imagecreatetruecolor()函数用于创建给定尺寸的图像。

$x:此参数用于保存点的x坐标。

$y:此参数用于保存点的y坐标。

返回值:此函数在失败时返回颜色索引(颜色像素值)或FALSE。

下面的程序演示了PHP中的imagecolorat()函数。

注意:下面给出的图像在以下程序中使用。

228c98d6ff62e907625fff7fe242f4b7.png

示例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)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值