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