What is the recommended method for identifying duplicate images in a PHP application when file names may vary?

When identifying duplicate images in a PHP application where file names may vary, the recommended method is to compare the image content itself rather than relying solely on file names. This can be achieved by calculating a hash of the image data using a hashing algorithm such as MD5 or SHA-1, and then comparing these hashes to determine if images are duplicates.

// Function to calculate MD5 hash of an image file
function calculateImageHash($imagePath) {
    return md5_file($imagePath);
}

// Get the image hashes for two images
$hash1 = calculateImageHash('image1.jpg');
$hash2 = calculateImageHash('image2.png');

// Compare the hashes to determine if the images are duplicates
if ($hash1 === $hash2) {
    echo 'Images are duplicates';
} else {
    echo 'Images are not duplicates';
}