Home >Backend Development >PHP Tutorial >Introduction to how to get the main color of an image in PHP
This article mainly introduces the method of obtaining the main color of the picture through PHP programming. The Imagick extension based on PHP implements the function of obtaining the color value of the picture. Friends in need can refer to the following
This article describes PHP with examples Programmatically obtain the main color of an image. Share it with everyone for your reference, 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 specific installation is as follows (under windows): Installing PHP under windows7 Imagick and imagemagick extension tutorial
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>"; ?>
The above is the detailed content of Introduction to how to get the main color of an image in PHP. For more information, please follow other related articles on the PHP Chinese website!