获取图片的色系和颜色值(十六进制)
适用PHP环境,最好能够安装GM(GraphicsMagick),速度比GD快很多
pictureColor.class.php
<?php /** * 取得图片色系名称 * 参考地址 * http://www.cnblogs.com/codingspace/archive/2010/04/09/1707900.html * http://www.workwithcolor.com/color-converter-01.htm * http://www.easyrgb.com/index.php?X=MATH&H=18#text18 * * 用法 * $obj = new pictureColor(); * echo $obj->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'; /** * 获取颜色使用库类型 * gd or gm */ public $type = 'gd'; /** * 十六进制 */ public $hex = array ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); /** * 获得图片色系 * * @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); } /** * 取得图片十六进制 * * @param string $file * @return string */ public function hexName($file) { if (empty($file)) { return false; } $rgb = $this->getRGB($file, $this->type); return $this->RGB2Hex($rgb); } /** * 取得图片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 == 'gm') { //$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; } /** * RGB转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 - $min; $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对应颜色名称 * * @param array $hsl * @return string */ public function getColorName($hsl) { $colorarr = array( '0, 100, 50' => '红色', '30, 100, 50' => '橙色', '60, 100, 50' => '黄色', '120, 100, 75' => '绿色', '240, 100, 25' => '蓝色', '300, 100, 25' => '紫色', '255, 152, 191' => '粉红', //'136, 84, 24' => '棕色', '0, 0, 50' => '灰色', '0, 0, 0' => '黑色', '0, 0, 100' => '白色', ); $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]; } }
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전
By DDD

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7335
9


자바 튜토리얼
1627
14


Cakephp 튜토리얼
1352
46


라라벨 튜토리얼
1264
25


PHP 튜토리얼
1209
29

