<output id="r87xx"></output>
    1. 
      
      <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
        •   

               當(dāng)前位置:首頁>軟件介紹>PHP識(shí)別圖片主色調(diào) 查詢:
               
          PHP識(shí)別圖片主色調(diào)

          一、適用情景:,根據(jù)顏色列出相應(yīng)圖片。


          二、主程序及API解釋:
          [php] 
          <?php  
          class MajorColor 

          //參考顏色 
          protected $_colors = null;  
          //容差 
          protected $_tolerance = 80;  
          //忽略的顏色 
          protected $_ignoreColors = array();  
          //支持的圖片類型 
          protected $_funcs = array('image/png' => 'imagecreatefrompng', 'image/jpeg' => 'imagecreatefromjpeg', 'image/gif' => 'imagecreatefromgif');  
          public function __construct(array $colors = null) { 
          if(null !== $colors) { 
          $this->_colors = $colors; 

          }  
          public function setColors(array $colors) { 
          $this->_colors = $colors; 
          }  
          public function setTolerance($tolerance) { 
          $this->_tolerance = $tolerance; 
          }  
          public function setIgnoreColors($colors) { 
          $this->_ignoreColors = $colors; 
          }  
          public function _isValidColor($confVal, $val) { 
          if(is_array($confVal)) { 
          return $val >= $confVal[0] && $val <= $confVal[1]; 
          } else { 
          return $val >= $confVal - $this->_tolerance && $val <= $confVal + $this->_tolerance; 

          }  
          public function getOrderedColors($pic) { 
          $size = getimagesize($pic); 
          if(!$size) { 
          return false; 
          }  
          $width = $size[0]; 
          $height = $size[1]; 
          $mime = $size['mime']; 
          $func = isset($this->_funcs[$mime]) ? $this->_funcs[$mime] : null; 
          if(!$func) { 
          return false; 
          }  
          $im = $func($pic); 
          if(!$im) { 
          return false; 
          }  
          $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); 
          $alpha = $color_tran['alpha']; 
          unset($color_tran['alpha']); 
          if(100 < $alpha || in_array($color_tran, $this->_ignoreColors)) { 
          continue; 
          }  
          foreach ($this->_colors as $colorid => $color) { 
          if($this->_isValidColor($color['red'], $color_tran['red']) 
          && $this->_isValidColor($color['green'], $color_tran['green']) 
          && $this->_isValidColor($color['blue'], $color_tran['blue']) 
          ) { 
          $nums[$colorid] = isset($nums[$colorid]) ? $nums[$colorid] + 1 : 1; 



          }  
          imagedestroy($im); 
          arsort($nums); 
          return $nums; 
          }  
          public function getMajorColor($pic) { 
          $nums = $this->getOrderedColors($pic); 
          $keys = array_keys($nums); 
          return $keys[0]; 


          1.void setColors(array $colors)
          設(shè)置可選顏色,即上圖中“全部顏色”下的所有顏色(白、灰、黑...)

          2.void setTolerance(int $tolerance)
          設(shè)置容差,比如綠色的RGB值為(0,255,0),如果設(shè)置容差為40,那么-40<=R<<40 && 215<=G<=295 && -40<=B<=40范圍內(nèi)的所有顏色將被視為綠色。
          此方法用于大致區(qū)別各顏色。

          3.void setIgnoreColors(array $colors)
          設(shè)置不需考慮的顏色。如大多圖片的背景是白色,而我們顯然不希望結(jié)果是白色,此時(shí)可調(diào)用此方法簡略白色。

          4.array getOrderedColors($pic)
          根據(jù)$pic獲取各種顏色(用setColors設(shè)置的顏色)的匹配數(shù)量,按匹配量由高到低排列參數(shù)$pic是待檢測圖片的路徑

          5.mix getMajorColor($pic)
          內(nèi)部調(diào)用getOrderedColors,返回匹配量最高的顏色的key

          三、$colors的格式及范圍確定
          1.如果$colors中的各種顏色差別很明顯,我們只需簡單的傳入顏色值,內(nèi)部會(huì)根據(jù)setTolerance設(shè)置的容差來區(qū)別各顏色。
          [php] 
          $colors = array( 
          1 => array('red' => 0xff, 'green' => 0xff, 'blue' => 0xff), 
          2 => array('red' => 0xc0, 'green' => 0xc0, 'blue' => 0xc0), 
          2 => array('red' => 0x00, 'green' => 0x00, 'blue' => 0x00), 
          ); 

          2.setTolerance設(shè)置容差的方法只能大致區(qū)分各種顏色,如果需要更精確的控制,則需要分別設(shè)置某一顏色的R、G、B范圍www.2cto.com
          [php] 
          $colors = array( 
          1 => array('red' => array(189, 230), 'green' => array(189, 230), 'blue' => array(189, 230)), 
          2 => array('red' => array(0, 37), 'green' => array(0, 37), 'blue' => array(0, 37)), 
          3 => array('red' => array(128, 255), 'green' => array(0, 76), 'blue' => array(0, 100)), 
          ); 
          需要進(jìn)行一系列的微調(diào),直至能明顯區(qū)分各種顏色。


          如何配置php服務(wù)器-iis7.5配置php環(huán)境網(wǎng)站搭建配置Editplus調(diào)試PHP程序入門教程
          PHP對(duì)數(shù)組的處理將PHP作為Shell腳本語言使用
          關(guān)于php測試部署和持續(xù)集成不常見卻非常有用的PHP函數(shù)
          安全配置PHP的25個(gè)實(shí)踐事例php總結(jié)報(bào)告
          PHP+PDO+學(xué)習(xí)筆記PHP開發(fā)中常見的錯(cuò)誤
          PHP+MySQL分頁原理及實(shí)現(xiàn)PHP連接各種數(shù)據(jù)庫代碼
          php程序員學(xué)習(xí)計(jì)劃PHP實(shí)現(xiàn)排序算法
          PHP程序員的技術(shù)成長規(guī)劃php定時(shí)執(zhí)行任務(wù)的幾個(gè)方法
          信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
          • 勁爆價(jià):
            不限功能
            不限用戶
            1998元/年

          • 微信客服

            <output id="r87xx"></output>
          1. 
            
            <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
              • 亚洲日韩免费视频 | 亚洲综合五月 | 国产熟女操逼 | 欧美自拍另类在线 | 九九成人在线视频 | 国产精品无码在线看 | 黄色视频在线观看免费 | 波多野结衣红桃视频 | 翔田千里性爱视频 | 超碰中文网 |