0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Mike Brant Jun 9 '17 at 19:46
  • \$\begingroup\$ 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. \$\endgroup\$ – Travis Smith Jun 12 '17 at 16:39
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Siegen Aug 8 '17 at 18:41
-1
\$\begingroup\$

Without knowing much about your use case, the code looks straightforward. It does exactly what you specify and nothing more.

A few points:

  1. The constant image hash can be defined as global(?)
  2. Use === for comparison.
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Conor Mancone Nov 6 '17 at 15:00
  • \$\begingroup\$ The biggest issue is the choice of a hash comparison: that will fail if the image changes at all. There are lots of easy ways for the image to change in the course of an assignment like this, so even if his code is correct it is likely to fail for conceptual reasons. @Siegen's comment discuss that point. \$\endgroup\$ – Conor Mancone Nov 6 '17 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.