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 相关文章推荐
下载文件的点击数回填
Oct 09 PHP
PHP 开发环境配置(测试开发环境)
Apr 28 PHP
PHP 实现类似js中alert() 提示框
Mar 18 PHP
利用PHP将部分内容用星号替换
Apr 21 PHP
WordPress中缩略图的使用以及相关技巧
Nov 24 PHP
Zend Framework动作助手Url用法详解
Mar 05 PHP
PHP实现的数独求解问题示例
Apr 18 PHP
详解thinkphp5+swoole实现异步邮件群发(SMTP方式)
Oct 13 PHP
PHP 命名空间和自动加载原理与用法实例分析
Apr 29 PHP
PHP number_format函数原理及实例解析
Jul 14 PHP
利用PHP计算有多少小于当前数字的数字方法示例
Aug 26 PHP
PHP网站常见安全漏洞,及相应防范措施总结
Mar 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
如何在PHP中使用Oracle数据库(2)
2006/10/09 PHP
网络资源
2006/10/09 PHP
PHP 透明水印生成代码
2012/08/27 PHP
解析获取优酷视频真实下载地址的PHP源代码
2013/06/26 PHP
解密ThinkPHP3.1.2版本之模板继承
2014/06/19 PHP
利用PHP绘图函数实现简单验证码功能的方法
2016/10/18 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
Git命令之分支详解
2021/03/02 PHP
从阿里妈妈发现的几个不错的表单验证函数
2007/09/21 Javascript
jquery ajax post提交数据乱码
2013/11/05 Javascript
上传图片预览JS脚本 Input file图片预览的实现示例
2014/10/23 Javascript
js中日期的加减法
2015/05/06 Javascript
jQuery实现商品活动倒计时
2015/10/16 Javascript
JS实现物体带缓冲的间歇运动效果示例
2016/12/22 Javascript
JavaScript转换数据库DateTime字段类型方法
2017/06/27 Javascript
Vue源码学习之初始化模块init.js解析
2017/11/02 Javascript
Vue 换肤的示例实践
2018/01/23 Javascript
React如何避免重渲染
2018/04/10 Javascript
微信小程序rich-text富文本用法实例分析
2019/05/20 Javascript
Layui给switch添加响应事件的例子
2019/09/03 Javascript
使用webpack/gulp构建TypeScript项目的方法示例
2019/12/18 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
python验证码识别的示例代码
2017/09/21 Python
pandas 缺失值与空值处理的实现方法
2019/10/12 Python
python模拟实现斗地主发牌
2020/01/07 Python
adidas旗下高尔夫装备供应商:TaylorMade Golf(泰勒梅高尔夫)
2016/08/28 全球购物
波兰家居和花园家具专家:4Home
2019/05/26 全球购物
工程部主管岗位职责
2013/11/17 职场文书
《我的信念》教学反思
2014/02/15 职场文书
入党自我鉴定
2014/03/25 职场文书
社团活动总结范文
2014/04/26 职场文书
开幕式邀请函
2015/01/31 职场文书
立项申请报告范本
2015/05/15 职场文书
教师年度考核自我评鉴
2015/08/11 职场文书
新学期开学寄语2016
2015/12/04 职场文书
TypeScript 内置高级类型编程示例
2022/09/23 Javascript