1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdelespierre\PhpPhash\Command; |
4
|
|
|
|
5
|
|
|
use Bdelespierre\PhpPhash\Command\ValidatesSamplingSize; |
6
|
|
|
use Bdelespierre\PhpPhash\PHash; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class Compare extends Command |
14
|
|
|
{ |
15
|
|
|
use ValidatesSamplingSize; |
16
|
|
|
|
17
|
|
|
protected $phash; |
18
|
|
|
|
19
|
|
|
public function __construct(PHash $phash) |
20
|
|
|
{ |
21
|
|
|
$this->phash = $phash; |
22
|
|
|
|
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->setName('compare') |
29
|
|
|
->setDescription('Compare two images and outputs how similar they are') |
30
|
|
|
->addArgument('file1', InputArgument::REQUIRED, 'Pass the first file.') |
31
|
|
|
->addArgument('file2', InputArgument::REQUIRED, 'Pass the second file.') |
32
|
|
|
->addOption('size', 's', InputOption::VALUE_REQUIRED, 'Sampling size.', 8) |
33
|
|
|
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format.', 'percent'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
$this->validate($input); |
40
|
|
|
} catch (\InvalidArgumentException $e) { |
41
|
|
|
$output->writeln("<error>{$e->getMessage()}</error>"); |
42
|
|
|
return Command::FAILURE; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$size = $input->getOption('size'); |
46
|
|
|
$hash1 = $this->phash->hash(new \SplFileInfo($input->getArgument('file1')), $size); |
47
|
|
|
$hash2 = $this->phash->hash(new \SplFileInfo($input->getArgument('file2')), $size); |
48
|
|
|
$dist = $this->getHammingDistance($hash1, $hash2); |
49
|
|
|
$sim = 1 - $dist / ($size ** 2); |
50
|
|
|
|
51
|
|
|
$this->display($output, $input->getOption('format'), $dist, $sim); |
52
|
|
|
return Command::SUCCESS; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function validate(InputInterface $input) |
56
|
|
|
{ |
57
|
|
|
foreach (['file1', 'file2'] as $arg) { |
58
|
|
|
if (! is_readable($input->getArgument($arg))) { |
59
|
|
|
throw new \InvalidArgumentException("File {$input->getArgument($arg)} not found or unreadable"); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->validateSamplingSize($input->getOption('size')); |
64
|
|
|
|
65
|
|
|
if (! in_array($input->getOption('format'), ['percent', 'ratio', 'float', 'integer', 'int'])) { |
66
|
|
|
throw new \InvalidArgumentException("Invalid format"); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getHammingDistance(string $hash1, string $hash2): int |
71
|
|
|
{ |
72
|
|
|
$size = strlen($hash1); |
73
|
|
|
|
74
|
|
|
for ($dist = 0, $i = 0; $i < $size; $i++) { |
75
|
|
|
if ($hash1[$i] != $hash2[$i]) { |
76
|
|
|
$dist++; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $dist; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function display(OutputInterface $output, string $format, int $distance, float $similarity) |
84
|
|
|
{ |
85
|
|
|
switch ($format) { |
86
|
|
|
default: |
87
|
|
|
case 'percent': |
88
|
|
|
$output->writeln(round($similarity * 100) . '%'); |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'ratio': |
92
|
|
|
case 'float': |
93
|
|
|
$output->writeln($similarity); |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
case 'integer': |
97
|
|
|
case 'int': |
98
|
|
|
$output->writeln($distance); |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.