Hash of strippedImage with Imagick differes on every run

LordRazen

New member
Hello,

I created a small script which first copy an image, strip the Exif Data from an Image and rewrite it and then generate a regular md5_hash from it.

Well, this little script gives the same hash on almost every file. There's just one file, which returns a new random hash every time, and I simply can't find the reason why.

Here's the code:
Code:
<?php
// Copy due to trim process!
$imagePath = "Test.png";
copy('Skin2WithTrash.png', $imagePath);
// copy('Skin3WithTrash.png', $imagePath);

# Reduce image
$image = new Imagick(dirname(__FILE__) . '/' . $imagePath);
$image->stripImage();
$image->writeImage(dirname(__FILE__) . '/' . $imagePath);
$image->destroy();

# Return hash
var_dump(md5_file($imagePath));
unlink($imagePath);

While the Skin3WithTrash.png returns the same hash, it varies for Skin2WithTrash.png every time.

The simple question is: Why? And how can I solved this..

All files for needed to test it are attachted.
View attachment HashProblem.zip

Thx for every hint and happy Easter! :)
 
The reason why you are getting different MD5 hashes for the Skin2WithTrash.png file every time is likely due to the fact that the file contains some form of metadata or embedded information that is changing each time the file is copied or processed.
When you use the copy() function to duplicate the file, it creates an identical copy of the file, including any embedded metadata or additional attributes. However, when you strip the Exif data using Imagick's stripImage() method, it removes certain metadata from the image, potentially altering the file in a way that produces a different MD5 hash.If you want to consistently generate the same MD5 hash for Skin2WithTrash.png, you could try completely removing the Exif data using a different method or library, such as the exiftool command-line tool or a dedicated Exif data manipulation library in PHP.
Here's an example using the ExifTool command-line tool:
$imagePath = "Skin2WithTrash.png";
copy('Skin2WithTrash.png', $imagePath);

# Remove Exif data using exiftool
exec("exiftool -all= $imagePath");

# Return hash
var_dump(md5_file($imagePath));
unlink($imagePath);

By using exiftool -all=, you can remove all metadata from the image file before calculating the MD5 hash. This should produce a consistent hash for the Skin2WithTrash.png file.

Make sure to have exiftool installed on your system and accessible from the command line for this method to work.
 
MLSDev's team consists of skilled professionals who are passionate about delivering high-quality software solutions. They follow agile methodologies to ensure effective communication, collaboration, and timely project delivery.
 
Last edited:
Back
Top