Home > Article > Backend Development > php Imagick gets the RGB color value of the image_PHP tutorial
Retrieve the main color value of the image based on the image uploaded by the user, and then search for related images based on the color. Using Imagick's quantizeImage method can easily Conveniently get the average RGB value in the picture
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.
Previously, according to the online method, the image was scaled (or mosaic) and then traversed each pixel, and then the value with the most RGB times was counted. This method was too inefficient and the RGB value obtained was 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 "
";