Completed
Push — master ( 0235df...db4a8b )
by Bjørn
12:10 queued 01:07
created

ImagickBinary::doActualConvert()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 48
nop 0
dl 0
loc 38
ccs 0
cts 25
cp 0
crap 56
rs 8.6666
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
    protected function getOptionDefinitionsExtra()
27
    {
28
        return [
29
            ['use-nice', 'boolean', false],
30
        ];
31
    }
32
33
    public static function imagickInstalled()
34
    {
35
        exec('convert -version', $output, $returnCode);
36
        return ($returnCode == 0);
37
    }
38
39
    // Check if webp delegate is installed
40
    public static function webPDelegateInstalled()
41
    {
42
43
        exec('convert -list delegate', $output, $returnCode);
44
        foreach ($output as $line) {
45
            if (preg_match('#webp\\s*=#i', $line)) {
46
                return true;
47
            }
48
        }
49
50
        // try other command
51
        exec('convert -list configure', $output, $returnCode);
52
        foreach ($output as $line) {
53
            if (preg_match('#DELEGATE.*webp#i', $line)) {
54
                return true;
55
            }
56
        }
57
58
        return false;
59
60
        // PS, convert -version does not output delegates on travis, so it is not reliable
61
    }
62
63
    /**
64
     * Check (general) operationality of imagack converter executable
65
     *
66
     * @throws SystemRequirementsNotMetException  if system requirements are not met
67
     */
68
    public function checkOperationality()
69
    {
70
        if (!self::imagickInstalled()) {
71
            throw new SystemRequirementsNotMetException('imagick is not installed');
72
        }
73
        if (!self::webPDelegateInstalled()) {
74
            throw new SystemRequirementsNotMetException('webp delegate missing');
75
        }
76
    }
77
78
    protected function doActualConvert()
79
    {
80
        //$this->logLn('Using quality:' . $this->getCalculatedQuality());
81
82
        // Should we use "magick" or "convert" command?
83
        // It seems they do the same. But which is best supported? Which is mostly available (whitelisted)?
84
        // Should we perhaps try both?
85
        // For now, we just go with "convert"
86
87
88
        $commandArguments = [];
89
        if ($this->isQualityDetectionRequiredButFailing()) {
90
            // quality:auto was specified, but could not be determined.
91
            // we cannot apply the max-quality logic, but we can provide auto quality
92
            // simply by not specifying the quality option.
93
        } else {
94
            $commandArguments[] = '-quality ' . escapeshellarg($this->getCalculatedQuality());
95
        }
96
        $commandArguments[] = escapeshellarg($this->source);
97
        $commandArguments[] = escapeshellarg('webp:' . $this->destination);
98
99
        $command = 'convert ' . implode(' ', $commandArguments);
100
101
        $useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false;
102
        if ($useNice) {
103
            $this->logLn('using nice');
104
            $command = 'nice ' . $command;
105
        }
106
        $this->logLn('command: ' . $command);
107
        exec($command, $output, $returnCode);
108
        if ($returnCode == 127) {
109
            throw new SystemRequirementsNotMetException('imagick is not installed');
110
        }
111
        if ($returnCode != 0) {
112
            $this->logLn('command:' . $command);
113
            $this->logLn('return code:' . $returnCode);
114
            $this->logLn('output:' . print_r(implode("\n", $output), true));
115
            throw new SystemRequirementsNotMetException('The exec call failed');
116
        }
117
    }
118
}
119