> php教程 > PHP源码 > 获取图片的色系和颜色值(十六进制)

获取图片的色系和颜色值(十六进制)

PHP中文网
풀어 주다: 2016-05-23 08:39:10
원래의
2037명이 탐색했습니다.

适用PHP环境,最好能够安装GM(GraphicsMagick),速度比GD快很多

  1. 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(&#39;E:\project\lumen\public\t.jpg&#39;);
 * echo $obj->hexName(&#39;E:\project\lumen\public\t.jpg&#39;);
 *
 * @author lock
 * @link https://github.com/lock-upme
 */
 
 
 
class pictureColor
{
    /**
     * GM Lib
     */
    public $gm = &#39;C:\Progra~1\GraphicsMagick-1.3.22-Q8\gm.exe&#39;;
     
    /**
     * 获取颜色使用库类型
     * gd or gm
     */
    public $type = &#39;gd&#39;;
     
    /**
     * 十六进制
     */
    public $hex = array (&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;);
     
    /**
     * 获得图片色系
     *
     * @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=&#39;gd&#39;) {
        if (empty($file)) { return false; }
         
        if ($type == &#39;gd&#39;) {
            $filext = trim(strtolower(strrchr($file, &#39;.&#39;)),&#39;.&#39;);
            if ($filext == &#39;jpg&#39; ||  $filext == &#39;jpeg&#39;) {
                $img = ImageCreateFromJpeg($file);
            } elseif ($filext == &#39;png&#39;) {
                $img = imagecreatefrompng($file);
            } elseif ($filext == &#39;bmp&#39;) {
                $img = imagecreatefromwbmp($file);
            } elseif ($filext == &#39;gif&#39;) {
                $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( &#39;0&#39; => $r, &#39;1&#39; => $g, &#39;2&#39; => $b );
             
        } elseif ($type == &#39;gm&#39;) {  
            //$cmd = $this->gm. " identify -verbose $file | grep Mean | awk -F&#39; &#39; &#39;{print $3}&#39; | tr -d &#39;()&#39;";
            $cmd = $this->gm . " identify -verbose $file";
            $res = shell_exec($cmd);
            //print_r($res);
             
            preg_match_all(&#39;/Mean:\s+[0-9]+\.[0-9]+\s\((.*)\)/&#39;, $res, $match);
            //print_r($match);
            $rgb = $match[1];
             
            if (count($rgb) != 3) { //workaround{TODO:to be fixed}
                $rgb[&#39;2&#39;] = $rgb[&#39;1&#39;] = $rgb[&#39;0&#39;];
            }
            while (list($key, $val) = each($rgb)) {
                $rgb[$key] = round($val * 255, 2);
            }
            return $rgb;
        }   
    }
     
    public function RGB2Hex($rgb) {
        $hexColor = &#39;&#39;;
        $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 ( &#39;&#39;, $ret );
            $item = str_pad ( $item, 2, &#39;0&#39;, 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(
                &#39;0, 100, 50&#39; => &#39;红色&#39;,
                &#39;30, 100, 50&#39; => &#39;橙色&#39;,
                &#39;60, 100, 50&#39; => &#39;黄色&#39;,
                &#39;120, 100, 75&#39; => &#39;绿色&#39;,
                &#39;240, 100, 25&#39; => &#39;蓝色&#39;,
                &#39;300, 100, 25&#39; => &#39;紫色&#39;,
                &#39;255, 152, 191&#39; => &#39;粉红&#39;,
                //&#39;136, 84, 24&#39; => &#39;棕色&#39;,
                &#39;0, 0, 50&#39; => &#39;灰色&#39;,
                &#39;0, 0, 0&#39; => &#39;黑色&#39;,
                &#39;0, 0, 100&#39; => &#39;白色&#39;,
        );
        $distarr = array();
        foreach ($colorarr as $key => $val) {
            list($h, $s, $l) = explode(&#39;,&#39;, $key);
            $distarr[$key] = pow(($hsl[&#39;0&#39;] - $h), 2) + pow(($hsl[&#39;1&#39;] - $s), 2) + pow(($hsl[&#39;2&#39;] - $l), 2);
        }
        asort($distarr);
        list($key) = each($distarr);
        return $colorarr[$key];
    }
     
}
로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿