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乱码问题
Mar 25 PHP
PHP ? EasyUI DataGrid 资料存的方式介绍
Nov 07 PHP
领悟php接口中interface存在的意义
Jun 27 PHP
浅谈php扩展imagick
Jun 02 PHP
PHP+MySQL删除操作实例
Jan 21 PHP
PHP 5.3和PHP 5.4出现FastCGI Error解决方法
Feb 12 PHP
php字符串按照单词进行反转的方法
Mar 14 PHP
总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
Sep 09 PHP
thinkPHP多语言切换设置方法详解
Nov 11 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
Apr 23 PHP
laravel-admin 实现给grid的列添加行数序号的方法
Oct 08 PHP
YII2框架中查询生成器Query()的使用方法示例
Mar 18 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
探讨如何在php168_cms中提取验证码
2013/06/08 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
PHP实现将HTML5中Canvas图像保存到服务器的方法
2014/11/28 PHP
PHP实现返回JSON和XML的类分享
2015/01/28 PHP
详谈php静态方法及普通方法的区别
2016/10/04 PHP
PHP通过文件路径获取文件名的实例代码
2018/10/14 PHP
5分钟理解JavaScript中this用法分享
2013/11/09 Javascript
提高jQuery性能的十个诀窍
2013/11/14 Javascript
jquery实现无限分级横向导航菜单的方法
2015/03/12 Javascript
浅谈javascript语法和定时函数
2015/05/03 Javascript
基于jquery实现动态竖向柱状条特效
2016/02/12 Javascript
JavaScript中点击事件的写法
2016/06/28 Javascript
js无提示关闭浏览器窗口的两种方法分析
2016/11/06 Javascript
JavaScript中清空数组的方法总结
2016/12/02 Javascript
jQuey将序列化对象在前台显示地实现代码(方法总结)
2016/12/13 Javascript
基于JQuery的Ajax方法使用详解
2017/08/16 jQuery
vue项目持久化存储数据的实现代码
2018/10/01 Javascript
JavaScript常用事件介绍
2019/01/21 Javascript
layer.open的自适应及居中及子页面标题的修改方法
2019/09/05 Javascript
Vue修改项目启动端口号方法
2019/11/07 Javascript
如何在vue中使用video.js播放m3u8格式的视频
2021/02/01 Vue.js
python使用range函数计算一组数和的方法
2015/05/07 Python
Python+微信接口实现运维报警
2016/08/27 Python
python复制文件到指定目录的实例
2018/04/27 Python
python opencv 读取本地视频文件 修改ffmpeg的方法
2019/01/26 Python
windows下python虚拟环境virtualenv安装和使用详解
2019/07/16 Python
Python读取excel文件中带公式的值的实现
2020/04/17 Python
Python接口测试环境搭建过程详解
2020/06/29 Python
Django --Xadmin 判断登录者身份实例
2020/07/03 Python
Python实现一个简单的递归下降分析器
2020/08/01 Python
基于python爬取链家二手房信息代码示例
2020/10/21 Python
Python中使用Selenium环境安装的方法步骤
2021/02/22 Python
面向对象编程OOP的优点
2013/01/22 面试题
计算机专业应届毕业生自荐信
2013/09/26 职场文书
副检察长四风问题对照检查材料思想汇报
2014/10/07 职场文书
MySQL实战记录之如何快速定位慢SQL
2022/03/23 MySQL