[PHP] determines the dominant color of an image.

Source: Internet
Author: User
Tags 0xc0
[PHP] Recognition of dominant image tones 1. applicable scenarios: for example, www.teapic.comlist.htm. the corresponding image is listed based on the color. 2. main program and API explanation: & lt ;? PhpclassMajorColor {reference color: protected $ _ colorsnull; protected $ _ tolerance80; ignored color p [PHP] recognizes dominant image tones

1. applicable scenarios: for example, http://www.teapic.com/list.htm. the corresponding image is displayed in the color column.



II. main program and API explanation:

 'Imagecreatefrompng ', 'image/jpeg' => 'imagecreatefromjpeg ', 'image/GIF' => 'imagecreatefromgif'); public function _ construct (array $ colors = null) {if (null! ==$ Colors) {$ this-> _ colors = $ colors;} public function setColors (array $ colors) {$ this-> _ colors = $ colors ;} public function setTolerance ($ tolerance) {$ this-> _ tolerance = $ tolerance;} public function setIgnoreColors ($ colors) {$ this-> _ ignoreColors = $ colors ;} public function _ isValidColor ($ confVal, $ val) {if (is_array ($ confVal )) {return $ val >=$ confVal [0] & $ val <= $ confVal [1];} else {return $ val >=$ ConfVal-$ this-> _ tolerance & $ val <= $ confVal + $ this-> _ tolerance ;}} public function getOrderedColors ($ pic) {$ size = getimagesize ($ pic); if (! $ Size) {return false;} $ width = $ size [0]; $ height = $ size [1]; $ mime = $ size ['Mime ']; $ func = isset ($ this-> _ funcs [$ mime])? $ This-> _ funcs [$ mime]: null; if (! $ Func) {return false;} $ im = $ func ($ pic); if (! $ Im) {return false;} $ total = $ width * $ height; $ nums = array (); for ($ I = 0; $ I <$ width; $ I ++) {for ($ m = 0; $ m <$ height; $ m ++) {$ color_index = imagecolorat ($ im, $ I, $ m ); $ color_tran = imagecolorsforindex ($ im, $ color_index); $ alpha = $ color_tran ['alpha']; unset ($ color_tran ['alpha']); if (100 <$ alpha | in_array ($ color_tran, $ this-> _ ignoreColors) {continue ;} foreach ($ this-> _ colors as $ colorid => $ color) {If ($ this-> _ isValidColor ($ color ['red'], $ color_tran ['red']) & $ this-> _ isValidColor ($ color ['green'], $ color_tran ['green']) & $ this-> _ isValidColor ($ color ['blue'], $ color_tran ['blue']) {$ nums [$ colorid] = isset ($ nums [$ colorid])? $ Nums [$ colorid] + 1: 1 ;}}} imagedestroy ($ im); arsort ($ nums); return $ nums;} public function getMajorColor ($ pic) {$ nums = $ this-> getOrderedColors ($ pic); $ keys = array_keys ($ nums); return $ keys [0];}

1. void setColors (array $ colors)

Set the optional color, that is, all colors under "all colors" (white, gray, black ...)


2. void setTolerance (int $ tolerance)

Set the tolerance. for example, if the green RGB value is (0,255, 0) and the tolerance is set to 40, then, all colors in the range of-40 <= R <40 & 215 <= G <= 295 &-40 <= B <= 40 will be treated as green.

This method is used to roughly differentiate colors.


3. void setIgnoreColors (array $ colors)

Set unnecessary colors. For example, the background of most images is white, but we obviously do not want the result to be white. in this case, you can use this method to make it white.


4. array getOrderedColors ($ pic)

Obtain the number of matches of various colors (set with setColors) based on $ pic, and sort by matching volume from high to low

The parameter $ pic is the path of the image to be detected.


5. mix getMajorColor ($ pic)

Call getOrderedColors internally to return the key of the color with the highest matching quantity.


III. determine the format and scope of $ colors

1. if the color differences in $ colors are obvious, we only need to pass in the color value. internally, we will differentiate the colors based on the tolerance set by setTolerance.

$colors = array(1 => array('red' => 0xff, 'green' => 0xff, 'blue' => 0xff),2 => array('red' => 0xc0, 'green' => 0xc0, 'blue' => 0xc0),2 => array('red' => 0x00, 'green' => 0x00, 'blue' => 0x00),);


2. the setTolerance setting method can only distinguish various colors. if you need more precise control, you need to set the R, G, and B ranges of a specific color respectively.

$colors = array(1 => array('red' => array(189, 230), 'green' => array(189, 230), 'blue' => array(189, 230)),2 => array('red' => array(0, 37), 'green' => array(0, 37), 'blue' => array(0, 37)),3 => array('red' => array(128, 255), 'green' => array(0, 76), 'blue' => array(0, 100)),);
A series of fine-tuning is required until various colors can be clearly distinguished.


IV. download demo

Http://download.csdn.net/detail/xiaodao1986/4479821

Points are insufficient to earn points. If you really don't have any credits, contact me via QQ: 88433062.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.