Created
May 28, 2020 13:00
-
-
Save garygreen/dbb565fa1efbf5b8179d4570e88adb25 to your computer and use it in GitHub Desktop.
Revisions
-
garygreen created this gist
May 28, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ <?php namespace App\Image; use Jenssegers\ImageHash\Hash; use Jenssegers\ImageHash\Implementation; class GdDifferenceHash implements Implementation { /** * @var int */ protected $size; /** * @param int $size */ public function __construct($size = 8) { $this->size = $size; } /** * @inheritdoc */ public function hash($image) { $source = imagecreatefromjpeg($image); // For this implementation we create a 8x9 image. $width = $this->size + 1; $height = $this->size; // Resize the image. // $resized = $image->resize($width, $height); $resized = imagecreatetruecolor($width, $height); list($origwidth, $origheight) = getimagesize($image); imagecopyresized($resized, $source, 0, 0, 0, 0, $width, $height, $origwidth, $origheight); $bits = []; for ($y = 0; $y < $height; $y++) { // Get the pixel value for the leftmost pixel. $rgb = $this->pickColor($resized, 0, $y); $left = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114)); for ($x = 1; $x < $width; $x++) { // Get the pixel value for each pixel starting from position 1. $rgb = $this->pickColor($resized, $x, $y); $right = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114)); // Each hash bit is set based on whether the left pixel is brighter than the right pixel. // http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html $bits[] = (int) ($left > $right); // Prepare the next loop. $left = $right; } } return Hash::fromBits($bits); } protected function pickColor($image, $x, $y) { $colorIndex = imagecolorsforindex($image, imagecolorat($image, $x, $y)); return [$colorIndex['red'], $colorIndex['green'], $colorIndex['blue']]; } }