Created
October 31, 2011 05:33
-
-
Save mncaudill/1326966 to your computer and use it in GitHub Desktop.
PHP version of simple image similarity algorithm
This file contains 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 | |
# Algorithm found here: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html | |
$filename = 'image.jpg'; | |
list($width, $height) = getimagesize($filename); | |
$img = imagecreatefromjpeg($filename); | |
$new_img = imagecreatetruecolor(8, 8); | |
imagecopyresampled($new_img, $img, 0, 0, 0, 0, 8, 8, $width, $height); | |
imagefilter($new_img, IMG_FILTER_GRAYSCALE); | |
$colors = array(); | |
$sum = 0; | |
for ($i = 0; $i < 8; $i++) { | |
for ($j = 0; $j < 8; $j++) { | |
$color = imagecolorat($new_img, $i, $j) & 0xff; | |
$sum += $color; | |
$colors[] = $color; | |
} | |
} | |
$avg = $sum / 64; | |
$hash = ''; | |
$curr = ''; | |
$count = 0; | |
foreach ($colors as $color) { | |
if ($color > $avg) { | |
$curr .= '1'; | |
} else { | |
$curr .= '0'; | |
} | |
$count++; | |
if (!($count % 4)) { | |
$hash .= dechex(bindec($curr)); | |
$curr = ''; | |
} | |
} | |
print $hash . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment