Passed
Push — master ( 74ce7c...3af96c )
by Bjørn
03:41
created

ImagickBinary::supportsLossless()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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