Completed
Push — master ( b2eb8d...1d7bbd )
by Bjørn
03:51
created

ImagickBinary::checkOperationality()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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