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