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文本操作类
Nov 25 PHP
PHP加密扩展库Mcrypt安装和实例
Nov 10 PHP
php中mt_rand()随机数函数用法
Nov 24 PHP
php两种无限分类方法实例
Apr 21 PHP
php递归实现无限分类的方法
Jul 28 PHP
浅析php设计模式之数据对象映射模式
Mar 03 PHP
ThinkPHP模板循环输出Volist标签用法实例详解
Mar 23 PHP
ThinkPHP使用Ueditor的方法详解
May 20 PHP
Yii2框架dropDownList下拉菜单用法实例分析
Jul 18 PHP
PHP自定义函数格式化json数据示例
Sep 14 PHP
php实现简单加入购物车功能
Mar 07 PHP
PHP长连接实现与使用方法详解
Feb 11 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配置参数总结
2013/06/14 PHP
php调整gif动画图片尺寸示例代码分享
2013/12/05 PHP
WampServer下安装多个版本的PHP、mysql、apache图文教程
2015/01/07 PHP
PHP实现的注册,登录及查询用户资料功能API接口示例
2017/06/06 PHP
页面右下角弹出提示框示例代码js版
2013/08/02 Javascript
用javascript关闭本窗口技巧小结
2014/09/05 Javascript
javascript中parseInt()函数的定义和用法分析
2014/12/20 Javascript
js文本框走动跑马灯效果代码分享
2015/08/25 Javascript
Boostrap模态窗口的学习小结
2016/03/28 Javascript
判断是否存在子节点的实现代码
2016/05/18 Javascript
关于Javascript中defer和async的区别总结
2016/09/20 Javascript
vue2.X组件学习心得(新手必看篇)
2017/07/05 Javascript
利用canvas中toDataURL()将图片转为dataURL(base64)的方法详解
2017/11/20 Javascript
微信小程序全局变量的设置、使用、修改过程解析
2019/09/24 Javascript
Python 返回汉字的汉语拼音
2009/02/27 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
2016/06/30 Python
python图书管理系统
2020/04/05 Python
python3+requests接口自动化session操作方法
2018/10/13 Python
pycharm 配置远程解释器的方法
2018/10/28 Python
关于pandas的离散化,面元划分详解
2019/11/22 Python
浅谈Python中threading join和setDaemon用法及区别说明
2020/05/02 Python
Python 内存管理机制全面分析
2021/01/16 Python
浅谈基于HTML5的在线视频播放方案
2016/02/18 HTML / CSS
奥地利网上现代灯具和灯饰店:Lampenwelt.at
2018/01/29 全球购物
英国创新设计文具、卡片和礼品包装网站:Paperchase
2018/07/14 全球购物
广告设计专业自荐信范文
2013/11/14 职场文书
四年级科学教学反思
2014/02/10 职场文书
2014国培学习感言
2014/03/05 职场文书
拒绝黄毒毒宣传标语
2014/06/26 职场文书
感恩祖国演讲稿
2014/09/09 职场文书
师德师风个人自我剖析材料
2014/09/27 职场文书
个人创业事迹材料
2014/12/30 职场文书
婚宴新娘致辞
2015/07/28 职场文书
Django展示可视化图表的多种方式
2021/04/08 Python
nginx结合openssl实现https的方法
2021/07/25 Servers
SQL Server数据库查询出现阻塞之性能调优
2022/04/10 SQL Server