Skip to content

Commit

Permalink
Merge pull request #24 from klermonte/master
Browse files Browse the repository at this point in the history
Fix hexadecimal to decimal conversion for negative  hashes
  • Loading branch information
jenssegers authored Apr 21, 2017
2 parents 02279b8 + 337bc08 commit 005c906
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/ImageHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public function distance($hash1, $hash2)
}
} else {
if ($this->mode === self::HEXADECIMAL) {
$hash1 = hexdec($hash1);
$hash2 = hexdec($hash2);
$hash1 = $this->hexdec($hash1);
$hash2 = $this->hexdec($hash2);
}

$dh = 0;
Expand All @@ -129,6 +129,22 @@ public function distance($hash1, $hash2)
return $dh;
}

/**
* Convert hexadecimal to signed decimal.
*
* @param string $hex
* @return int
*/
public function hexdec($hex)
{
if (strlen($hex) == 16 && hexdec($hex[0]) > 8) {
list($higher, $lower) = array_values(unpack('N2', hex2bin($hex)));
return $higher << 32 | $lower;
}

return hexdec($hex);
}

/**
* Get a GD2 resource from file.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/ImageHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,26 @@ public function testHashStringSameAsFile()

$this->assertSame($this->imageHash->hash($path), $this->imageHash->hashFromString(file_get_contents($path)));
}

public function testHexdecForNegativeIntegers()
{
// native hexdec dechex conversion working for positive integers
$this->assertEquals(1, hexdec(dechex(1)));
// but not working for negative
$this->assertNotEquals(-1, hexdec(dechex(-1)));

// custom hexdec implementation works for both
$this->assertEquals(1, $this->imageHash->hexdec(dechex(1)));
$this->assertEquals(-1, $this->imageHash->hexdec(dechex(-1)));
}

public function testDistanceOfNegativeHashes()
{
$imageHash = new ImageHash(null, ImageHash::HEXADECIMAL);
$hash1 = 'ffffffffffffffff'; // -1
$hash2 = 'fffffffffffffff0'; // -16

$distance = $imageHash->distance($hash1, $hash2);
$this->assertEquals(4, $distance);
}
}

0 comments on commit 005c906

Please sign in to comment.