imagecolorexact
获取指定颜色的索引
PHP 4.3.0 及以上版本
imagecolorexact 函数用来在图像资源中查找与指定颜色最精确匹配的颜色索引。如果找到匹配的颜色,它会返回该颜色的索引,否则返回 -1。
int imagecolorexact(resource $image, int $red, int $green, int $blue);
返回找到的匹配颜色的索引(整数)。如果没有找到精确匹配的颜色,返回 -1。
以下代码示例展示了如何使用 imagecolorexact 函数在图像中查找精确匹配的颜色。
$image = imagecreate(100, 100); // 创建一个 100x100 的空白图像 $white = imagecolorallocate($image, 255, 255, 255); // 分配一个白色 $black = imagecolorallocate($image, 0, 0, 0); // 分配一个黑色 <p>// 查找与白色最精确匹配的颜色<br> $colorIndex = imagecolorexact($image, 255, 255, 255);</p> <p>if ($colorIndex != -1) {<br> echo "找到匹配的颜色索引: $colorIndex";<br> } else {<br> echo "没有找到精确匹配的颜色";<br> }</p> <p>// 销毁图像资源<br> imagedestroy($image);<br>
在此示例中,我们首先创建了一个 100x100 的空白图像,并分配了白色和黑色。然后,使用 imagecolorexact 函数查找与白色最精确匹配的颜色。如果找到了匹配的颜色,它会返回该颜色的索引;如果没有匹配的颜色,则返回 -1。