0

I'm building an app that's kind of a canvas-paint art generator. The details aren't important - what is important is ensuring that the same image is never saved twice.

It's fine if the same image is generated more than once, but before it gets saved I need to check all images to-date for an identical copy. Eventually there are going to be thousands - even millions - of these images, so it's pretty unreasonable to store the raw files and check every single one against the active. Is there a way to reduce an image file to a unique key or string?

I considered some kind of SHA conversion - it would be really easy to check an image's hash against a table of logged hashes - but there is a distressing lack of information on the topic, and SHA has a small possibility of duplicates. Any help is appreciated - thanks!

4

1 回答 1

1

You may be able to use the hash_file function to perform this. (it's a pecl extension)

$hash = hash_file("sha256", $filename);

Basically collisions with the hash will be possible but highly unlikely. To further guard against them you can add on additional attributes like the size of the file to the hash.

$hash = hash_file("sha256", $filename)."-".filesize($filename);

Now collisions are only possible with two files of the exact same size that produce the same hash.

于 2013-07-02T21:01:32.763 回答