TL;DR:what the title says  I am developing some sort of image board in PHP. I was thinking of changing each image's filename to it's checksum prior to saving it. This way, I might be able to prevent duplicates.I know this wouldn't work for two images that are the same but differ in size or level of compression or whatnot, but this method would allow for an early check.What bugs me is that I never saw this method implemented anywhere, so I was wondering if there is a catch to it. Maybe it is just more efficient to keep the original filename and store the hash in DB? Maybe the whole method is just not useful and my question is moot?What do you think?On a side note, I don't really get how hashes are calculated so I was wondering, if my first question checks out, if it would be possible to calculate the likeness that two images are similar by comparing hashes (levenshtein or something of the sort).
Convert filenames to their checksum before saving to prevent duplicates. Is is a smart thing to do?
file structure;hashing
A hash seeded with something static (like a file name or the actual file) will tell you whether there's an exact match, but it won't help you with similarity, as hashes will diverge significantly even with minor changes. So you have two options:1. Ignore the problem......and just ensure files don't collide with each other, even if they're duplicates (Fastest).This is where hashes come in handy: seeding a hash with something relatively unique virtually ensures the ability to uniquely reference a file, even if its file name is the same or the image is similar. The value of this, especially in an image board, is for CDNs: most CDNs generally determine duplicates based off of file name (because it's fast). So if someone makes a modification to an image but uses the same file name, the CDN will ignore the new version.So, if your file name is foo.jpg, you could create a hash using something likelist(basename, extension) = split(filename, '.')hash = md5(filename . ':' . time())filename = basename . '_' . hash . extensionAnd arrive at something like foo_a23aed3a298ae.jpg. Since the base name and the extension doesn't change, it'd be trivial to generate the original file name. Plus, as the hash was seeded with the time, you should have a mostly unique hash, even when the two files are mostly the same.2. Compute the image similarity criteria once......and store the results in the image's metadata or a separate database (Slower, but higher chance of saving storage).This would go hand-in-hand with the first option.Any good image library will provide a wealth of data about an image that you could use to create a comparison formula between two images. You'd then store those data either in the image itself (accessible via metadata like EXIF or IPTC) or in a database referenced by the image's unique hash.Of course, in this scenario, you're doing searches throughout your database for matches. You might save some time by adding the important metadata to the file name:foo_200x200_300dpi_cats_a23aed3a298ae.jpgBut depending on the amount of images you have to sort through, this can be really expensive, and would likely outweigh the cost of additional storage.