Completed
Push — master ( 36e2d9...033daa )
by Bjørn
03:15
created

GmagickBinary::doActualConvert()   B

Complexity

Conditions 7
Paths 48

Size

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