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

时间:2014-11-12 09:09:34

标签: php image

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

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

2 个答案:

答案 0 :(得分:0)

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

imagecolorat()(示例#2)

答案 1 :(得分:0)

如果安装了the PHP Imagick extension,可以采取一种方法。

遍历每个像素,寻找目标颜色。

/**
 * 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;
}