Fix typos and warnings

develop
travail 2017-09-25 00:33:05 +09:00
parent 471391788d
commit 6f941d3a60
8 changed files with 87 additions and 58 deletions

View File

@ -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

View File

@ -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)

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}