Use LengthException

This commit is contained in:
travail 2015-01-22 00:11:12 +09:00
parent 284c50ba5b
commit 54719f6084
2 changed files with 17 additions and 11 deletions

View File

@ -2,7 +2,7 @@
namespace Image\PerceptualHash\Algorithm; namespace Image\PerceptualHash\Algorithm;
use Exception; use LengthException;
use Image\PerceptualHash\Algorithm; use Image\PerceptualHash\Algorithm;
class AverageHash implements Algorithm class AverageHash implements Algorithm
@ -34,25 +34,28 @@ class AverageHash implements Algorithm
// Calculate the average pixel value // Calculate the average pixel value
$average = floor(array_sum($pixels) / count($pixels)); $average = floor(array_sum($pixels) / count($pixels));
$binary = ''; $bin = '';
$one = 1; $one = 1;
foreach ($pixels as $pixel) { foreach ($pixels as $pixel) {
$binary .= $pixel > $average ? 1 : 0; $bin .= $pixel > $average ? 1 : 0;
$one = $one << 1; $one = $one << 1;
} }
return $binary; return $bin;
} }
public function hex($binary) /**
* {@inheritDoc}
*/
public function hex($bin)
{ {
if (strlen($binary) !== self::SIZE * self::SIZE) { if (strlen($bin) !== self::SIZE * self::SIZE) {
throw new Exception('Binary length must be ' . self::SIZE * self::SIZE); throw new LengthException('Binary length must be ' . self::SIZE * self::SIZE);
} }
$hex = ''; $hex = '';
foreach (str_split($binary, 4) as $binary) { foreach (str_split($bin, 4) as $bin) {
$hex .= dechex(bindec($binary)); $hex .= dechex(bindec($bin));
} }
return $hex; return $hex;

View File

@ -2,7 +2,7 @@
namespace Image\PerceptualHash\Algorithm; namespace Image\PerceptualHash\Algorithm;
use Exception; use LengthException;
use Image\PerceptualHash\Algorithm; use Image\PerceptualHash\Algorithm;
class DifferenceHash implements Algorithm class DifferenceHash implements Algorithm
@ -45,10 +45,13 @@ class DifferenceHash implements Algorithm
return $bin; return $bin;
} }
/**
* {@inheritDoc}
*/
public function hex($bin) public function hex($bin)
{ {
if (strlen($bin) !== self::SIZE * self::SIZE) { if (strlen($bin) !== self::SIZE * self::SIZE) {
throw new Exception('Binary length must be ' . self::SIZE * self::SIZE); throw new LengthException('Binary length must be ' . self::SIZE * self::SIZE);
} }
$hex = ''; $hex = '';