Completed
Push — master ( 5f5fca...2fa1e0 )
by Bjørn
12:28 queued 02:24
created

ImagickBinary::webPDelegateInstalled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
// TODO: Quality option
3
namespace WebPConvert\Convert\Converters;
4
5
use WebPConvert\Convert\BaseConverters\AbstractExecConverter;
6
7
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
8
use WebPConvert\Convert\Exceptions\ConversionFailedException;
9
10
//use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException;
11
12
// To futher improve this converter, I could check out:
13
// https://github.com/Orbitale/ImageMagickPHP
14
class ImagickBinary extends AbstractExecConverter
15
{
16
    protected $supportsLossless = false;
17
    
18 1
    protected function getOptionDefinitionsExtra()
19
    {
20
        return [
21 1
            ['use-nice', 'boolean', false],
22 1
        ];
23
    }
24
25 1
    public static function imagickInstalled()
26
    {
27 1
        exec('convert -version', $output, $returnCode);
28 1
        return ($returnCode == 0);
29
    }
30
31
    // Check if webp delegate is installed
32 1
    public static function webPDelegateInstalled()
33
    {
34
        /* HM. We should not rely on grep being available
35
        $command = 'convert -list configure | grep -i "delegates" | grep -i webp';
36
        exec($command, $output, $returnCode);
37
        return (count($output) > 0);
38
        */
39 1
        $command = 'convert -version';
40 1
        exec($command, $output, $returnCode);
41 1
        foreach ($output as $line) {
42 1
            if (preg_match('/Delegate.*webp.*/i', $line)) {
43
                return true;
44
            }
45 1
        }
46 1
        return false;
47
    }
48
49
    /**
50
     * Check (general) operationality of imagack converter executable
51
     *
52
     * @throws SystemRequirementsNotMetException  if system requirements are not met
53
     */
54 1
    protected function checkOperationality()
55
    {
56 1
        if (!self::imagickInstalled()) {
57
            throw new SystemRequirementsNotMetException('imagick is not installed');
58
        }
59 1
        if (!self::webPDelegateInstalled()) {
60 1
            throw new SystemRequirementsNotMetException('webp delegate missing');
61
        }
62
    }
63
64
    protected function doActualConvert()
65
    {
66
        //$this->logLn('Using quality:' . $this->getCalculatedQuality());
67
        // Should we use "magick" or "convert" command?
68
        // It seems they do the same. But which is best supported? Which is mostly available (whitelisted)?
69
        // Should we perhaps try both?
70
        // For now, we just go with "convert"
71
72
73
        $commandArguments = [];
74
        if ($this->isQualityDetectionRequiredButFailing()) {
75
            // quality:auto was specified, but could not be determined.
76
            // we cannot apply the max-quality logic, but we can provide auto quality
77
            // simply by not specifying the quality option.
78
        } else {
79
            $commandArguments[] = '-quality ' . escapeshellarg($this->getCalculatedQuality());
80
        }
81
        $commandArguments[] = escapeshellarg($this->source);
82
        $commandArguments[] = escapeshellarg('webp:' . $this->destination);
83
84
        $command = 'convert ' . implode(' ', $commandArguments);
85
86
        $useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false;
87
        if ($useNice) {
88
            $this->logLn('using nice');
89
            $command = 'nice ' . $command;
90
        }
91
        $this->logLn('command: ' . $command);
92
        exec($command, $output, $returnCode);
93
        if ($returnCode == 127) {
94
            throw new SystemRequirementsNotMetException('imagick is not installed');
95
        }
96
        if ($returnCode != 0) {
97
            $this->logLn('command:' . $command);
98
            $this->logLn('return code:' . $returnCode);
99
            $this->logLn('output:' . print_r(implode("\n", $output), true));
100
            throw new SystemRequirementsNotMetException('The exec call failed');
101
        }
102
    }
103
}
104