資源描述:
《[php]識別圖片主色調》由會員上傳分享,免費在線閱讀,更多相關內容在教育資源-天天文庫。
1、[PHP]識別圖片主色調2012-08-0710:16:52????我來說兩句?收藏??我要投稿??[字體:小大]一、適用情景:,根據顏色列出相應圖片。二、主程序及API解釋:[php]?
2、array('image/png'=>'imagecreatefrompng','image/jpeg'=>'imagecreatefromjpeg','image/gif'=>'imagecreatefromgif');?????????publicfunction__construct(array$colors=null){????????if(null!==$colors){????????????$this->_colors=$colors;????????}????}?????????publicfunctionsetColors(array$colors)
3、{????????$this->_colors=$colors;????}?????????publicfunctionsetTolerance($tolerance){????????$this->_tolerance=$tolerance;????}?????????publicfunctionsetIgnoreColors($colors){????????$this->_ignoreColors=$colors;????}?????????publicfunction_isValidColor($confVal,$val){????????if(is_arra
4、y($confVal)){????????????return$val>=$confVal[0]&&$val<=$confVal[1];????????}else{????????????return$val>=$confVal-$this->_tolerance&&$val<=$confVal+$this->_tolerance;????????}????}?????????publicfunctiongetOrderedColors($pic){????????$size=getimagesize($pic);????????if(!$size){????????
5、????returnfalse;????????}?????????????????$width=$size[0];????????$height=$size[1];????????$mime=$size['mime'];????????$func=isset($this->_funcs[$mime])?$this->_funcs[$mime]:null;????????if(!$func){????????????returnfalse;????????}?????????????????$im=$func($pic);????????if(!$im){??????
6、??????returnfalse;????????}?????????$total=$width*$height;????????$nums=array();????????for($i=0;$i<$width;$i++){????????????for($m=0;$m<$height;$m++){????????????????$color_index=imagecolorat($im,$i,$m);????????????????$color_tran=imagecolorsforindex($im,$color_index);????????????????$
7、alpha=$color_tran['alpha'];????????????????unset($color_tran['alpha']);????????????????if(100<$alpha
8、
9、in_array($color_tran,$this->_ignoreColors)){????????????????????continue;????????????????}?????????????????foreach($this->_colorsas$colorid=>$color){????????????????????if($thi