Skip to content

Instantly share code, notes, and snippets.

@MSeven
Last active March 17, 2024 16:22
Show Gist options
  • Save MSeven/9654f6d70713c141ed25bfc0d3bf1a96 to your computer and use it in GitHub Desktop.
Save MSeven/9654f6d70713c141ed25bfc0d3bf1a96 to your computer and use it in GitHub Desktop.
Image Hash/deduplication. Scaling the image down, and converting to a predefined color palette will remove most scaling and compression artefacts, and will make images with similar content compareable/findable, independent of original scaling and file formats.
<?php
public function generateImageHash(bool $getRaw = false)
{
$imgHandle = new \Imagick();
try {
$imgHandle->readImageBlob($this->getData());
$imgHandle = $imgHandle->coalesceImages();
$imgHandle->setImageFormat("png");
#$imgHandle->gaussianBlurImage(0.5, 1);
$imgHandle->thumbnailImage(50, 50);
#$imgHandle->blurImage(3, 3);
$imgHandle->posterizeImage(3, false);
foreach ($imgHandle->getImageProperties() as $key => $value) {
$imgHandle->deleteImageProperty($key);
}
if ($getRaw) {
return $imgHandle->getImageBlob();
}
$this->imghash = base64_encode(hex2bin(md5($imgHandle->getImageBlob())));
} catch (\ImagickException $e) {
echo "error creating imgHash.\n";
echo '>>>>>' . $e->getMessage() . "\n";
$this->imghash = $this->md5;
}
return $this->imghash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment