# Using hash_file for image recognition

My team is writing software to be able to grade a basic skills in word/excel/powerpoint test. We are trying to figure out the best way to verify that they included a correct image in their word doc. We know how to get the image file that they use through the xml of the word file. The problem is when we want to verify that the image is the one we expect to be there. Right now we are using the hash_file function in php:

//we know this is the correct hash for the image
$img_hash_correct = 'ab9df995315e2c5cbd0d5cf2210410afa44958c3ca55147497c7a6761340e058';$hashed_file_submitted = hash_file("sha256", 'hashtest/word/media/image2.jpg');

if ($hashed_file_submitted ==$img_hash_correct) {
echo 'same image!   ';
} else {
echo 'these are not the same   ';
}


This code has worked after doing some testing. Is this a reliable way to verify that a submitted image is what we are looking for, or is there a more reliable way that you know of?

• Is this your real code? I would find it hard to believe that $img_hash_correct and the file path to the image for hashing are hard-coded like this. – Mike Brant Jun 9 '17 at 19:46 • The$hashed_file_submitted var will not end up being hard coded. This is just for testing purposes, I change the file path for every test right now. – Travis Smith Jun 12 '17 at 16:39
• This will only work if you expect them to submit a pixel-perfect replica of whatever your "good" value is. Otherwise, the hash will be different. If you expect the image to be close but not pixel-perfect, take a look at stuff like pHash. There are perceptual hashing algorithms designed for image comparisons that will give you a much better idea of how "close" the two images are. – Siegen Aug 8 '17 at 18:41

2. Use === for comparison.
• It is a short bit of code, but even still I don't think this short answer is very helpful. Moreover, I'm not sure if they are good suggestions. I don't see much point in making the image hash global: globals are rarely the answer, and that just means that you're going to have to hunt through your codebase to change it if it ever needs to change. The === operator doesn't change the picture here, so I don't think it is useful to suggest it without explaining. This can only be a string comparison, so the == is exactly the same as ===. So why should he care which he uses here? – Conor Mancone Nov 6 '17 at 15:00