Home >php教程 >php手册 >php Imagick获取图片RGB颜色值,

php Imagick获取图片RGB颜色值,

WBOY
WBOYOriginal
2016-06-13 09:28:171086browse

php Imagick获取图片RGB颜色值,

很多图片站点都会根据用户上传的图片检索出图片的主要颜色值,然后在通过颜色搜索相关的图片。

之前按照网上的方法将图片缩放(或者马赛克)然后遍历每个像素点,然后统计处RGB次数最多的值,这做法效率太低而且取到的RGB值不够精确。之后才发现使用Imagick的quantizeImage方法能够很方便的取到图片中平均的RGB值.

$average = new Imagick("xiaocai.jpg");
$average->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, false, false );
$average->uniqueImageColors();
function GetImagesColor( Imagick $im ){
$colorarr = array();
$it = $im->getPixelIterator();
$it->resetIterator();
while( $row = $it->getNextIteratorRow() ){
foreach ( $row as $pixel ){
// www.jbxue.com
$colorarr[] = $pixel->getColor();
}
}
return $colorarr;
}
$colorarr = GetImagesColor($average);
foreach($colorarr as $val){
echo "<div style='background-color: rgb({$val['r']},{$val['g']},{$val['b']});width:50px;height:50px;float:left;'></div>";
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn