• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 298  文章 - 377  trackbacks - 0
            <2007年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(34)

            隨筆分類

            隨筆檔案

            文章檔案

            相冊

            收藏夾

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            <?php
            /**
             * 
             * 相似圖片搜索hash的php實現(xiàn)
             * @author welefen
             *
             
            */
            class Imghash{
                
                private static $_instance = null;
                
                public $rate = 2;
                
                public static function getInstance(){
                    if (self::$_instance === null){
                        self::$_instance = new self();
                    }
                    return self::$_instance;
                }
                public function run($file){
                    if (!function_exists('imagecreatetruecolor')){
                        throw new Exception('must load gd lib', 1);
                    }
                    $isString = false;
                    if (is_string($file)){
                        $file = array($file);
                        $isString = true;
                    }
                    $result = array();
                    foreach ($file as $f){
                        $result[] = $this->hash($f);
                    }
                    return $isString ? $result[0] : $result;
                }
                public function checkIsSimilarImg($imgHash$otherImgHash){
                    if (file_exists($imgHash) && file_exists($otherImgHash)){
                        $imgHash = $this->run($imgHash);
                        $otherImgHash = $this->run($otherImgHash);
                    }
                    if (strlen($imgHash) !== strlen($otherImgHash)) return false;
                    $count = 0;
                    $len = strlen($imgHash);
                    for($i=0;$i<$len;$i++){
                        if ($imgHash{$i} !== $otherImgHash{$i}){
                            $count++;
                        }
                    }
                    return $count <= (5 * $this->rate * $this->rate) ? true : false;
                }
                public function hash($file){
                    if (!file_exists($file)){
                        return false;
                    }
                    $height = 8 * $this->rate;
                    $width = 8 * $this->rate;
                    $img = imagecreatetruecolor($width$height);
                    list($w$h) = getimagesize($file);
                    $source = $this->createImg($file);
                    imagecopyresampled($img$source, 0, 0, 0, 0, $width$height$w$h);
                    $value = $this->getHashValue($img);
                    imagedestroy($img);
                    return $value;
                }
                public function getHashValue($img){
                    $width = imagesx($img);
                    $height = imagesy($img);
                    $total = 0;
                    $array = array();
                    for ($y=0;$y<$height;$y++){
                        for ($x=0;$x<$width;$x++){
                            $gray = ( imagecolorat($img$x$y) >> 8 ) & 0xFF;
                            if (!@is_array($array[$y])){
                                $array[$y] = array();
                            }
                            $array[$y][$x] = $gray;
                            $total += $gray;
                        }
                    }
                    $average = intval($total / (64 * $this->rate * $this->rate));
                    $result = '';
                    for ($y=0;$y<$height;$y++){
                        for ($x=0;$x<$width;$x++){
                            if ($array[$y][$x] >= $average){
                                $result .= '1';
                            }else{
                                $result .= '0';
                            }
                        }
                    }
                    return $result;
                }
                public function createImg($file){
                    $ext = $this->getFileExt($file);
                    if ($ext === 'jpeg') $ext = 'jpg';
                    $img = null;
                    switch ($ext){
                        case 'png' : $img = imagecreatefrompng($file);break;
                        case 'jpg' : $img = imagecreatefromjpeg($file);break;
                        case 'gif' : $img = imagecreatefromgif($file);
                    }
                    return $img;
                }
                public function getFileExt($file){
                    $infos = explode('.', $file);
                    $ext = strtolower($infos[count($infos) - 1]);
                    return $ext;
                }
            }
            @import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
            posted on 2016-09-22 10:29 聶文龍 閱讀(426) 評論(1)  編輯 收藏 引用

            FeedBack:
            # re: 相似圖片搜索hash的php實現(xiàn) 2016-09-22 11:07 聶文龍
            require_once "Imghash.class.php";
            $instance = ImgHash::getInstance();
            $result = $instance->checkIsSimilarImg('chenyin/IMG_3214.png', 'chenyin/IMG_3212.JPG');
            如果$result值為true, 則表明2個圖片相似,否則不相似。  回復  更多評論
              
            日韩精品国产自在久久现线拍| 久久本道综合久久伊人| 亚洲级αV无码毛片久久精品| 国产精品久久午夜夜伦鲁鲁| 国产99久久久久久免费看| 一级A毛片免费观看久久精品| 亚洲乱码精品久久久久..| 亚洲国产精品久久久久网站| 亚洲精品无码专区久久同性男| 麻豆亚洲AV永久无码精品久久| 精品久久久久久久久久中文字幕| 狠狠色婷婷久久一区二区| 国产2021久久精品| 一本久久知道综合久久| 精品久久人人爽天天玩人人妻 | 日产久久强奸免费的看| 狠狠88综合久久久久综合网| 精品久久久久久无码人妻热| 成人资源影音先锋久久资源网| 精品国产乱码久久久久软件| 久久中文字幕视频、最近更新| a级毛片无码兔费真人久久| 精品国产VA久久久久久久冰| 久久亚洲AV无码精品色午夜麻豆| 2020最新久久久视精品爱| 精品无码久久久久国产| 久久亚洲欧美国产精品 | 久久夜色精品国产噜噜噜亚洲AV| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久久精品午夜免费不卡| 久久人人爽人人爽人人爽| 久久婷婷色综合一区二区| 久久精品中文字幕一区| 丰满少妇人妻久久久久久4| 99久久婷婷国产一区二区| 久久夜色tv网站| 精品国产婷婷久久久| 精品国产乱码久久久久久浪潮 | 久久99精品久久久久久久久久| 久久亚洲精品国产精品| 亚洲av伊人久久综合密臀性色|