Skip to content

Commit

Permalink
simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
Seshachala Malisetti committed Oct 14, 2014
1 parent 0345dfb commit 3c1b0d4
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/DifferenceHashing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down

0 comments on commit 3c1b0d4

Please sign in to comment.