使用 PHP 检查图像是否唯一的好方法是什么?
使用 PHP 检查图像是否唯一的最佳方法是什么?假设我有一个大约 30 张图像(大约 500*500 像素)的目录,并且有人上传了另一张图片,有什么好方法来检查上传的图像是否不在该目录中?
是否有某种方法可以创建可以轻松比较的图像哈希?然后,我可以将图像的哈希值保存在目录中,并将其与上传图像的哈希值进行比较。
计算能力并不是什么大问题,它不必能够每分钟处理超过几张图片。具有一个像素差异的图像也不会被视为不同的图像。系统应该能够过滤掉完全相同的图像。
What is the best way to check if an image is unique using PHP? Say I have a directory of about 30 images (about 500*500 pixels), and someone uploads another picture, what is a good way to check if the uploaded image is not yet in the directory?
Is there some sort of way to create hash's of images which can be easily compared? I then could save the hash's of the images in the directory and compare those to the hash of the uploaded image.
Computing power is not much of an issue, it doesn't have to be able to handle more then a few pictures per minute. Nor is the fact that images with one pixel difference will be seen as different images. The system should just be able to filter out images that are excactly the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
md5
或sha1
。Use
md5
orsha1
on image file.对文件运行校验和 ..如果它与您匹配已经有了,那么它可能是完全相同的图像。
run a checksum on the file .. if it matches one you already have then its probably the same exact image.
在这种情况下,您可能会忘记您正在谈论图像,而只是将它们视为二进制文件,使用
hash_file()
创建哈希。当然,这也会导致仅元数据不同的图像产生不同的哈希值,例如 JPEG 图像中的 EXIF 注释。您必须决定这对您来说是否有问题。
In that case you could simply forget that you're talking about images and just treat them as binary files, using
hash_file()
to create a hash.Of course, this would also result in different hashes for images that differ only in metadata such as EXIF comments in JPEG images. You'll have to decide whether that's a problem for you.
即使 ID3 标签等小细节发生更改,文件的字节比较也会失败。要比较图片内容,您必须打开图像文件并创建实际图像像素数据的哈希值。但即便如此,也可以通过以稍微不同的质量级别保存两次 JPEG 文件来撤销 - 细微的编码差异将导致像素颜色值发生变化。
因此,如果您确实希望跨格式和质量匹配图像内容,那么您将打开一个巨大的蠕虫罐:)
Byte-wise comparison of files will fail even when a small detail like a ID3 tag has changed. To compare the picture contents, you would have to open the image file and create a hash of the actual image pixel data. But even that can be undone by saving, say, a JPEG file twice with a slightly different quality level - the subtle encoding differences will create changes in the pixel colour values.
So if you are really looking to match image contents across formats and qualities, you are opening a huge can of worms :)
快速回答,但我推荐这种方法:
否则,请将哈希值保存在平面文件中,如下所示:
<块引用>
<前><代码>68b329da9893e34099c7d8ad5cb9c940 file2.bmp
da1e100dc9e7bebb810985e37875de38 file1.jpg
Quick answer, but I recommend this approach:
Otherwise, keep the hashes in a flat file like this: