├── README.md └── pictureColor.class.php /README.md: -------------------------------------------------------------------------------- 1 | #Php pictureColor class 2 | 3 | #用法 4 | $obj = new pictureColor(); 5 | 6 | echo $obj->colorName('E:\project\lumen\public\t.jpg'); 7 | 8 | echo $obj->hexName('E:\project\lumen\public\t.jpg'); 9 | 10 | -------------------------------------------------------------------------------- /pictureColor.class.php: -------------------------------------------------------------------------------- 1 | colorName('E:\project\lumen\public\t.jpg'); 12 | * echo $obj->hexName('E:\project\lumen\public\t.jpg'); 13 | * 14 | * @author lock 15 | * @link https://github.com/lock-upme 16 | */ 17 | 18 | 19 | 20 | class pictureColor 21 | { 22 | /** 23 | * GM Lib 24 | */ 25 | public $gm = 'C:\Progra~1\GraphicsMagick-1.3.22-Q8\gm.exe'; 26 | 27 | /** 28 | * 获取颜色使用库类型 29 | * gd or gm 30 | */ 31 | public $type = 'gd'; 32 | 33 | /** 34 | * 十六进制 35 | */ 36 | public $hex = array ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); 37 | 38 | /** 39 | * 获得图片色系 40 | * 41 | * @param string $file 42 | * @return string 43 | */ 44 | public function colorName($file) { 45 | if (empty($file)) { return false; } 46 | $rgb = $this->getRGB($file, $this->type); 47 | $hsl = $this->RGB2HSL($rgb); 48 | return $this->getColorName($hsl); 49 | } 50 | 51 | /** 52 | * 取得图片十六进制 53 | * 54 | * @param string $file 55 | * @return string 56 | */ 57 | public function hexName($file) { 58 | if (empty($file)) { return false; } 59 | $rgb = $this->getRGB($file, $this->type); 60 | return $this->RGB2Hex($rgb); 61 | } 62 | 63 | /** 64 | * 取得图片RGB 65 | * 66 | * @param string $file 67 | * @param string $type gd/gm 68 | * @return array 69 | */ 70 | public function getRGB($file, $type='gd') { 71 | if (empty($file)) { return false; } 72 | 73 | if ($type == 'gd') { 74 | $filext = trim(strtolower(strrchr($file, '.')),'.'); 75 | if ($filext == 'jpg' || $filext == 'jpeg') { 76 | $img = ImageCreateFromJpeg($file); 77 | } elseif ($filext == 'png') { 78 | $img = imagecreatefrompng($file); 79 | } elseif ($filext == 'bmp') { 80 | $img = imagecreatefromwbmp($file); 81 | } elseif ($filext == 'gif') { 82 | $img = imagecreatefromgif($file); 83 | } 84 | $w = imagesx($img); 85 | $h = imagesy($img); 86 | $r = $g = $b = 0; 87 | for($y = 0; $y < $h; $y++) { 88 | for($x = 0; $x < $w; $x++) { 89 | $rgb = imagecolorat($img, $x, $y); 90 | $r += $rgb >> 16; 91 | $g += $rgb >> 8 & 255; 92 | $b += $rgb & 255; 93 | } 94 | } 95 | $pxls = $w * $h; 96 | 97 | $r = (round($r / $pxls)); 98 | $g = (round($g / $pxls)); 99 | $b = (round($b / $pxls)); 100 | /* 101 | $r = dechex (round($r / $pxls)); 102 | $g = dechex (round($g / $pxls)); 103 | $b = dechex (round($b / $pxls)); 104 | return $r.$g.$b; 105 | */ 106 | return array( '0' => $r, '1' => $g, '2' => $b ); 107 | 108 | } elseif ($type == 'gm') { 109 | //$cmd = $this->gm. " identify -verbose $file | grep Mean | awk -F' ' '{print $3}' | tr -d '()'"; 110 | $cmd = $this->gm . " identify -verbose $file"; 111 | $res = shell_exec($cmd); 112 | //print_r($res); 113 | 114 | preg_match_all('/Mean:\s+[0-9]+\.[0-9]+\s\((.*)\)/', $res, $match); 115 | //print_r($match); 116 | $rgb = $match[1]; 117 | 118 | if (count($rgb) != 3) { //workaround{TODO:to be fixed} 119 | $rgb['2'] = $rgb['1'] = $rgb['0']; 120 | } 121 | while (list($key, $val) = each($rgb)) { 122 | $rgb[$key] = round($val * 255, 2); 123 | } 124 | return $rgb; 125 | } 126 | } 127 | 128 | public function RGB2Hex($rgb) { 129 | $hexColor = ''; 130 | $hex = $this->hex; 131 | for($i = 0; $i < 3; $i ++) { 132 | $r = null; 133 | $c = $rgb [$i]; 134 | $hexAr = array (); 135 | 136 | while ( $c > 16 ) { 137 | $r = $c % 16; 138 | $c = ($c / 16) >> 0; 139 | array_push ( $hexAr, $hex [$r] ); 140 | } 141 | array_push ( $hexAr, $hex [$c] ); 142 | 143 | $ret = array_reverse ( $hexAr ); 144 | $item = implode ( '', $ret ); 145 | $item = str_pad ( $item, 2, '0', STR_PAD_LEFT ); 146 | $hexColor .= $item; 147 | } 148 | return $hexColor; 149 | } 150 | 151 | /** 152 | * RGB转HSL 153 | * 154 | * @param array $rgb 155 | * @return array 156 | */ 157 | public function RGB2HSL($rgb) { 158 | list($r, $g, $b) = $rgb; 159 | $r /= 255; 160 | $g /= 255; 161 | $b /= 255; 162 | $max = max($r, $g, $b); 163 | $min = min($r, $g, $b); 164 | $delta = $max - $min; 165 | $l = ($max + $min) / 2; 166 | 167 | if ($delta == 0) { 168 | $h = 0; 169 | $s = 0; 170 | } else { 171 | $s = ($l < 0.5) ? $delta / ($max + $min) : $delta / (2 - $max - $min); 172 | 173 | $deltar = ((($max - $r) / 6) + ($max / 2)) / $delta; 174 | $deltag = ((($max - $g) / 6) + ($max / 2)) / $delta; 175 | $deltab = ((($max - $b) / 6) + ($max / 2)) / $delta; 176 | 177 | if ($r == $max) { 178 | $h = $deltab - $deltag; 179 | } else if ($g == $max) { 180 | $h = (1 / 3) + $deltar - $deltab; 181 | } else if ($b == $max) { 182 | $h = (2 / 3) + $deltag - $deltar; 183 | } 184 | $h += ($h < 0) ? 1 : ($h > 1 ? -1 : 0); 185 | } 186 | return array($h * 360, $s * 100, $l * 100); 187 | } 188 | 189 | /** 190 | * HSL对应颜色名称 191 | * 192 | * @param array $hsl 193 | * @return string 194 | */ 195 | public function getColorName($hsl) { 196 | $colorarr = array( 197 | '0, 100, 50' => '红色', 198 | '30, 100, 50' => '橙色', 199 | '60, 100, 50' => '黄色', 200 | '120, 100, 75' => '绿色', 201 | '240, 100, 25' => '蓝色', 202 | '300, 100, 25' => '紫色', 203 | '255, 152, 191' => '粉红', 204 | //'136, 84, 24' => '棕色', 205 | '0, 0, 50' => '灰色', 206 | '0, 0, 0' => '黑色', 207 | '0, 0, 100' => '白色', 208 | ); 209 | $distarr = array(); 210 | foreach ($colorarr as $key => $val) { 211 | list($h, $s, $l) = explode(',', $key); 212 | $distarr[$key] = pow(($hsl['0'] - $h), 2) + pow(($hsl['1'] - $s), 2) + pow(($hsl['2'] - $l), 2); 213 | } 214 | asort($distarr); 215 | list($key) = each($distarr); 216 | return $colorarr[$key]; 217 | } 218 | 219 | } --------------------------------------------------------------------------------