如何知道图片中是否有颜色


How to know if a color is in a picture

如何知道PHP中的图片中是否有颜色?

function colorIsInPicture('path/to/picture.jpg', '#f55'){ }    

如果安装了PHP Imagick扩展,则可以使用一种方法。

在每个像素上循环,寻找目标颜色。

/**
 * Warning: Untested, but *should* work.
 * Takes a path to the image file and a color in the form 'rgb(r,g,b)'.
 * Left as exercise to reader to test and write any hex support for $sColor.
 */
function colorIsInPicture($sPath, $sColor)
{
    $im = new Imagick($sPath);
    $it = $im->getPixelIterator();
    foreach($it as $row => $pixels)
        foreach($pixels as $column => $pixel)
            if($pixel->getColorAsString() == $sColor)
                return true;
    return false;
}

您需要使用一个检查像素颜色的函数来遍历每个像素。

imagecolorat()(示例#2)