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 相关文章推荐
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Dec 06 PHP
在IIS7.0下面配置PHP 5.3.2运行环境的方法
Apr 13 PHP
php 编写安全的代码时容易犯的错误小结
May 20 PHP
php设计模式 Adapter(适配器模式)
Jun 26 PHP
PHP文件操作实现代码分享
Sep 01 PHP
PHP 中检查或过滤IP地址的实现代码
Nov 27 PHP
PHP设置一边执行一边输出结果的代码
Sep 30 PHP
PHP截取指定图片大小的方法
Dec 10 PHP
PHP测试成功的邮件发送案例
Oct 26 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
Aug 01 PHP
浅谈PHP5.6 与 PHP7.0 区别
Oct 09 PHP
Laravel 创建可以传递参数 Console服务的例子
Oct 14 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
菜鸟修复电子管记
2021/03/02 无线电
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
2007/03/15 PHP
php5数字型字符串加解密代码
2008/04/24 PHP
php 编写安全的代码时容易犯的错误小结
2010/05/20 PHP
PHP服务器页面间跳转实现方法
2012/08/02 PHP
jquery固定底网站底部菜单效果
2013/08/13 Javascript
js的for in循环和java里foreach循环的区别分析
2015/01/28 Javascript
javascript中错误使用var造成undefined
2016/03/31 Javascript
用jQuery获取table中行id和td值的实现代码
2016/05/19 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
AngularJS中$watch和$timeout的使用示例
2016/09/20 Javascript
又一款MVVM组件 构建自己的Vue组件(2)
2017/03/13 Javascript
js实现登录注册框手机号和验证码校验(前端部分)
2017/09/28 Javascript
开发Vue树形组件的示例代码
2017/12/21 Javascript
Vuex 快速入门(简单易懂)
2018/09/20 Javascript
详解JS取出两个数组中的不同或相同元素
2019/03/20 Javascript
vue学习笔记之slot插槽基本用法实例分析
2020/02/01 Javascript
Javascript执行流程细节原理解析
2020/05/14 Javascript
Vue记住滚动条和实现下拉加载的完美方法
2020/07/31 Javascript
Vue实现boradcast和dispatch的示例
2020/11/13 Javascript
[02:04]完美世界城市挑战赛秋季赛报名开始 谁是solo路人王?
2019/10/10 DOTA
Python3中简单的文件操作及两个简单小实例分享
2017/06/18 Python
解决Python 爬虫URL中存在中文或特殊符号无法请求的问题
2018/05/11 Python
使用Python处理BAM的方法
2018/09/28 Python
解决python3中cv2读取中文路径的问题
2018/12/05 Python
关于PyTorch 自动求导机制详解
2019/08/18 Python
python使用yield压平嵌套字典的超简单方法
2019/11/02 Python
解决Python中回文数和质数的问题
2019/11/24 Python
matplotlib.pyplot画图并导出保存的实例
2019/12/07 Python
谈谈python垃圾回收机制
2020/09/27 Python
pycharm激活码免费分享适用最新pycharm2020.2.3永久激活
2020/11/25 Python
基于DOM+CSS3实现OrgChart组织结构图插件
2016/03/02 HTML / CSS
伊莱克斯阿根廷网上商店:Tienda Electrolux
2021/03/08 全球购物
毕业证丢失证明
2014/01/15 职场文书
2016庆祝教师节新闻稿
2015/11/25 职场文书
python b站视频下载的五种版本
2021/05/27 Python