php獲取圖片主題顏色_第1頁
php獲取圖片主題顏色_第2頁
php獲取圖片主題顏色_第3頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、hp獲取圖片主題顏色文章簡介文章簡介本文提供一個獲取圖片主題顏色的 php 工具類。使用場景比如做一個服裝 app 或者網(wǎng)站,需要根據(jù)顏色對商品進(jìn)行分類。用戶點(diǎn)擊紅色分類按鈕, 出現(xiàn)紅色的衣服, 點(diǎn)擊綠色分類按鈕展示綠色的衣服。當(dāng)然可以人工對商品進(jìn)行顏色分類, 但是在商品居多的情況下, 還是得借助代碼自動識別。代碼實(shí)例( 文章中代碼塊可左右滑動)(1) 目錄結(jié)構(gòu)(2) 工具類: pictureColor.php(2) 工具類: pictureColor.phpclass class pictureColor/*/* 獲取顏色使用庫類型*/public public $type = gd;/*/

2、* 十六進(jìn)制*/public public $hex = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F);/*/* 獲得圖片色系* param string* param string $file* return string* return string*/*/public functionpublic function colorName($file)if if (empty($file) return falsereturn false;$rgb =$rgb = $this-getRGB($file);$hsl = $this-

3、RGB2HSL($rgb);returnreturn $this-getColorName($hsl);/*/* 取得圖片十六進(jìn)制* param string* param string $file* return string* return string*/*/public function hexName($file)try try if if (empty($file) return falsereturn false;for ($y = 0; $y $h; $y+) for ($y = 0; $y getRGB($file, $this-type);$color =$color =

4、$this-RGB2Hex($rgb);if if (strlen($color) 6) $color = substr($color, 1, 6);return return $color; catch (Exception $e) echo echo $e;/*/* RGB* param string* param string $file* param string $type* param string $type gd/gm* return array* return array*/*/public functionpublic function getRGB($file)if if

5、 (empty($file) return falsereturn false;$imageSize =$imageSize = getimagesize($file);if if ($imageSizemime = image/jpeg) $img= imagecreatefromjpeg($file); elseif($imageSizemime = image/png)$img= imagecreatefrompng($file); elseif($imageSizemime = image/gif)$img= imagecreatefromgif($file);$w=imagesx($

6、img);$h=imagesy($img);$r=$g = $b = 0;for for ($x = 0; $x $r += $rgb 16;$g += $rgb $g += $rgb 8 & 255;$b += $rgb & 255;$pxls = $w * $h;$pxls = $w * $h;$r = (round($r /$r = (round($r / $pxls);list($r, $g, $b) = $rgb;$delta = $max - $min;list($r, $g, $b) = $rgb;$delta = $max - $min;$g = (round($g /$pxl

7、s);$b = (round($b /$pxls);return array(0= $r, 1 = $g, 2=$b);public functionpublic function RGB2Hex($rgb)$hexColor = $hexColor = ;$hex = $hex = $this-hex;for for ($i = 0; $i 16) $r = $c % $r = $c % 16;$c = ($c / $c = ($c / 16) 0;array_push($hexAr, $hexarray_push($hexAr, $hex $r);array_push($hexAr, $h

8、exarray_push($hexAr, $hex $c);$ret =$ret = array_reverse($hexAr);$item = $item = $ret);$item = str_pad($item, $item = str_pad($item, 2, 0, STR_PAD_LEFT);$hexColor .= $item;$hexColor .= $item;return return $hexColor;/*/* RGB轉(zhuǎn) HSL*param arrayparam array $rgbreturn array*/*/public functionpublic functi

9、on RGB2HSL($rgb)$r/=255;$g/=255;$b/=255;$max =max($r, $g,$b);$min =min($r, $g,$b);$l = ($max + $min) / $l = ($max + $min) / 2;if if ($delta = 0) $h = $h = 0;$s = $s = 0; else $s = ($l $s = ($l 0.5) ? $delta / ($max + $min) : $delta / - $max - $min);ax - $min);$deltar=($max- $r)/6)+($max/2)/$delta;$d

10、eltag=($max- $g)/6)+($max/2)/$delta;$deltab=($max- $b)/6)+($max/2)/$delta;if if ($r = $max) $h = $deltab -$h = $deltab - $deltag; else if ($g = $max) $h = $h = / 3) + $deltar - $deltab; else if ($b = $max) $h = $h = / 3) + $deltag - $deltar;$h += ($h $h += ($h 1 ? -1 : 0);return arrayreturn array($h

11、 * 360, $s * 100, $l * 100);/*/* HSL對應(yīng)顏色名稱* param array* param array $hsl* return string* return string*/*/public functionpublic function getColorName($hsl)$colorarr = $colorarr = array(0, 100, 50 0, 100, 50 = 紅色,30, 100, 50 30, 100, 50 = 橙色,60, 100, 50 60, 100, 50 = 黃色,120, 100, 75 120, 100, 75 = 綠

12、色,240,100,25 = 藍(lán)色,300,100,25 = 紫色,255,152,191 = 粉紅,/136, 84, 24 = 棕色,0,0,50 = 灰色,0,0,0 = 黑色,0,0,100 = 白色,);$distarr = $distarr = array();foreach foreach ($colorarr as $key = $val) listlist($h, $s, $l) = $key);$distarr$key =pow($hsl0- $h),2)+pow($hsl1- $s), 2) + pow($hsl2 -$l), 2);asort($distarr);asort($distarr);listlist($key) = each($distarr);return return $colorarr$key;(3) 調(diào)用工具類: index.php/(3) 調(diào)用工具類: index.php/引入工具類include_once ./pictureColor.php;$pictureColor = new pictureColor();$picUrls =$picUrls = array(.picsa.jpg,.picsb.jpg,.picsc.jpg);foreach foreach ($picUrls as $k

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論