PHP实现搜索相似图片


Posted in PHP onSeptember 22, 2015

感知哈希算法

count < =5 匹配最相似
count > 10 两张不同的图片
var_dump(ImageHash::run(‘./1.png', ‘./psb.jpg'));

<?php
class ImageHash {
  const FILE_NOT_FOUND = '-1';
  const FILE_EXTNAME_ILLEGAL = '-2';
  private function __construct() {}
  public static function run($src1, $src2) {
    static $self;
    if(!$self) $self = new static;
    if(!is_file($src1) || !is_file($src2)) exit(self::FILE_NOT_FOUND);
    $hash1 = $self->getHashValue($src1);
    $hash2 = $self->getHashValue($src2);
    if(strlen($hash1) !== strlen($hash2)) return false;
    $count = 0;
    $len = strlen($hash1);
    for($i = 0; $i < $len; $i++) if($hash1[$i] !== $hash2[$i]) $count++;
    return $count <= 10 ? true : false;
  }
  public function getImage($file) {
    $extname = pathinfo($file, PATHINFO_EXTENSION);
    if(!in_array($extname, ['jpg','jpeg','png','gif'])) exit(self::FILE_EXTNAME_ILLEGAL);
    $img = call_user_func('imagecreatefrom'. ( $extname == 'jpg' ? 'jpeg' : $extname ) , $file);
    return $img;
  }
  public function getHashValue($file) {
    $w = 8;
    $h = 8;
    $img = imagecreatetruecolor($w, $h);
    list($src_w, $src_h) = getimagesize($file);
    $src = $this->getImage($file);
    imagecopyresampled($img, $src, 0, 0, 0, 0, $w, $h, $src_w, $src_h);
    imagedestroy($src);
    $total = 0;
    $array = array();
    for( $y = 0; $y < $h; $y++) {
      for ($x = 0; $x < $w; $x++) {
        $gray = (imagecolorat($img, $x, $y) >> 8) & 0xFF;
        if(!isset($array[$y])) $array[$y] = array();
        $array[$y][$x] = $gray;
        $total += $gray;
      }
    }
    imagedestroy($img);
    $average = intval($total / ($w * $h * 2));
    $hash = '';
    for($y = 0; $y < $h; $y++) {
      for($x = 0; $x < $w; $x++) {
        $hash .= ($array[$y][$x] >= $average) ? '1' : '0';
      }
    }
    var_dump($hash);
    return $hash;
  }
}
var_dump(ImageHash::run('./1.png', './psb.jpg'));

方法二:

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 * $rate * $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;
 }
}

调用方式如下:

require_once "Imghash.class.php";
$instance = ImgHash::getInstance();
$result = $instance->checkIsSimilarImg('chenyin/IMG_3214.png', 'chenyin/IMG_3212.JPG');

如果$result值为true, 则表明2个图片相似,否则不相似。

PHP 相关文章推荐
PHP新手上路(十一)
Oct 09 PHP
php学习 字符串课件
Jun 15 PHP
php IP转换整形(ip2long)的详解
Jun 06 PHP
php ckeditor上传图片文件名乱码解决方法
Nov 15 PHP
两种设置php载入页面时编码的方法
Jul 29 PHP
php实现的Captcha验证码类实例
Sep 22 PHP
PHP实现的下载远程图片自定义函数分享
Jan 28 PHP
PHP中的闭包(匿名函数)浅析
Feb 07 PHP
php异常处理方法实例汇总
Jun 24 PHP
修改WordPress中文章编辑器的样式的方法详解
Dec 15 PHP
PHP文件操作简单介绍及函数汇总
Dec 11 PHP
如何用PHP实现多线程编程
May 26 PHP
从刷票了解获得客户端IP的方法
Sep 21 #PHP
fsockopen pfsockopen函数被禁用,SMTP发送邮件不正常的解决方法
Sep 20 #PHP
分享ThinkPHP3.2中关联查询解决思路
Sep 20 #PHP
使用PHPCMS搭建wap手机网站
Sep 20 #PHP
求帮忙修改个php curl模拟post请求内容后并下载文件的解决思路
Sep 20 #PHP
PHP执行SQL文件并将SQL文件导入到数据库
Sep 17 #PHP
如何使用PHP对网站验证码进行破解
Sep 17 #PHP
You might like
php之XML转数组函数的详解
2013/06/07 PHP
PHP学习笔记(一):基本语法之标记、空白、和注释
2015/04/17 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
浅谈关于PHP解决图片无损压缩的问题
2017/09/01 PHP
PHP使用glob方法遍历文件夹下所有文件的实例
2018/10/17 PHP
doctype后如何获得body.clientHeight的方法
2007/07/11 Javascript
等待指定时间后自动跳转或关闭当前页面的js代码
2013/07/09 Javascript
js 获取、清空input type=&quot;file&quot;的值示例代码
2014/02/19 Javascript
jQuery实现瀑布流布局
2014/12/12 Javascript
JavaScript中Cookie操作实例
2015/01/09 Javascript
jQuery Ajax中的事件详细介绍
2015/04/16 Javascript
AngularJS 入门教程之HTML DOM实例详解
2016/07/28 Javascript
bootstrap下拉列表与输入框组结合的样式调整
2016/10/08 Javascript
JavaScript利用正则表达式替换字符串中的内容
2016/12/12 Javascript
AngularJS实现自定义指令及指令配置项的方法
2017/11/20 Javascript
js实现复制功能(多种方法集合)
2018/01/06 Javascript
Vue多种方法实现表头和首列固定的示例代码
2018/02/02 Javascript
JS前端知识点总结之页面加载事件,数组操作,DOM节点操作,循环和分支
2019/07/04 Javascript
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
python中sys.argv参数用法实例分析
2015/05/20 Python
Django中日期处理注意事项与自定义时间格式转换详解
2018/08/06 Python
python如何实现异步调用函数执行
2019/07/08 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
pytorch中获取模型input/output shape实例
2019/12/30 Python
安装多个版本的TensorFlow的方法步骤
2020/04/21 Python
Python join()函数原理及使用方法
2020/11/14 Python
解决selenium+Headless Chrome实现不弹出浏览器自动化登录的问题
2021/01/09 Python
深入解析HTML5的IndexedDB索引数据库
2015/09/14 HTML / CSS
怎样在程序里获得一个空指针
2015/01/24 面试题
会计系个人求职信范文分享
2013/12/20 职场文书
小学教师师德反思
2014/02/03 职场文书
农村产权制度改革实施方案
2014/03/21 职场文书
英语教育专业自荐信
2014/05/29 职场文书
企业贷款委托书格式
2014/09/12 职场文书
2015年中秋晚会主持稿
2015/07/30 职场文书
彻底弄懂Python中的回调函数(callback)
2022/06/25 Python