使用 PHP 检查图像是否唯一的好方法是什么?

发布于 2024-08-16 21:21:11 字数 249 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过潦 2024-08-23 21:21:11

使用 md5sha1

Use md5 or sha1 on image file.

硪扪都還晓 2024-08-23 21:21:11

对文件运行校验和 ..如果它与您匹配已经有了,那么它可能是完全相同的图像。

run a checksum on the file .. if it matches one you already have then its probably the same exact image.

左耳近心 2024-08-23 21:21:11

系统应该能够
过滤掉完全一致的图像
一样的。

在这种情况下,您可能会忘记您正在谈论图像,而只是将它们视为二进制文件,使用 hash_file() 创建哈希。

当然,这也会导致仅元数据不同的图像产生不同的哈希值,例如 JPEG 图像中的 EXIF 注释。您必须决定这对您来说是否有问题。

The system should just be able to
filter out images that are excactly
the same.

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.

雨后彩虹 2024-08-23 21:21:11

即使 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 :)

離殇 2024-08-23 21:21:11

快速回答,但我推荐这种方法:

  • 使用 md5sum 对图像进行哈希处理(PHP 中有一个函数可以实现此目的)。
  • 如果您使用数据库,请将 md5sum 设为图片文件表的一列,并按该字段对表进行索引。
  • 否则,请将哈希值保存在平面文件中,如下所示:

    <块引用>
    <前><代码>68b329da9893e34099c7d8ad5cb9c940 file2.bmp
    da1e100dc9e7bebb810985e37875de38 file1.jpg

Quick answer, but I recommend this approach:

  • Use md5sum to hash the images (there's a function in PHP for this).
  • If you're using a database, have the md5sum be a column of a table of picture files, and index the table by that field.
  • Otherwise, keep the hashes in a flat file like this:

    68b329da9893e34099c7d8ad5cb9c940 file2.bmp
    da1e100dc9e7bebb810985e37875de38 file1.jpg
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文