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

GmagickBinary::getOptionDefinitionsExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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 gmagick binary (gm).
14
 *
15
 * @package    WebPConvert
16
 * @author     Bjørn Rosell <[email protected]>
17
 * @since      Class available since Release 2.0.0
18
 */
19
class GmagickBinary extends AbstractExecConverter
20
{
21
    protected $supportsLossless = false;
22
23 1
    protected function getOptionDefinitionsExtra()
24
    {
25
        return [
26 1
            ['use-nice', 'boolean', false],
27
        ];
28
    }
29
30 1
    public static function gmagickInstalled()
31
    {
32 1
        exec('gm -version', $output, $returnCode);
33 1
        return ($returnCode == 0);
34
    }
35
36
    // Check if webp delegate is installed
37
    public static function webPDelegateInstalled()
38
    {
39
        exec('gm -version', $output, $returnCode);
40
        foreach ($output as $line) {
41
            if (preg_match('#WebP.*yes#i', $line)) {
42
                return true;
43
            }
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * Check (general) operationality of imagack converter executable
50
     *
51
     * @throws SystemRequirementsNotMetException  if system requirements are not met
52
     */
53 1
    public function checkOperationality()
54
    {
55 1
        if (!self::gmagickInstalled()) {
56 1
            throw new SystemRequirementsNotMetException('gmagick is not installed');
57
        }
58
        if (!self::webPDelegateInstalled()) {
59
            throw new SystemRequirementsNotMetException('webp delegate missing');
60
        }
61
    }
62
63
    protected function doActualConvert()
64
    {
65
        //$this->logLn('Using quality:' . $this->getCalculatedQuality());
66
67
        $commandArguments = [];
68
        if ($this->isQualityDetectionRequiredButFailing()) {
69
            // quality:auto was specified, but could not be determined.
70
            // we cannot apply the max-quality logic, but we can provide auto quality
71
            // simply by not specifying the quality option.
72
        } else {
73
            $commandArguments[] = '-quality ' . escapeshellarg($this->getCalculatedQuality());
74
        }
75
        $commandArguments[] = escapeshellarg($this->source);
76
        $commandArguments[] = escapeshellarg('webp:' . $this->destination);
77
78
        $command = 'gm convert ' . implode(' ', $commandArguments);
79
80
        $useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false;
81
        if ($useNice) {
82
            $this->logLn('using nice');
83
            $command = 'nice ' . $command;
84
        }
85
        $this->logLn('command: ' . $command);
86
        exec($command, $output, $returnCode);
87
        if ($returnCode == 127) {
88
            throw new SystemRequirementsNotMetException('gmagick is not installed');
89
        }
90
        if ($returnCode != 0) {
91
            $this->logLn('command:' . $command);
92
            $this->logLn('return code:' . $returnCode);
93
            $this->logLn('output:' . print_r(implode("\n", $output), true));
94
            throw new SystemRequirementsNotMetException('The exec call failed');
95
        }
96
    }
97
}
98