Home >Backend Development >PHP Tutorial >PHP method to obtain the main color of an image
This article mainly introduces the method of obtaining the main color of the picture in PHP. The Imagick extension based on PHP implements the function of obtaining the color value of the picture. Friends who need it can refer to it.
The details are as follows:
The code uses the image extension of PHP, so before using it, you need to install the Imagick extension of PHP.
The code is as follows:
<?php $average = new Imagick("./fruit/143511081321676_593.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 ){ $colorarr[] = $pixel->getColor(); } } return $colorarr; } $colorarr = GetImagesColor($average); foreach($colorarr as $val){ $r += $val['r']; $g += $val['g']; $b += $val['b']; echo "<p style='background-color: rgb({$val['r']},{$val['g']},{$val['b']});width:50px;height:50px;float:left;'></p>"; } $r = round($r/10); $g = round($g/10); $b = round($b/10); echo "<br><p style='background-color: rgb({$r},{$g},{$b});width:100px;height:100px;float:left;'></p>"; ?>##Related recommendations:
PHP determines one Code snippet of the main color of the picture
Using k-means clustering algorithm to identify the pictureMain color_PHP tutorial
[PHP]Identify pictureMain color_PHP tutorial
##
The above is the detailed content of PHP method to obtain the main color of an image. For more information, please follow other related articles on the PHP Chinese website!