首页 > php教程 > PHP源码 > 正文

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

PHP中文网
发布: 2016-05-23 08:39:10
原创
2368人浏览过

适用php环境,最好能够安装gm(graphicsmagick),速度比gd快很多

  1. pictureColor.class.php

    Cutout老照片上色
    Cutout老照片上色

    Cutout.Pro推出的黑白图片上色

    Cutout老照片上色 20
    查看详情 Cutout老照片上色
<?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];
    }
     
}
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号