resizeExact($image, $width, $height); // Resize to exactly 8x8 $gd = $image->getCore(); // Create an array of greyscale pixel values. $pixels = array(); for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $rgba = imagecolorat($gd, $x, $y); $r = ($rgba >> 16) & 0xFF; $g = ($rgba >> 8) & 0xFF; $b = $rgba & 0xFF; $pixels[] = floor(($r + $g + $b) / 3); // Gray } } // Get the average pixel value. $average = floor(array_sum($pixels) / count($pixels)); // Each hash bit is set based on whether the current pixels value is above or below the average. $hash = ''; foreach ($pixels as $pixel) { if ($pixel > $average) { $hash .= '1'; } else { $hash .= '0'; } } return $hash; } }