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 ...
随机推荐
- 铁轨(rails, ACM/ICPC CERC 1997,Uva 514)
铁轨(rails, ACM/ICPC CERC 1997,Uva 514) 题目描述 某城市有一个火车站,铁轨铺设如图所示.有n节车厢从A方向驶入车站,按进站顺序编号为1~n.你的任务是让它们按照某种 ...
- C# Js 时间格式化问题
C# 后台: .ToString("dd-MMM-yyyy", System.Globalization. DateTimeFormatInfo.InvariantInfo) eg ...
- java小学生四则运算带面板版 但我不知道为什么同类变量却进不了动作监听中去
---恢复内容开始--- package yun; import java.util.*; import java.awt.*; import java.awt.event.ActionEvent; ...
- NodeJs实现客户端登陆
nodejs的api中有一个process进程对象,process 对象是一个 global (全局变量),提供有关信息,控制当前 Node.js 进程.作为一个对象,它对于 Node.js 应用程序 ...
- vue 实战 遇到问题记录
vue-router 配置路由遇到问题 1.一个 new Router({ routes:[ { path:'/', component:Good ///不要写成components 否则报 ...
- ResourceBundle类读取properties文件
1.Properties与ResourceBundle类都可以读取属性文件key/value的键值对 2.ResourceBundle类主要用来解决国际化和本地化问题,国际化时properties文件 ...
- 作死实验,删除libc.so.6
参考https://www.cnblogs.com/fjping0606/p/4551475.html https://www.cnblogs.com/weijing24/p/5890031.html ...
- Android中线程间通信原理分析:Looper,MessageQueue,Handler
自问自答的两个问题 在我们去讨论Handler,Looper,MessageQueue的关系之前,我们需要先问两个问题: 1.这一套东西搞出来是为了解决什么问题呢? 2.如果让我们来解决这个问题该怎么 ...
- php手册 | python手册 | perl手册 | c#.net手册 | c++手册 | ruby手册 | jquery手册 | js手册 | prototype手册 | mysql手册 | smarty手册 | css手册 | html手册 | nginx手册 | apache手册 | shell手册 | svn手册
收集各种实用类手册: http://shouce.jb51.net/shell/
- 我的虚拟机中的 centOS 连不了网了
网上的办法试过了,查看虚拟机的网络配置,是 NET的, 也 cd 到/etc/sysconfig/network-script/ifcfg-eth0 里面看了,onboot 本来就是 yes,要不然我 ...