Obtain the color and color of the image (hexadecimal). it is applicable to the PHP environment. it is best to install GM (GraphicsMagick), which is much faster than GD.
PictureColor. class. php
ColorName ('E: \ project \ lumen \ public \ t.jpg '); * echo $ obj-> hexName ('E: \ project \ lumen \ public \ t.jpg '); ** @ author lock * @ link https://github.com/lock-upme */Class pictureColor {/*** GM Lib */public $ gm = 'C: \ Progra ~ 1 \ GraphicsMagick-1.3.22-Q8 \ gm.exe ';/*** obtain the color using the library type * gd or gm */public $ type = 'gd '; /*** hexadecimal */public $ hex = array ('0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'e ', 'F');/*** obtain the image color ** @ param string $ file * @ return string */public function colorName ($ file) {if (empty ($ file) {return false;} $ rgb = $ this-> getRGB ($ file, $ this-> type ); $ hsl = $ this-> RGB2HSL ($ rgb ); Return $ this-> getColorName ($ hsl );} /*** obtain the image hexadecimal format ** @ param string $ file * @ return string */public function hexName ($ file) {if (empty ($ file )) {return false;} $ rgb = $ this-> getRGB ($ file, $ this-> type); return $ this-> RGB2Hex ($ rgb );} /*** obtain the image RGB ** @ param string $ file * @ param string $ type gd/gm * @ return array */public function getRGB ($ file, $ type = 'gd ') {if (empty ($ file) {return false;} If ($ type = 'gd ') {$ filext = trim (strtolower (strrchr ($ file ,'. ')),'. '); if ($ filext = 'jpg' | $ filext = 'jpeg ') {$ img = ImageCreateFromJpeg ($ file );} elseif ($ filext = 'PNG ') {$ img = imagecreatefrompng ($ file);} elseif ($ filext = 'bmp ') {$ img = imagecreatefromwbmp ($ file);} elseif ($ filext = 'GIF') {$ img = imagecreatefromgif ($ file );} $ w = imagesx ($ img); $ h = imagesy ($ img); $ r = $ g = $ B = 0; for ($ y = 0; $ y <$ h; $ y ++) {for ($ x = 0; $ x <$ w; $ x ++) {$ rgb = imagecolorat ($ img, $ x, $ y); $ r + = $ rgb> 16; $ g + = $ rgb >>8 & 255; $ B + = $ rgb & 255 ;}$ pxls = $ w * $ h; $ r = (round ($ r/$ pxls); $ g = (round ($ g/$ pxls )); $ B = (round ($ B/$ pxls);/* $ r = dechex (round ($ r/$ pxls )); $ g = dechex (round ($ g/$ pxls); $ B = dechex (round ($ B/$ pxls); return $ r. $ g. $ B; */return array ('0' =>$ R, '1' =>$ g, '2' =>$ B);} elseif ($ type = 'g ') {// $ cmd = $ this-> gm. "identify-verbose $ file | grep Mean | awk-F ''' {print $3} '| tr-D '()'"; $ cmd = $ this-> gm. "identify-verbose $ file"; $ res = shell_exec ($ cmd); // print_r ($ res); preg_match_all ('/Mean: \ s + [0-9] + \. [0-9] + \ s \((. *) \)/', $ res, $ match); // print_r ($ match); $ rgb = $ match [1]; if (count ($ rgb )! = 3) {// workaround {TODO: to be fixed} $ rgb ['2'] = $ rgb ['1'] = $ rgb ['0'];} while (list ($ key, $ val) = each ($ rgb) {$ rgb [$ key] = round ($ val * 255, 2 );} return $ rgb ;}} public function RGB2Hex ($ rgb) {$ hexColor = ''; $ hex = $ this-> hex; for ($ I = 0; $ I <3; $ I ++) {$ r = null; $ c = $ rgb [$ I]; $ hexAr = array (); while ($ c> 16) {$ r = $ c % 16; $ c = ($ c/16)> 0; array_push ($ hexAr, $ hex [$ r]);} Array_push ($ hexAr, $ hex [$ c]); $ ret = array_reverse ($ hexAr); $ item = implode ('', $ ret ); $ item = str_pad ($ item, 2, '0', STR_PAD_LEFT); $ hexColor. = $ item;} return $ hexColor;}/*** convert RGB to HSL ** @ param array $ rgb * @ return array */public function RGB2HSL ($ rgb) {list ($ r, $ g, $ B) = $ rgb; $ r/= 255; $ g/= 255; $ B/= 255; $ max = max ($ r, $ g, $ B); $ min = min ($ r, $ g, $ B); $ delta = $ max-$ m In; $ l = ($ max + $ min)/2; if ($ delta = 0) {$ h = 0; $ s = 0 ;} else {$ s = ($ l <0.5 )? $ Delta/($ max + $ min): $ delta/(2-$ max-$ min); $ deltar = ($ max-$ r)/6) + ($ max/2)/$ delta; $ deltag = ($ max-$ g)/6) + ($ max/2)/$ delta; $ deltab = ($ max-$ B)/6) + ($ max/2)/$ delta; if ($ r = $ max) {$ h = $ deltab-$ deltag;} else if ($ g = $ max) {$ h = (1/3) + $ deltar-$ deltab ;} else if ($ B ==$ max) {$ h = (2/3) + $ deltag-$ deltar;} $ h + = ($ h <0 )? 1: ($ h> 1? -1: 0);} return array ($ h * 360, $ s * 100, $ l * 100 );} /*** HSL corresponding color name ** @ param array $ hsl * @ return string */public function getColorName ($ hsl) {$ colorarr = array ('123, 50' => 'red', '2014, 50' => 'Orange ', '2014, 50' => 'yellow', '2014, 30,100, 75 '=> 'green', '1970, 25' => 'Blue', '1970, 25' => 'Purple ', '123' => 'Pink ', // '2014, 84, 24' => 'brown ', '0, 0, 50' => 'gray', '0, 0, 0' => 'black', '0, 808080' => 'white ',); $ distarr = array (); foreach ($ colorarr as $ key => $ val) {list ($ h, $ s, $ l) = explode (',', $ key ); $ distarr [$ key] = pow ($ hsl ['0']-$ h), 2) + pow ($ hsl ['1']-$ s ), 2) + pow ($ hsl ['2']-$ l), 2);} asort ($ distarr); list ($ key) = each ($ distarr ); return $ colorarr [$ key] ;}}