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垃圾代码优化操作代码
Aug 05 PHP
php empty() 检查一个变量是否为空
Nov 10 PHP
浅析php插件 HTMLPurifier HTML解析器
Jul 01 PHP
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
May 07 PHP
php实现的Cookies操作类实例
Sep 24 PHP
PHP中应该避免使用同名变量(拆分临时变量)
Apr 03 PHP
PHP版本如何选择?应该使用哪个版本?
May 13 PHP
php类中的$this,static,final,const,self这几个关键字使用方法
Dec 14 PHP
PHP 配置后台登录以及模板引入
Jan 24 PHP
php+mysql+jquery实现简易的检索自动补全提示功能
Apr 15 PHP
PHP 文件上传限制问题
Sep 01 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 遍历XP文件夹下所有文件
2008/11/27 PHP
redirect_uri参数错误的解决方法(必看)
2017/02/16 PHP
PHP从零开始打造自己的MVC框架之路由类实现方法分析
2019/06/03 PHP
JS实现悬浮移动窗口(悬浮广告)的特效
2013/03/12 Javascript
文字溢出实现溢出的部分再放入一个新生成的div中具体代码
2013/05/17 Javascript
jquery实现table鼠标经过变色代码
2013/09/25 Javascript
html5+javascript制作简易画板附图
2014/04/25 Javascript
学习JavaScript设计模式之装饰者模式
2016/01/19 Javascript
JavaScript正则表达式匹配 div  style标签
2016/03/15 Javascript
AngularJS表单验证中级篇(3)
2016/09/28 Javascript
详解微信小程序开发之——wx.showToast(OBJECT)的使用
2017/01/18 Javascript
Vue中UI组件库之Vuex与虚拟服务器初识
2019/05/07 Javascript
JS删除String里某个字符的方法
2021/01/06 Javascript
python版简单工厂模式
2017/10/16 Python
Python将列表数据写入文件(txt, csv,excel)
2019/04/03 Python
pandas中DataFrame修改index、columns名的方法示例
2019/08/02 Python
Python使用random模块生成随机数操作实例详解
2019/09/17 Python
Django更新models数据库结构步骤
2020/04/01 Python
完美解决Django2.0中models下的ForeignKey()问题
2020/05/19 Python
Zavvi美国:英国娱乐之家
2017/03/19 全球购物
Skyscanner台湾:全球知名的旅行比价引擎
2018/07/01 全球购物
马来西亚最热门的在线时尚商店:FashionValet
2018/11/11 全球购物
个性化皮包、小袋、生活配件:Mon Purse
2019/03/26 全球购物
料理师求职信
2014/01/30 职场文书
顶撞老师检讨书
2014/02/07 职场文书
优秀学生事迹材料
2014/02/08 职场文书
支行行长竞聘演讲稿
2014/05/15 职场文书
大学应届毕业生求职信
2014/05/24 职场文书
大学英语专业求职信
2014/06/21 职场文书
525心理活动总结
2014/07/04 职场文书
2015年元旦主持词结束语
2014/12/14 职场文书
机关保密工作承诺书
2015/05/04 职场文书
2015年度绩效考核工作总结
2015/05/27 职场文书
vue使用v-model进行跨组件绑定的基本实现方法
2021/04/28 Vue.js
PostgreSQL并行计算算法及参数强制并行度设置方法
2022/04/07 PostgreSQL
vue中data里面的数据相互使用方式
2022/06/05 Vue.js