• <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
            <2013年1月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(34)

            隨筆分類

            隨筆檔案

            文章檔案

            相冊

            收藏夾

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            <?php
            /**
             * 
             * 相似圖片搜索hash的php實現
             * @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實現 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個圖片相似,否則不相似。  回復  更多評論
              
            免费精品久久久久久中文字幕 | 久久久久人妻精品一区二区三区| 色综合合久久天天给综看| 久久精品视屏| 2021最新久久久视精品爱| 亚洲精品午夜国产VA久久成人| 久久水蜜桃亚洲av无码精品麻豆| 久久成人国产精品二三区| 亚洲v国产v天堂a无码久久| 亚洲精品无码久久一线| 久久福利片| 国产精品视频久久久| 久久亚洲中文字幕精品一区四 | 99久久国产亚洲高清观看2024| 久久久久久A亚洲欧洲AV冫| 久久综合久久自在自线精品自| 久久91这里精品国产2020| 久久精品国产亚洲AV无码偷窥| 久久人妻少妇嫩草AV蜜桃| 久久精品亚洲一区二区三区浴池 | 久久天天躁狠狠躁夜夜av浪潮| 亚洲精品无码专区久久久| 久久天天躁狠狠躁夜夜不卡 | 少妇久久久久久被弄高潮| 精品欧美一区二区三区久久久| 久久久久亚洲AV无码麻豆| 久久无码中文字幕东京热| 久久综合久久鬼色| 91精品国产91久久久久久蜜臀| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 国内精品人妻无码久久久影院导航| 精品久久久久久久中文字幕| 久久精品国产99久久久| 午夜精品久久久久久毛片| 香蕉99久久国产综合精品宅男自 | 亚洲国产精品久久久久| 无码人妻久久一区二区三区免费丨 | 国产精品美女久久久久网| 久久精品国产亚洲av日韩| 久久ZYZ资源站无码中文动漫| 亚洲中文字幕无码久久2017|