(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. Git----02本地仓库进行文件添加&修改&删除&查看

    一.将新文件上传到本地仓库----使用小乌龟工具 1.1.将文件添加到暂存区 进入仓库目录,创建文件,添加暂存区     1.2.将文件添加到本地仓库 选中已经添加到暂存区的文件,进行提交 二.查看本 ...

  2. Plasma Cash 合约解读

    作者介绍 虫洞社区·签约作者 steven bai Plasma Cash 合约解读 Plasma Cash 合约解读 1. 合约代码 2. 合约文件简单介绍 3. Plasma Cash 的基础数据 ...

  3. 关于Python的面试题

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.ap ...

  4. 随手记录-linux-vim使用

  5. SpringMvc跨域支持

    SpringMvc跨域支持 在controller层加上注解@CrossOrigin可以实现跨域 该注解有两个参数 1,origins  : 允许可访问的域列表 2,maxAge:飞行前响应的缓存持续 ...

  6. 2-Eighth Scrum Meeting20151208

    任务分配 闫昊: 今日完成:和唐彬讨论研究上届的网络接口代码. 明日任务:商讨如何迁移ios代码到android平台. 唐彬: 今日完成:和闫昊讨论研究上届的网络接口代码. 明日任务:商讨如何迁移io ...

  7. Spring笔记③--spring的命名空间

    p:命名空间: xmlns:p="http://www.springframework.org/schema/p" 作用:简化在xml配置bean的属性 在<bean> ...

  8. 第三次作业---excel导入数据库及显示(2)

    发现第一次做的功能有点复杂,不能理解.而且第一次的想法是在页面上上传文件,连接并导入到数据库,并在页面上显示.后来才看到要求是直接在本地将数据导入数据库就行了,然后显示.所以才出现了一堆看不懂也解决不 ...

  9. DPDK L3fwd 源码阅读

    代码部分 整个L3fwd有三千多行代码,但总体思想就是在L2fwd的基础上,增加网络层的根据 IP 地址进行路由查找的内容. main.c 文件 int main(int argc, char **a ...

  10. 1105 C程序的推导过程