Using hash_file for image recognition

Posted on

Problem

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?

Solution

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.

Leave a Reply

Your email address will not be published. Required fields are marked *