From 6f941d3a60795954c9677ed0061bba47e0e29fb7 Mon Sep 17 00:00:00 2001 From: travail Date: Mon, 25 Sep 2017 00:33:05 +0900 Subject: [PATCH] Fix typos and warnings --- README.md | 2 +- lib/PerceptualHash.php | 4 +-- .../Algorithm/DifferenceHash.php | 8 ++--- .../Exception/FileNotFoundException.php | 2 +- tests/unit/AverageHashTest.php | 35 +++++++++++-------- tests/unit/BasicTest.php | 24 ++++++++----- tests/unit/DifferenceHashTest.php | 35 +++++++++++-------- tests/unit/PerceptionHashTest.php | 35 +++++++++++-------- 8 files changed, 87 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 79ff31d..a61ac28 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ int distance(string $hash1, string $hash2) double similarity(string|resource $file) ``` -Calcuates the similarity to another image. +Calculates the similarity to another image. #### $file diff --git a/lib/PerceptualHash.php b/lib/PerceptualHash.php index 3a6fa50..59fbcad 100644 --- a/lib/PerceptualHash.php +++ b/lib/PerceptualHash.php @@ -36,7 +36,7 @@ class PerceptualHash { public function __construct($file, Algorithm $algorithm = null) { $resource = is_resource($file) ? $file : $this->load($file); - $this->algorithm = $algorithm ?: new AverageHash; + $this->algorithm = $algorithm instanceof Algorithm ? $algorithm : new AverageHash; $this->bin = $this->algorithm->bin($resource); $this->hex = $this->algorithm->hex($this->bin); } @@ -128,7 +128,7 @@ class PerceptualHash { /** * @param string $file Path to file * @return resource - * @throws Image\PerceptualHash\Exception\FileNotFoundException + * @throws \Image\PerceptualHash\Exception\FileNotFoundException * @throws Exception */ protected function load($file) diff --git a/lib/PerceptualHash/Algorithm/DifferenceHash.php b/lib/PerceptualHash/Algorithm/DifferenceHash.php index 30c3532..7a01761 100644 --- a/lib/PerceptualHash/Algorithm/DifferenceHash.php +++ b/lib/PerceptualHash/Algorithm/DifferenceHash.php @@ -15,16 +15,16 @@ class DifferenceHash implements Algorithm public function bin($resource) { $width = self::SIZE + 1; - $heigth = self::SIZE; + $height = self::SIZE; // Resize the image. - $resized = imagecreatetruecolor($width, $heigth); + $resized = imagecreatetruecolor($width, $height); imagecopyresampled($resized, $resource, 0, 0, 0, 0, - $width, $heigth, imagesx($resource), imagesy($resource)); + $width, $height, imagesx($resource), imagesy($resource)); $bin = ''; $one = 1; - for ($y = 0; $y < $heigth; $y++) { + for ($y = 0; $y < $height; $y++) { // Get the pixel value for the leftmost pixel. $rgb = imagecolorsforindex($resized, imagecolorat($resized, 0, $y)); $left = floor(($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3); diff --git a/lib/PerceptualHash/Exception/FileNotFoundException.php b/lib/PerceptualHash/Exception/FileNotFoundException.php index b93d945..8dbee50 100644 --- a/lib/PerceptualHash/Exception/FileNotFoundException.php +++ b/lib/PerceptualHash/Exception/FileNotFoundException.php @@ -6,7 +6,7 @@ use Exception; class FileNotFoundException extends Exception { - public function __construct($message = "", $code = 0, Exception $previous = null) + public function __construct($message = '', $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/tests/unit/AverageHashTest.php b/tests/unit/AverageHashTest.php index e3b2f42..4660b73 100644 --- a/tests/unit/AverageHashTest.php +++ b/tests/unit/AverageHashTest.php @@ -4,46 +4,53 @@ use Image\PerceptualHash; class AverageHashTest extends PHPUnit_Framework_TestCase { + private $path_image1; + private $path_image2; + private $hex_image1; + private $hex_image2; + private $bin_image1; + private $bin_image2; + public function setUp() { - $this->path_inuo1 = __DIR__ . '/../images/inuo1.jpg'; - $this->path_inuo2 = __DIR__ . '/../images/inuo2.jpg'; - $this->hex_inuo1 = 'fdd7a9d1c383c1ff'; - $this->hex_inuo2 = 'f7f30200c3c3c3ff'; - $this->bin_inuo1 = '1111110111010111101010011101000111000011100000111100000111111111'; - $this->bin_inuo2 = '1111011111110011000000100000000011000011110000111100001111111111'; + $this->path_image1 = __DIR__ . '/../images/inuo1.jpg'; + $this->path_image2 = __DIR__ . '/../images/inuo2.jpg'; + $this->hex_image1 = 'fdd7a9d1c383c1ff'; + $this->hex_image2 = 'f7f30200c3c3c3ff'; + $this->bin_image1 = '1111110111010111101010011101000111000011100000111100000111111111'; + $this->bin_image2 = '1111011111110011000000100000000011000011110000111100001111111111'; } public function testCalculateBinaryHash() { - $ph = new PerceptualHash($this->path_inuo1); + $ph = new PerceptualHash($this->path_image1); $bin = $ph->bin(); $this->assertEquals(64, strlen($bin)); - $this->assertEquals($this->bin_inuo1, $bin); + $this->assertEquals($this->bin_image2, $bin); } public function testCalculateHexHash() { - $ph = new PerceptualHash($this->path_inuo1); + $ph = new PerceptualHash($this->path_image1); $hex = $ph->hex(); $this->assertEquals(16, strlen($hex)); - $this->assertEquals($this->hex_inuo1, $hex); + $this->assertEquals($this->hex_image2, $hex); } public function testCompareDifferentImages() { - $ph = new PerceptualHash($this->path_inuo1); - $diff = $ph->compare($this->path_inuo2); + $ph = new PerceptualHash($this->path_image1); + $diff = $ph->compare($this->path_image2); $this->assertEquals(15, $diff); } public function testCompareSameImages() { - $ph = new PerceptualHash($this->path_inuo1); - $diff = $ph->compare($this->path_inuo1); + $ph = new PerceptualHash($this->path_image1); + $diff = $ph->compare($this->path_image2); $this->assertEquals(0, $diff); } diff --git a/tests/unit/BasicTest.php b/tests/unit/BasicTest.php index 7314f4b..7f3ca07 100644 --- a/tests/unit/BasicTest.php +++ b/tests/unit/BasicTest.php @@ -5,15 +5,23 @@ use Image\PerceptualHash\Exception\FileNotFoundException; class BasicTest extends PHPUnit_Framework_TestCase { + private $path_non_existent_image; + private $path_image1; + private $path_image2; + private $hex_image1; + private $hex_image2; + private $bin_image1; + private $bin_image2; + public function setUp() { - $this->path_non_existent_inuo = __DIR__ . '/../images/non_existent_inuo.jpg'; - $this->path_inuo1 = __DIR__ . '/../images/inuo1.jpg'; - $this->path_inuo2 = __DIR__ . '/../images/inuo2.jpg'; - $this->hex_inuo1 = 'fdd7a9d1c383c1ff'; - $this->hex_inuo2 = 'f7f30200c3c3c3ff'; - $this->bin_inuo1 = '1111110111010111101010011101000111000011100000111100000111111111'; - $this->bin_inuo2 = '1111011111110011000000100000000011000011110000111100001111111111'; + $this->path_non_existent_image = __DIR__ . '/../images/non_existent_inuo.jpg'; + $this->path_image1 = __DIR__ . '/../images/inuo1.jpg'; + $this->path_image2 = __DIR__ . '/../images/inuo2.jpg'; + $this->hex_image1 = 'fdd7a9d1c383c1ff'; + $this->hex_image2 = 'f7f30200c3c3c3ff'; + $this->bin_image1 = '1111110111010111101010011101000111000011100000111100000111111111'; + $this->bin_image2 = '1111011111110011000000100000000011000011110000111100001111111111'; } public function testVersion() @@ -27,7 +35,7 @@ class BasicTest extends PHPUnit_Framework_TestCase public function testLoad() { try { - $ph = new PerceptualHash($this->path_non_existent_inuo); + new PerceptualHash($this->path_non_existent_image); } catch (FileNotFoundException $e) { $this->assertTrue($e instanceof FileNotFoundException); } diff --git a/tests/unit/DifferenceHashTest.php b/tests/unit/DifferenceHashTest.php index 854ccdc..43514a0 100644 --- a/tests/unit/DifferenceHashTest.php +++ b/tests/unit/DifferenceHashTest.php @@ -5,41 +5,48 @@ use Image\PerceptualHash\Algorithm\DifferenceHash; class DifferenceHashTest extends PHPUnit_Framework_TestCase { + private $path_image1; + private $path_image2; + private $hex_image1; + private $hex_image2; + private $bin_image1; + private $bin_image2; + public function setUp() { - $this->path_inuo1 = __DIR__ . '/../images/inuo1.jpg'; - $this->path_inuo2 = __DIR__ . '/../images/inuo2.jpg'; - $this->hex_inuo1 = '4c58d4dcc9ed6c49'; - $this->hex_inuo2 = '5978ac96d0555949'; - $this->bin_inuo1 = '0100110001011000110101001101110011001001111011010110110001001001'; - $this->bin_inuo2 = '0101100101111000101011001001011011010000010101010101100101001001'; + $this->path_image1 = __DIR__ . '/../images/inuo1.jpg'; + $this->path_image2 = __DIR__ . '/../images/inuo2.jpg'; + $this->hex_image1 = '4c58d4dcc9ed6c49'; + $this->hex_image2 = '5978ac96d0555949'; + $this->bin_image1 = '0100110001011000110101001101110011001001111011010110110001001001'; + $this->bin_image2 = '0101100101111000101011001001011011010000010101010101100101001001'; } public function testCalculateBinaryHash() { $algorithm = new DifferenceHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); + $ph = new PerceptualHash($this->path_image1, $algorithm); $bin = $ph->bin(); $this->assertEquals(64, strlen($bin)); - $this->assertEquals($this->bin_inuo1, $bin); + $this->assertEquals($this->bin_image2, $bin); } public function testCalculateHexHash() { $algorithm = new DifferenceHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); + $ph = new PerceptualHash($this->path_image1, $algorithm); $hex = $ph->hex(); $this->assertEquals(16, strlen($hex)); - $this->assertEquals($this->hex_inuo1, $hex); + $this->assertEquals($this->hex_image2, $hex); } public function testCompareDifferentImages() { $algorithm = new DifferenceHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); - $diff = $ph->compare($this->path_inuo2); + $ph = new PerceptualHash($this->path_image1, $algorithm); + $diff = $ph->compare($this->path_image2); $this->assertEquals(22, $diff); } @@ -47,8 +54,8 @@ class DifferenceHashTest extends PHPUnit_Framework_TestCase public function testCompareSameImages() { $algorithm = new DifferenceHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); - $diff = $ph->compare($this->path_inuo1); + $ph = new PerceptualHash($this->path_image1, $algorithm); + $diff = $ph->compare($this->path_image2); $this->assertEquals(0, $diff); } diff --git a/tests/unit/PerceptionHashTest.php b/tests/unit/PerceptionHashTest.php index d254d21..c0a78fa 100644 --- a/tests/unit/PerceptionHashTest.php +++ b/tests/unit/PerceptionHashTest.php @@ -5,41 +5,48 @@ use Image\PerceptualHash\Algorithm\PerceptionHash; class PerceptionHashTest extends PHPUnit_Framework_TestCase { + private $path_image1; + private $path_image2; + private $hex_image1; + private $hex_image2; + private $bin_image1; + private $bin_image2; + public function setUp() { - $this->path_inuo1 = __DIR__ . '/../images/inuo1.jpg'; - $this->path_inuo2 = __DIR__ . '/../images/inuo2.jpg'; - $this->hex_inuo1 = 'eacc911396406eb7'; - $this->hex_inuo2 = 'b9f8910e89f44e43'; - $this->bin_inuo1 = '1110101011001100100100010001001110010110010000000110111010110111'; - $this->bin_inuo2 = '1011100111111000100100010000111010001001111101000100111001000011'; + $this->path_image1 = __DIR__ . '/../images/inuo1.jpg'; + $this->path_image2 = __DIR__ . '/../images/inuo2.jpg'; + $this->hex_image1 = 'eacc911396406eb7'; + $this->hex_image2 = 'b9f8910e89f44e43'; + $this->bin_image1 = '1110101011001100100100010001001110010110010000000110111010110111'; + $this->bin_image2 = '1011100111111000100100010000111010001001111101000100111001000011'; } public function testCalculateBinaryHash() { $algorithm = new PerceptionHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); + $ph = new PerceptualHash($this->path_image1, $algorithm); $bin = $ph->bin(); $this->assertEquals(64, strlen($bin)); - $this->assertEquals($this->bin_inuo1, $bin); + $this->assertEquals($this->bin_image1, $bin); } public function testCalculateHexHash() { $algorithm = new PerceptionHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); + $ph = new PerceptualHash($this->path_image1, $algorithm); $hex = $ph->hex(); $this->assertEquals(16, strlen($hex)); - $this->assertEquals($this->hex_inuo1, $hex); + $this->assertEquals($this->hex_image1, $hex); } public function testCompareDifferentImages() { $algorithm = new PerceptionHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); - $diff = $ph->compare($this->path_inuo2); + $ph = new PerceptualHash($this->path_image1, $algorithm); + $diff = $ph->compare($this->path_image2); $this->assertEquals(26, $diff); } @@ -47,8 +54,8 @@ class PerceptionHashTest extends PHPUnit_Framework_TestCase public function testCompareSameImages() { $algorithm = new PerceptionHash; - $ph = new PerceptualHash($this->path_inuo1, $algorithm); - $diff = $ph->compare($this->path_inuo1); + $ph = new PerceptualHash($this->path_image1, $algorithm); + $diff = $ph->compare($this->path_image1); $this->assertEquals(0, $diff); }