Example of obtaining the RGB color value of an image using php,
Example of obtaining the RGB color value of an image using php
Many image sites retrieve the main color values of the Image Based on the image uploaded by the user, and then search for the relevant image by color.
Previously, the image was scaled (or mosaic) according to the online method, then each pixel was traversed, and the value of RGB was the most frequently. This was too inefficient and the obtained RGB value was not accurate enough. Then we can find that the average RGB value in the image can be easily obtained using the quantizeImage method of Imagick.
$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>";}
Articles you may be interested in:
- Php scans image pixels to obtain and output the color values per pixel
- How does php obtain the image color value?
- Example of php getting the image color value
- Php color value conversion php converts hexadecimal numbers and color values