Passed
Push — master ( 26c56b...7d441f )
by Bjørn
08:31
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

Importance

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