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 仿Comsenz安装效果代码打包提供下载
May 09 PHP
PHP XML数据解析代码
May 26 PHP
php中在PDO中使用事务(Transaction)
May 14 PHP
PHP投票系统防刷票判断流程分析
Feb 04 PHP
基于php split()函数的用法详解
Jun 05 PHP
ThinkPHP学习笔记(一)ThinkPHP部署
Jun 22 PHP
ThinkPHP无限级分类原理实现留言与回复功能实例
Oct 31 PHP
标准版Eclipse搭建PHP环境的详细步骤
Nov 18 PHP
WordPress中访客登陆实现邮件提醒的PHP脚本实例分享
Dec 14 PHP
浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别
Dec 15 PHP
Laravel 对某一列进行筛选然后求和sum()的例子
Oct 10 PHP
PHP判断是否是json字符串
Apr 01 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
Linux下创建nginx脚本-start、stop、reload…
2014/08/03 PHP
golang与php实现计算两个经纬度之间距离的方法
2016/07/22 PHP
PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
2016/09/11 PHP
PHP检查URL包含特定字符串实例方法
2019/02/11 PHP
Json对象替换字符串占位符实现代码
2010/11/17 Javascript
利用JQuery动画制作滑动菜单项效果实现步骤及代码
2013/02/07 Javascript
Java/JS获取flash高宽的具体方法
2013/12/27 Javascript
node.js学习总结之调式代码的方法
2014/06/25 Javascript
JavaScript中双叹号!!作用示例介绍
2014/09/21 Javascript
JavaScript中的分号插入机制详细介绍
2015/02/11 Javascript
jQuery的end()方法使用详解
2015/07/15 Javascript
用Vue.js实现监听属性的变化
2016/11/17 Javascript
简单易扩展可控性强的Jquery转盘抽奖程序
2019/03/16 jQuery
图解javascript作用域链
2019/05/27 Javascript
ionic2.0双击返回键退出应用
2019/09/17 Javascript
详解利用nodejs对本地json文件进行增删改查
2019/09/20 NodeJs
Vue 实现点击空白处隐藏某节点的三种方式(指令、普通、遮罩)
2019/10/23 Javascript
jquery.validate自定义验证用法实例分析【成功提示与择要提示】
2020/06/06 jQuery
electron踩坑之dialog中的callback解决
2020/10/06 Javascript
Python导入txt数据到mysql的方法
2015/04/08 Python
python计算一个序列的平均值的方法
2015/07/11 Python
举例讲解Python面相对象编程中对象的属性与类的方法
2016/01/19 Python
Python编程中NotImplementedError的使用方法
2018/04/21 Python
解决Pycharm出现的部分快捷键无效问题
2018/10/22 Python
Python弹出输入框并获取输入值的实例
2019/06/18 Python
Python3+Appium安装使用教程
2019/07/05 Python
HTML5之SVG 2D入门3—文本与图像及渲染文本介绍
2013/01/30 HTML / CSS
Html5页面点击遮罩层背景关闭遮罩层
2020/11/30 HTML / CSS
Lampenwelt德国:欧洲领先的灯具和照明在线商店
2018/08/05 全球购物
德国受欢迎的旅游和休闲网站:lastminute.de
2019/09/23 全球购物
美国排名第一的葡萄酒俱乐部:Firstleaf Wine Club
2020/01/02 全球购物
幼儿园教师工作感言
2014/02/15 职场文书
感恩母亲节演讲稿
2014/05/07 职场文书
安全员岗位职责
2015/02/10 职场文书
采购部年度工作总结
2015/08/13 职场文书
vue3使用vue-router的完整步骤记录
2021/06/20 Vue.js