Home >Backend Development >PHP Tutorial >Two ways to get the color value of an image in php
Two ways for php to get the color value of an image
Example 2, php gets the main RGB color value of an image. Retrieve the main color value of the image based on the image uploaded by the user, and then search for related images by color. Follow the online method to scale (or mosaic) the image 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 picture. (bbs.it-home.org Scripting School) Code: ?php $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 ){ $colorarr[] = $pixel->getColor(); } } // bbs.it-home.org return $colorarr; } $colorarr = GetImagesColor($average); foreach($colorarr as $val){ echo " "; } Output result: |