稻田代码

php获取图片主题颜色

文章简介
本文提供一个获取图片主题颜色的php工具类。

使用场景
比如做一个服装app或者网站,需要根据颜色对商品进行分类。用户点击红色分类按钮,出现红色的衣服,点击绿色分类按钮展示绿色的衣服。当然可以人工对商品进行颜色分类,但是在商品居多的情况下,还是得借助代码自动识别。

代码实例(文章中代码块可左右滑动)
(1)目录结构

(2)工具类: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];

    }
}

(3)调用工具类: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;background-color:$color;'>$colorName</div>";
}

(4)运行结果:





本原创文章未经允许不得转载 | 当前页面:稻田代码 » php获取图片主题颜色