Home  >  Article  >  Backend Development  >  php Imagick gets the RGB color value of the image, _PHP tutorial

php Imagick gets the RGB color value of the image, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:08868browse

php Imagick gets the RGB color value of the image,

Many picture sites will retrieve the main color value of the picture based on the picture uploaded by the user, and then search for related pictures by color.

I used to scale (or mosaic) the image according to the online method and then traverse each pixel, and then count the value with the most RGB times. This method is too inefficient and the RGB value obtained is not accurate enough. Later I discovered that using Imagick’s quantizeImage method can easily get the average RGB value in the image.

$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>";
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851334.htmlTechArticlephp Imagick gets the RGB color value of the image. Many image sites will retrieve the main color value of the image based on the image uploaded by the user. , and then search for related pictures by color. Previously follow...
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