这是一个方便的函数,它可以找到特定像素的第一个出现位置。您可以设置要查找颜色的容差,或者将其设置为 0 如果您想要精确匹配
<?php
function findPixel($img, $r, $g, $b, $tolerance=5)
{
$original_ = new Imagick($img);
$height = 0;
$width = 0;
list($width, $height) = getimagesize($img);
$matrix_org = array();
$matrix_mrk = array();
for( $x = 0 ; $x < $width ; $x++){
$matrix_org[$x] = array();
$matrix_mrk[$x] = array();
}
for( $x = 0 ; $x < $width ; $x++ )
{
for( $y = 0 ; $y < $height ; $y++ ){
$matrix_org[$x][$y] = $original_->getImagePixelColor($x, $y)->getColorAsString();
$colors = preg_replace('/[^-,0-9+$]/', '', $matrix_org[$x][$y]);
$colors = explode(',', $colors);
$r_org = $colors[0];
$g_org = $colors[1];
$b_org = $colors[2];
if( ( $r <= ($r_org+$tolerance) && $r >= ($r_org - $tolerance) )
&& ( $g <= ($g_org+$tolerance) && $g >= ($g_org - $tolerance) )
&& ( $b <= ($b_org+$tolerance) && $b >= ($b_org - $tolerance) ) )
{
return array( $x, $y );
}
}
}
return false;
}
?>