Compare::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Bdelespierre\PhpPhash\Command;
4
5
use Bdelespierre\PhpPhash\PHash;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Compare extends Command
13
{
14
    use ValidatesSamplingSize;
15
16
    protected $phash;
17
18
    public function __construct(PHash $phash)
19
    {
20
        $this->phash = $phash;
21
22
        parent::__construct();
23
    }
24
25 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
    {
27
        $this->setName('compare')
28
            ->setDescription('Compare two images and outputs how similar they are')
29
            ->addArgument('file1', InputArgument::REQUIRED, 'Pass the first file.')
30
            ->addArgument('file2', InputArgument::REQUIRED, 'Pass the second file.')
31
            ->addOption('size', 's', InputOption::VALUE_REQUIRED, 'Sampling size.', 8)
32
            ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format.', 'percent');
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        try {
38
            $this->validate($input);
39
        } catch (\InvalidArgumentException $e) {
40
            $output->writeln("<error>{$e->getMessage()}</error>");
41
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
53
        return Command::SUCCESS;
54
    }
55
56
    protected function validate(InputInterface $input)
57
    {
58
        foreach (['file1', 'file2'] as $arg) {
59
            if (!is_readable($input->getArgument($arg))) {
60
                throw new \InvalidArgumentException("File {$input->getArgument($arg)} not found or unreadable");
61
            }
62
        }
63
64
        $this->validateSamplingSize($input->getOption('size'));
65
66
        if (!in_array($input->getOption('format'), ['percent', 'ratio', 'float', 'integer', 'int'])) {
67
            throw new \InvalidArgumentException('Invalid format');
68
        }
69
    }
70
71
    protected function getHammingDistance(string $hash1, string $hash2): int
72
    {
73
        $size = strlen($hash1);
74
75
        for ($dist = 0, $i = 0; $i < $size; ++$i) {
76
            if ($hash1[$i] != $hash2[$i]) {
77
                ++$dist;
78
            }
79
        }
80
81
        return $dist;
82
    }
83
84
    protected function display(OutputInterface $output, string $format, int $distance, float $similarity)
85
    {
86
        switch ($format) {
87
            default:
88
            case 'percent':
89
                $output->writeln(round($similarity * 100).'%');
90
                break;
91
92
            case 'ratio':
93
            case 'float':
94
                $output->writeln($similarity);
95
                break;
96
97
            case 'integer':
98
            case 'int':
99
                $output->writeln($distance);
100
                break;
101
        }
102
    }
103
}
104