Home  >  Article  >  Backend Development  >  Example of getting the RGB color value of an image in php, _PHP tutorial

Example of getting the RGB color value of an image in php, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:331053browse

Example of php getting the RGB color value of an image,

Example of php getting the RGB color value of an image

Many picture sites The main color value of the image will be retrieved based on the image uploaded by the user, and then related images will be searched by color.

I used to scale (or mosaic) the image according to the online method 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 image.

<span>$average</span> = <span>new</span> Imagick("xiaocai.jpg"<span>);

</span><span>$average</span>->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, <span>false</span>, <span>false</span><span> );

</span><span>$average</span>-><span>uniqueImageColors();

</span><span>function</span> GetImagesColor( Imagick <span>$im</span><span> ){

</span><span>$colorarr</span> = <span>array</span><span>();

</span><span>$it</span> = <span>$im</span>-><span>getPixelIterator();

</span><span>$it</span>-><span>resetIterator();

</span><span>while</span>( <span>$row</span> = <span>$it</span>-><span>getNextIteratorRow() ){

</span><span>foreach</span> ( <span>$row</span> <span>as</span> <span>$pixel</span><span> ){
// www.jbxue.com
</span><span>$colorarr</span>[] = <span>$pixel</span>-><span>getColor();

}

}

</span><span>return</span> <span>$colorarr</span><span>;

}

</span><span>$colorarr</span> = GetImagesColor(<span>$average</span><span>);

</span><span>foreach</span>(<span>$colorarr</span> <span>as</span> <span>$val</span><span>){

</span><span>echo</span> "<div style='background-color: rgb({<span>$val</span>['r']},{<span>$val</span>['g']},{<span>$val</span>['b']});width:50px;height:50px;float:left;'></div>"<span>;

}</span><span> </span>

Articles you may be interested in:

  • PHP scans the pixels of the image to obtain the color value of each pixel and outputs it
  • What are the ways to get the color value of an image in PHP?
  • Example of php getting the color value of an image
  • php color value conversion php realizes the conversion of hexadecimal numbers and color values

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/848236.htmlTechArticleExample of php getting the RGB color value of an image, php example of getting the RGB color value of an image. Many image sites will upload according to the user Retrieve the main color value of the picture, and then use the color...
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