php获取图片主题颜色_第1页
php获取图片主题颜色_第2页
php获取图片主题颜色_第3页
免费预览已结束,剩余4页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、hp获取图片主题颜色文章简介文章简介本文提供一个获取图片主题颜色的 php 工具类。使用场景比如做一个服装 app 或者网站,需要根据颜色对商品进行分类。用户点击红色分类按钮, 出现红色的衣服, 点击绿色分类按钮展示绿色的衣服。当然可以人工对商品进行颜色分类, 但是在商品居多的情况下, 还是得借助代码自动识别。代码实例( 文章中代码块可左右滑动)(1) 目录结构(2) 工具类: pictureColor.php(2) 工具类: pictureColor.phpclass class pictureColor/*/* 获取颜色使用库类型*/public public $type = gd;/*/

2、* 十六进制*/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);/*/* 取得图片十六进制* 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转 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对应颜色名称* 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 = 蓝色,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) 调用工具类: index.php/(3) 调用工具类: 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论