php判断两张图片是否相同

方法一:

sha1_file() 或者 md5_file()方法。

$file = "./img/1.jpg";
$file2 = "./img/2.jpg";
$gg = sha1_file($file);
$aa = sha1_file($file2);
if($aa == $gg) echo 'equation';

这两个方法只能判断两张完全相同的图片,包括文件的大小,尺寸,另外这个方法还可以用来判断文件。

方法二:

$filename = '1.jpg';
list($width, $height) = getimagesize($filename);
$img = imagecreatefromjpeg($filename);
$new_img = imagecreatetruecolor(8, 8);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, 8, 8, $width, $height);
imagefilter($new_img, IMG_FILTER_GRAYSCALE);
$colors = array();
$sum = 0;
 
for ($i = 0; $i < 8; $i++) {
    for ($j = 0; $j < 8; $j++) {
        $color = imagecolorat($new_img, $i, $j) & 0xff;
        $sum += $color;
        $colors[] = $color;
    }
}
 
$avg = $sum / 64;
$hash = '';
$curr = '';
$count = 0;
foreach ($colors as $color) {
    if ($color > $avg) {
        $curr .= '1';
    } else {
        $curr .= '0';
    }
    $count++;
    if (!($count % 4)) {
        $hash .= dechex(bindec($curr));
        $curr = '';
    }
}
print $hash . "\n";

将图片缩小,再取得其hash值。然后进行比较。

此源码来自github,地址:https://gist.github.com/mncaudill/1326966

方法三:

use ImageHash\ImageHash;
 
$file = "./img/1.jpg";
$hasher = new ImageHash;
$hash = $hasher->hash($file);
var_dump($hash);

需要下载imagehash包,github地址:https://github.com/jenssegers/imagehash

这个源码包比上面的扩展功能更强大,还可以直接
$distance = $hasher->compare('path/to/image1.jpg', 'path/to/image2.jpg'); 
这样调用,感兴趣得可以下载demo研究下。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值