PHP获取图片主题颜色
(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获取图片主题颜色的更多相关文章
- [Swift通天遁地]五、高级扩展-(5)获取互补色、渐变色、以及图片主题颜色
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- php 获取图片主要颜色的方法
本文章向码农们介绍php 获取图片主要颜色的方法,主要涉及php针对图片的相关操作技巧,需要的码农可以参考一下. $i = imagecreatefromjpeg("image.jpg&qu ...
- 获取当前主题颜色 Flutter
通过context获取当前主题颜色 Theme.of(context).accentColor
- uwp 用win2d获取图片主调颜色
win10在设置颜色里有个从“背景图片中选取一种主题颜色”的选项,还有在很多内容展示软件中都使用了这样的功能. 现在我们需要在 nuget 引用 win2d.uwp 和 Toolkit.uwp 两个库 ...
- 小技巧!CSS 提取图片主题色功能探索
本文将介绍一种利用 CSS 获取图片主题色的小技巧.一起看看~ 背景 起因是微信技术群里有个同学发问,有什么方法能够获取图片的主色呢?有一张图片,获取他的主色调: 利用获取到的这个颜色值,来实现类似这 ...
- php 提取图片主要颜色
PHP实现获取图片颜色值的方法 PHP获取图片颜色值检测图片主要颜色是通过imagecreatefromjpeg函数读取图片,再循环获得各个颜色值加以计算实现的. /** * 获取图片主要颜色 * @ ...
- iOS 开发之提取图片的主色调用于更换应用主题颜色
从刷爆 IT 圈的一个事件说起: 新闻:某互联网公司产品经理提出一个需求--要求APP开发人员做到软件根据用户的手机壳改变软件的主题颜色. What Fuck!还有这操作,PM,你过来,保证不打屎你. ...
- cocos2d-x 获取图片的某像素点的RGBA颜色 -转
cocos2d-x 获取图片的某像素点的RGBA颜色 原文:http://www.cnblogs.com/jaoye/archive/2013/02/19/2916501.html 没做过 太多的图 ...
- Android5.0新特性——图片和颜色(drawable)
图片和颜色 tint属性 tint属性一个颜色值,可以对图片做颜色渲染,我们可以给view的背景设置tint色值,给ImageView的图片设置tint色值,也可以给任意Drawable或者NineP ...
随机推荐
- 阿里云ubuntu16.04安装beef
0x0 前言 环境:阿里云轻量服务器ubuntu16.04 需要安装2.4以上版本的ruby:https://www.cnblogs.com/Rain99-/p/10666247.html 参考资料 ...
- python FTP服务器实现(Python3)
创建一个ftp.py文件(Linux环境),插入以下代码: from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handl ...
- UITabBarController的使用
UITabBarController的使用 前言: 苹果开发的小伙伴都知道,项目中只要用到了UITabBarController,UITabBarController就是APP的骨架.所以熟练掌握UI ...
- Linux 做网关
首先创建两张路由表,只需要添加到相应的文件中即可,Linux一共支持255个路由表,rt_tables文件中默认已经存在了三张路由表,分别是: 255 local 254 main 2 ...
- windows 无法链接 \\ , 拼写错误或者网络有问题,解决方法
1. 楼主首先在网上搜索了一遍问题, 比较全面的回答链接如下http://blog.csdn.net/newizan/article/details/50313137 然而并没有解决问题, 于是反思了 ...
- Django_WSGIRequest对象
WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.这个参数就是dja ...
- Vue 列表渲染及条件渲染实战
条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue 帮我们提供了一个v-if的指 ...
- 部署mysql版本项目问题记录
一,com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure报错 将url从jdbc:mysq ...
- teamwork 2
1.访问上学期项目团队,学习他们的得失. 上学期学长们有一个项目是学霸系统,在看过了学长们的相关博客后,我们可以感受到学长们确实花费了不少心思,也看到了许多值得我们学习的地方. 首先,学长们在项目开始 ...
- unique STL讲解和模板
unique()是C++标准库函数里面的函数,其功能是去除相邻的重复元素(只保留一个),所以使用前需要对数组进行排序. 代码: #include<bits/stdc++.h> using ...