Skip to content

Instantly share code, notes, and snippets.

@xaptronic
Forked from mncaudill/similar.php
Created March 7, 2013 22:43
Show Gist options
  • Save xaptronic/5112535 to your computer and use it in GitHub Desktop.
Save xaptronic/5112535 to your computer and use it in GitHub Desktop.
<?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