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的相似度计算函数:levenshtein的使用介绍
Apr 15 PHP
php实现的一个很好用HTML解析器类可用于采集数据
Sep 23 PHP
php计算程序运行时间的简单例子分享
May 10 PHP
Laravel 5 框架入门(三)
Apr 09 PHP
PHP中常见的缓存技术实例分析
Sep 23 PHP
php操纵mysqli数据库的实现方法
Sep 18 PHP
PHP微信公众号开发之微信红包实现方法分析
Jul 14 PHP
php实现的mongoDB单例模式操作类
Jan 20 PHP
laradock环境docker-compose操作详解
Jul 29 PHP
通过PHP实现获取访问用户IP
May 09 PHP
PHP强制转化的形式整理
May 22 PHP
PHP实现简单注册登录系统
Dec 28 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
PHILIPS D1835/D1875的电路分析与打理
2021/03/02 无线电
采用PHP函数memory_get_usage获取PHP内存清耗量的方法
2011/12/06 PHP
学习php中的正则表达式
2014/08/17 PHP
PHP里8个鲜为人知的安全函数分析
2014/12/09 PHP
thinkPHP5框架auth权限控制类与用法示例
2018/06/12 PHP
JS 拼图游戏 面向对象,注释完整。
2009/06/18 Javascript
JavaScript 垃圾回收机制分析
2013/10/10 Javascript
JS 数字转换研究总结
2013/12/26 Javascript
JS控制弹出新页面窗口位置和大小的方法
2015/03/02 Javascript
js实现图片放大和拖拽特效代码分享
2015/09/05 Javascript
基于BootStrap Metronic开发框架经验小结【三】下拉列表Select2插件的使用
2016/05/12 Javascript
jQuery EasyUI菜单与按钮详解
2016/07/13 Javascript
Bootstrap基本样式学习笔记之标签(5)
2016/12/07 Javascript
JavaScript对象封装的简单实现方法(3种方法)
2017/01/03 Javascript
JS实现页面打印(整体、局部)
2017/08/18 Javascript
vue系列之动态路由详解【原创】
2017/09/10 Javascript
JS继承与闭包及JS实现继承的三种方式
2017/10/15 Javascript
浅谈Angular文字折叠展开组件的原理分析
2017/11/24 Javascript
深入理解JavaScript 中的执行上下文和执行栈
2018/10/23 Javascript
微信公众号平台接口开发 获取access_token过程解析
2019/08/14 Javascript
vue数据响应式原理知识点总结
2020/02/16 Javascript
微信小程序利用button控制条件标签的变量问题
2020/03/15 Javascript
python实现决策树分类(2)
2018/08/30 Python
python数据挖掘需要学的内容
2019/06/23 Python
Python 简单计算要求形状面积的实例
2020/01/18 Python
Python局部变量与全局变量区别原理解析
2020/07/14 Python
德国运动营养和健身网上商店:Myprotein.de
2018/07/18 全球购物
Marlies Dekkers内衣荷兰官方网店:荷兰奢侈内衣品牌
2020/03/27 全球购物
2014国培学习感言
2014/03/05 职场文书
补充协议书范本
2014/04/23 职场文书
教师自我剖析材料
2014/09/29 职场文书
股东大会通知
2015/04/24 职场文书
观看禁毒宣传片后的感想
2015/08/11 职场文书
高考满分作文赏析(2篇)
2019/08/12 职场文书
简单总结SpringMVC拦截器的使用方法
2021/06/28 Java/Android
微信小程序中wxs文件的一些妙用分享
2022/02/18 Javascript