(1)工具类:pictureColor.php

class pictureColor
{     /**
     * 获取颜色使用库类型
     */
    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);
        $hsl = $this->RGB2HSL($rgb);
        return $this->getColorName($hsl);
    }     /**
     * 取得图片十六进制
     *
     * @param string $file
     * @return string
     */
    public function hexName($file)
    {
        try {
            if (empty($file)) {
                return false;
            }
            $rgb = $this->getRGB($file, $this->type);
            $color = $this->RGB2Hex($rgb);
            if (strlen($color) > 6) $color = substr($color, 1, 6);
            return $color;
        } catch (Exception $e) {
            echo $e;
        }
    }     /**
     * 取得图片RGB
     *
     * @param string $file
     * @param string $type gd/gm
     * @return array
     */
    public function getRGB($file)
    {
        if (empty($file)) {
            return false;
        }
        $imageSize = getimagesize($file);
        if ($imageSize['mime'] == 'image/jpeg') {
            $img = imagecreatefromjpeg($file);
        } elseif ($imageSize['mime'] == 'image/png') {
            $img = imagecreatefrompng($file);
        } elseif ($imageSize['mime'] == 'image/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));
        return array('0' => $r, '1' => $g, '2' => $b);
    }     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];     }
}

(2)调用工具类:index.php

//引入工具类
include_once './pictureColor.php';
$pictureColor = new pictureColor();
$picUrls = array('.\pics\a.jpg','.\pics\b.jpg','.\pics\c.jpg');
foreach ($picUrls as $key=>$url){
    //获取色值
    $color=$pictureColor->hexName($url);
    //获取颜色名称
    $colorName=$pictureColor->colorName($url);
    echo "<img style='width:100px;float: left;' src='$url'>";
    echo "<div style='width: 100px;height: 100px;float: left;'>$colorName</div>";
}
 

PHP获取图片主题颜色的更多相关文章

  1. [Swift通天遁地]五、高级扩展-(5)获取互补色、渐变色、以及图片主题颜色

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. php 获取图片主要颜色的方法

    本文章向码农们介绍php 获取图片主要颜色的方法,主要涉及php针对图片的相关操作技巧,需要的码农可以参考一下. $i = imagecreatefromjpeg("image.jpg&qu ...

  3. 获取当前主题颜色 Flutter

    通过context获取当前主题颜色   Theme.of(context).accentColor

  4. uwp 用win2d获取图片主调颜色

    win10在设置颜色里有个从“背景图片中选取一种主题颜色”的选项,还有在很多内容展示软件中都使用了这样的功能. 现在我们需要在 nuget 引用 win2d.uwp 和 Toolkit.uwp 两个库 ...

  5. 小技巧!CSS 提取图片主题色功能探索

    本文将介绍一种利用 CSS 获取图片主题色的小技巧.一起看看~ 背景 起因是微信技术群里有个同学发问,有什么方法能够获取图片的主色呢?有一张图片,获取他的主色调: 利用获取到的这个颜色值,来实现类似这 ...

  6. php 提取图片主要颜色

    PHP实现获取图片颜色值的方法 PHP获取图片颜色值检测图片主要颜色是通过imagecreatefromjpeg函数读取图片,再循环获得各个颜色值加以计算实现的. /** * 获取图片主要颜色 * @ ...

  7. iOS 开发之提取图片的主色调用于更换应用主题颜色

    从刷爆 IT 圈的一个事件说起: 新闻:某互联网公司产品经理提出一个需求--要求APP开发人员做到软件根据用户的手机壳改变软件的主题颜色. What Fuck!还有这操作,PM,你过来,保证不打屎你. ...

  8. cocos2d-x 获取图片的某像素点的RGBA颜色 -转

    cocos2d-x 获取图片的某像素点的RGBA颜色  原文:http://www.cnblogs.com/jaoye/archive/2013/02/19/2916501.html 没做过 太多的图 ...

  9. Android5.0新特性——图片和颜色(drawable)

    图片和颜色 tint属性 tint属性一个颜色值,可以对图片做颜色渲染,我们可以给view的背景设置tint色值,给ImageView的图片设置tint色值,也可以给任意Drawable或者NineP ...

随机推荐

  1. eclipse xml文件中按alt+/没有提示信息

    转载地址:http://blog.sina.com.cn/s/blog_972ddc1b01012mmh.html 今天要写这篇博文是因为遇到这样的不是技术的问题,但找到问题根源再解决这个问题又花费很 ...

  2. [leetcode-897-Increasing Order Search Tree]

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  3. 数据库——SQL数据定义

    数据定义  SQL的数据定义语句 操 作 对 象 操  作  方  式 创  建 删  除 修  改 表 CREATE TABLE DROP TABLE ALTER TABLE 视  图 CREATE ...

  4. Microsoft Visual Studio 2013 的安装及单元测试

    题目:练习教科书第22~25页单元测试练习,要求自行安装Visual Studio开发平台,版本至少在2010以上,要求把程序安装过程和练习过程写到博客上,越详细越好,要图文并茂. 安装过程: 1.下 ...

  5. Teamproject Week7 --Scrum Meeting #1 2014.10.28

    这是团队的第一次会议,具体议题如下: 1)我们明确了团队成员的职责所需: PM职责:根据项目范围.质量.时间与成本的综合因素的考虑,进行项目的总体规划与阶段计划.  控制项目组各成员的工作进度,即时了 ...

  6. 3、昨天的BUG

    基本功能实现了,但是有一些小问题,修改昨天余留的BUG

  7. Gogoing的NABCD

    特点之一:路线推荐 N  用户出行需要一个合理的路线计划 A 运用百度地图,还有根据自己的所想去的地方,推荐最省时间,最省钱的路线安排 B 方便用户出行,节约时间,节约金钱 C 对于旅行方面的App, ...

  8. 封装,策略,Asp换脸

    封装.策略 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  9. 2018软工实践—Beta冲刺(4)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 完成软件开发技术文稿 展示GitHub当日代码/文档签入 ...

  10. Linux操作系统(二)

    SSD工作原理:http://www.360doc.com/content/15/0318/15/16824943_456186965.shtml HHD工作原理:http://blog.csdn.n ...