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

GmagickBinary   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 25.58%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 88
ccs 11
cts 43
cp 0.2558
rs 10
c 0
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A checkOperationality() 0 7 3
A supportsLossless() 0 3 1
A getGmagickPath() 0 6 2
A gmagickInstalled() 0 4 1
B doActualConvert() 0 32 7
A getOptionDefinitionsExtra() 0 4 1
A webPDelegateInstalled() 0 9 3
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
    public function supportsLossless()
22
    {
23
        return false;
24
    }
25
26 2
    protected function getOptionDefinitionsExtra()
27
    {
28
        return [
29 2
            ['use-nice', 'boolean', false],
30
        ];
31
    }
32
33 2
    private static function getGmagickPath()
34
    {
35 2
        if (empty(getenv('GMAGICK_PATH'))) {
36 2
            return 'gm';
37
        } else {
38
            return getenv('GMAGICK_PATH') . '/gm';
39
        }
40
    }
41
42 2
    public static function gmagickInstalled()
43
    {
44 2
        exec(self::getGmagickPath() . ' -version', $output, $returnCode);
45 2
        return ($returnCode == 0);
46
    }
47
48
    // Check if webp delegate is installed
49
    public static function webPDelegateInstalled()
50
    {
51
        exec(self::getGmagickPath() . ' -version', $output, $returnCode);
52
        foreach ($output as $line) {
53
            if (preg_match('#WebP.*yes#i', $line)) {
54
                return true;
55
            }
56
        }
57
        return false;
58
    }
59
60
    /**
61
     * Check (general) operationality of imagack converter executable
62
     *
63
     * @throws SystemRequirementsNotMetException  if system requirements are not met
64
     */
65 2
    public function checkOperationality()
66
    {
67 2
        if (!self::gmagickInstalled()) {
68 2
            throw new SystemRequirementsNotMetException('gmagick is not installed');
69
        }
70
        if (!self::webPDelegateInstalled()) {
71
            throw new SystemRequirementsNotMetException('webp delegate missing');
72
        }
73
    }
74
75
    protected function doActualConvert()
76
    {
77
        //$this->logLn('Using quality:' . $this->getCalculatedQuality());
78
79
        $commandArguments = [];
80
        if ($this->isQualityDetectionRequiredButFailing()) {
81
            // quality:auto was specified, but could not be determined.
82
            // we cannot apply the max-quality logic, but we can provide auto quality
83
            // simply by not specifying the quality option.
84
        } else {
85
            $commandArguments[] = '-quality ' . escapeshellarg($this->getCalculatedQuality());
86
        }
87
        $commandArguments[] = escapeshellarg($this->source);
88
        $commandArguments[] = escapeshellarg('webp:' . $this->destination);
89
90
        $command = self::getGmagickPath() . ' convert ' . implode(' ', $commandArguments);
91
92
        $useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false;
93
        if ($useNice) {
94
            $this->logLn('using nice');
95
            $command = 'nice ' . $command;
96
        }
97
        $this->logLn('command: ' . $command);
98
        exec($command, $output, $returnCode);
99
        if ($returnCode == 127) {
100
            throw new SystemRequirementsNotMetException('gmagick is not installed');
101
        }
102
        if ($returnCode != 0) {
103
            $this->logLn('command:' . $command);
104
            $this->logLn('return code:' . $returnCode);
105
            $this->logLn('output:' . print_r(implode("\n", $output), true));
106
            throw new SystemRequirementsNotMetException('The exec call failed');
107
        }
108
    }
109
}
110