• <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
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(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 聶文龍 閱讀(419) 評論(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個圖片相似,否則不相似。  回復(fù)  更多評論
              

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            狠狠色狠狠色综合久久| 久久精品无码一区二区app| 一日本道伊人久久综合影| 久久无码AV中文出轨人妻| 77777亚洲午夜久久多喷| 久久免费的精品国产V∧| 国内精品久久久久久久久| 免费精品久久天干天干| 国产精品岛国久久久久| 久久婷婷五月综合色奶水99啪| 久久久久女人精品毛片| 亚洲国产成人久久笫一页| 国产精品无码久久综合 | 精品久久久久久无码中文字幕一区 | 久久精品无码一区二区WWW| 久久99精品国产99久久6男男| 青青草国产97免久久费观看| 精品蜜臀久久久久99网站| 久久人人爽人人人人爽AV| 99久久www免费人成精品| 99久久久精品免费观看国产| 亚洲综合久久久| 亚洲精品乱码久久久久久蜜桃 | 久久久久久狠狠丁香| 久久婷婷五月综合97色一本一本 | 一级女性全黄久久生活片免费 | 久久热这里只有精品在线观看| 国产ww久久久久久久久久| 2020久久精品国产免费| 久久午夜羞羞影院免费观看| 狠狠综合久久综合88亚洲| 一本色道久久综合狠狠躁篇| 国产精品久久久久一区二区三区| 2021精品国产综合久久| 久久青青草原精品影院| 91精品国产综合久久婷婷| av午夜福利一片免费看久久| 国内精品久久人妻互换| 精品久久久久久成人AV| 久久福利青草精品资源站| 91精品国产色综久久|