From 3c1b0d48763a97ce468744e4ca35efa70d11472b Mon Sep 17 00:00:00 2001 From: Seshachala Malisetti Date: Tue, 14 Oct 2014 15:37:45 +0530 Subject: [PATCH] simplified --- src/DifferenceHashing.php | 53 ++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/src/DifferenceHashing.php b/src/DifferenceHashing.php index 5d0bd04..2cb82cd 100644 --- a/src/DifferenceHashing.php +++ b/src/DifferenceHashing.php @@ -9,8 +9,8 @@ */ use Intervention\Image\ImageManagerStatic as Image; -class DifferenceHashing implements DifferenceHashingInterface -{ +class DifferenceHashing implements DifferenceHashingInterface { + const COMMON_HEIGHT = 8; const COMMON_WIDTH = 9; @@ -20,76 +20,65 @@ class DifferenceHashing implements DifferenceHashingInterface private $height; private $pixels = array(); private $difference = array(); - private $binaryStr; - private $hexStr; - public function __construct($image) - { + public function __construct($image) { $this->imagePath = $image; $this->interventionImage = Image::make($image); $this->width = $this->interventionImage->width(); $this->height = $this->interventionImage->height(); } - public function compareAdjacentPixels() - { + public function compareAdjacentPixels() { $resource = imagecreatefromstring($this->interventionImage->encode()->getEncoded()); $wRange = range(0, $this->width - 1); $hRange = range(0, $this->height - 1); - foreach($wRange as $row) { - foreach($hRange as $col) { + foreach ($wRange as $row) { + foreach ($hRange as $col) { $colorIndex = imagecolorat($resource, $row, $col); $r = ($colorIndex >> 16) & 0xFF; $this->pixels[$row][$col] = $r; } } - foreach($hRange as $row) { - foreach($wRange as $col) { - if($col != $this->width) { + foreach ($hRange as $row) { + foreach ($wRange as $col) { + if ($col != $this->width) { $this->difference[$row][$col] = $this->pixels[$row] > $this->pixels[$col]; } } } - + return $this; } - public function compareHashes($hash1, $hash2) - { + public function compareHashes($hash1, $hash2) { return $hash1 === $hash2; } - public function convertDifferenceIntoBits() - { - $this->binaryStr = ''; - foreach($this->difference as $diff) { - foreach($diff as $d) { - $this->binaryStr .= (int)$d; + public function convertDifferenceIntoBits() { + $binaryStr = ''; + foreach ($this->difference as $diff) { + foreach ($diff as $d) { + $binaryStr .= (int) $d; } } - - $this->hexStr = dechex(bindec($this->binaryStr)); - - return $this->hexStr; + + return dechex(bindec($binaryStr)); } - public function greyScaleImage() - { + public function greyScaleImage() { $this->interventionImage->greyscale(); return $this; } - public function openImage() - { + public function openImage() { $this->interventionImage = Image::make($this->imagePath); return $this; } - public function shrinkImageToCommonSize() - { + public function shrinkImageToCommonSize() { $this->interventionImage->resize(self::COMMON_WIDTH, self::COMMON_HEIGHT); $this->width = $this->interventionImage->width(); $this->height = $this->interventionImage->height();