Last active
April 2, 2024 10:33
-
-
Save vincurekf/dd867f14abced679615e905ca161cd68 to your computer and use it in GitHub Desktop.
Calculates average image hash, or compares two images if given two image paths. Ported code from example at https://01101001.net/averagehash.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $hash1 = ""; | |
| $hash2 = ""; | |
| $similarity = 0; | |
| if (isset($argv[1])) { | |
| $hash1 = averageHash($argv[1]); | |
| } | |
| if (isset($argv[2])) { | |
| $hash2 = averageHash($argv[2]); | |
| } | |
| if (!empty($hash1) && !empty($hash2)) { | |
| // Return similarity in percent | |
| $similarity = compareHash($hash1, $hash2); | |
| echo $similarity; | |
| die; | |
| } | |
| if (!empty($hash1)) { | |
| // Return single image hash | |
| echo $hash1; | |
| die; | |
| } | |
| function averageHash($imagePath) | |
| { | |
| $image = null; | |
| $extension = pathinfo($imagePath, PATHINFO_EXTENSION); | |
| switch ($extension) { | |
| case 'jpg': | |
| case 'jpeg': | |
| // For JPEG images | |
| $image = imagecreatefromjpeg($imagePath); | |
| break; | |
| case 'png': | |
| // For PNG images | |
| $image = imagecreatefrompng($imagePath); | |
| break; | |
| case 'gif': | |
| // For GIF images | |
| $image = imagecreatefromgif($imagePath); | |
| break; | |
| default: | |
| echo "$imagePath: This script only works with png,jpg or gif images. Exiting."; | |
| break; | |
| } | |
| if (is_null($image)) { | |
| return null; | |
| } | |
| $hashImage = imagescale($image, 8, 8); | |
| imagefilter($hashImage, IMG_FILTER_GRAYSCALE); | |
| $averagePixel = 0; | |
| $averageHashValue = ""; | |
| // Calculate the average brightness of the pixels. | |
| for ($y = 0; $y < 8; $y++) { | |
| for ($x = 0; $x < 8; $x++) { | |
| $averagePixel += imagecolorat($hashImage, $x, $y); | |
| } | |
| } | |
| $averagePixel /= 64.0; | |
| // Go through the image pixel by pixel. | |
| // Return 1-bits when a pixel is equal to or brighter than the average | |
| // pixel, and 0-bits when it's below. | |
| for ($y = 0; $y < 8; $y++) { | |
| for ($x = 0; $x < 8; $x++) { | |
| if (imagecolorat($hashImage, $x, $y) >= $averagePixel) { | |
| $averageHashValue .= "1"; | |
| } else { | |
| $averageHashValue .= "0"; | |
| } | |
| } | |
| } | |
| $hashValue = dechex((int) bindec($averageHashValue)); | |
| return $hashValue; | |
| } | |
| function xorCompare($bigInt1, $bigInt2) | |
| { | |
| while (strlen($bigInt1) < strlen($bigInt2)) { | |
| $bigInt1 = "0" . $bigInt1; | |
| } | |
| while (strlen($bigInt2) < strlen($bigInt1)) { | |
| $bigInt2 = "0" . $bigInt2; | |
| } | |
| $onexortwo = ""; | |
| for ($i = 0; $i < strlen($bigInt1); $i++) { | |
| if ($bigInt1[$i] == $bigInt2[$i]) { | |
| $onexortwo .= "0"; | |
| } else { | |
| $onexortwo .= "1"; | |
| } | |
| } | |
| return $onexortwo; | |
| } | |
| function compareHash($hash1, $hash2) | |
| { | |
| return (64 - (count(explode("1", xorCompare($hash1, $hash2))) - 1)) * 100.0 / 64.0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment