```
$pictureColor = new pictureColor();
$picUrls = array('./1.png','./2.png','./4.png','./3.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>";
}
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];
}
}
```
- 課程介紹
- thinkphp5.0
- 安裝
- 開發規范
- 目錄結構
- 配置參數
- 系統常量
- tp5自帶的函數
- 助手函數
- 擴展類庫
- 基本類庫
- Workerman
- think-queue
- 驗證碼
- 圖片
- 權限認證
- 課前準備
- 數據庫設計
- 模塊設計
- 管理員管理
- 添加
- 編輯
- 刪除和批量刪除
- 列表頁
- 實列
- 權限管理
- 操作日志
- 基于行為的日記錄
- 行為日志的擴展
- 助手類庫
- 自建函數
- 將數組轉成uri字符串
- 獲取當前服務器的IP
- curl-post
- 截取文字中間的字符串
- 檢查中文姓名
- 省市區分別截取
- 抽獎概率問題
- 短信郵箱模板替換
- 生成csv
- PHP 圖片轉base64
- 銀行卡驗證
- json返回接口封裝
- 無限極分類
- 病毒
- xml和數組互轉
- xml轉成數組
- 數組轉xml
- tp控制器相關
- 獲取thinkph5下控制器和方法名
- 后臺查詢的簡單封裝
- 網址信息
- 獲取網站logo
- 判斷url是否存在
- 獲取title
- 判斷遠程文件是否存在
- 獲取頁面所有鏈接
- 過濾
- 截取
- 時間
- 獲取服務器信息
- 根據id生成唯一邀請碼
- 隨機顏色
- 數組字符串互換
- 創建多級目錄
- 懶人查詢
- 時間和時間戳轉換
- 房間id生菜
- 獲取需要的數組元素
- 文件和文件夾
- 文件類庫
- 文件夾
- 七牛云
- 七牛云運用場景
- 七牛云使用實例
- 郵箱
- 郵箱驗證
- 郵箱發送
- 數據庫
- 數據庫在thinkphp中的補充方法
- 備份和安全
- sql執行
- 數據庫備份2
- 時間日歷
- 時間格式化
- 日歷
- 圖片相關
- 自動獲取圖片主題顏色
- 獲取html中的圖片路徑
- 獲取圖片場景
- 獲取圖片實踐
- 圖片處理類
- 圖片處理場景
- 圖片處理實踐
- 數據驗證分析
- 身份證相關
- 新聞
- 自建類庫
- 簡易分類庫
- php 壓縮CSS代碼
- 身份證
- 分詞和抽詞
- 分詞應用場景
- 分詞實踐
- 中文轉拼音
- 中文轉拼音場景
- 中文轉拼音實踐
- 二維碼操作
- 二維碼場景
- 二維碼實踐
- 短地址
- PHPWord
- 插件化
- 插件擴展庫
- 插件列表
- 插件安裝和卸載
- 插件實踐
- 插件的離線安裝
- 計劃任務
- 計劃任務安裝
- 計劃任務實踐
- 定時器
- 注冊登錄
- 普通登錄注冊
- 第三方登錄注冊
- jwt接口登錄注冊
- 短信
- 飛鴿短信
- 阿里短信
- 消息隊列
- 網站地圖
- 全站靜態化
- 緩存
- 文件導出
- PDF生成
- phpword
- PHPExcel
- 其他類庫
- 百度
- 百度語音
- 快遞
- 跨域問題
- 寶塔
- 搜索記錄