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

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

May 23, 2016 am 08:39 AM

适用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];
    }
     
}
Copier après la connexion
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)