Home  >  Article  >  Backend Development  >  php Imagick gets the RGB color value of the image_PHP tutorial

php Imagick gets the RGB color value of the image_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:51942browse

php Imagick gets the RGB color value of the image

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 "

";

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852826.htmlTechArticlephp Imagick gets the RGB color value of the image, retrieves the main color value of the image based on the image uploaded by the user, and then searches based on the color For related pictures, you can use Imagick's quantizeImage method...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn