通过比较 BASE64 检查是否已经上传了相同的图像?

Check if same image has already been uploaded by comparing BASE64?

我的问题是关于我的一个想法,我可以通过比较他们的 base64 编码字符串来检查图像是否已经上传...

示例用例是在您的数据库中查找重复项...

我想这个操作会相当大 - 首先将图像转换为 base64,然后使用 "strcmp()" 之类的东西进行比较..

不确定这是否有意义,但您如何看待这个想法?

手术会不会太大了?它有多准确?这个想法有意义吗?

如果我做这样的事情,我会使用 md5 哈希而不是 base64_encode。

$equal = ( md5($image1) == md5($image2)) ? true : false;

这里有一个功能可以帮助您更快地比较文件。

除了检查文件大小等显而易见的事情外,您还可以尝试比较二进制块。
例如,检查最后 n 个字节以及随机偏移量的块。

我使用校验和比较作为最后的手段。

在优化检查顺序时,您还可以考虑是否通常希望文件不同。

function areEqual($firstPath, $secondPath, $chunkSize = 500){

    // First check if file are not the same size as the fastest method
    if(filesize($firstPath) !== filesize($secondPath)){
        return false;
    }

    // Compare the first ${chunkSize} bytes
    // This is fast and binary files will most likely be different 
    $fp1 = fopen($firstPath, 'r');
    $fp2 = fopen($secondPath, 'r');
    $chunksAreEqual = fread($fp1, $chunkSize) == fread($fp2, $chunkSize);
    fclose($fp1);
    fclose($fp2);

    if(!$chunksAreEqual){
        return false;
    }

    // Compare hashes
    // SHA1 calculates a bit faster than MD5
    $firstChecksum = sha1_file($firstPath);
    $secondChecksum = sha1_file($secondPath);
    if($firstChecksum != $secondChecksum){
        return false;
    }

    return true;
}