Skip to content

Instantly share code, notes, and snippets.

@m8rge m8rge/HashBuilder.php forked from mncaudill/similar.php
Last active Aug 29, 2015

Embed
What would you like to do?
Image similarity algorithm
<?php
# Algorithm found here: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
class HashBuilder
{
private $buf;
function __construct()
{
$this->buf = imagecreatetruecolor(8, 8);
}
function __destruct()
{
imagedestroy($this->buf);
}
public function hash($imageData)
{
$img = imagecreatefromstring($imageData);
imagecopyresampled($this->buf, $img, 0, 0, 0, 0, 8, 8, imagesx($img), imagesy($img));
imagedestroy($img);
imagefilter($this->buf, IMG_FILTER_GRAYSCALE);
$colors = array();
for ($i = 0; $i < 8; $i++) {
for ($j = 0; $j < 8; $j++) {
$colors[] = imagecolorat($this->buf, $i, $j) & 0xFF;
}
}
$avg = array_sum($colors) / 64;
$bin = '';
foreach ($colors as $color) {
$bin .= $color > $avg ? '1' : '0';
}
return base_convert($bin, 2, 16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.