|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\Converters; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Convert\Converters\AbstractConverter; |
|
6
|
|
|
use WebPConvert\Convert\Converters\ConverterTraits\EncodingAutoTrait; |
|
7
|
|
|
use WebPConvert\Convert\Converters\ConverterTraits\ExecTrait; |
|
8
|
|
|
|
|
9
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; |
|
10
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailedException; |
|
11
|
|
|
|
|
12
|
|
|
//use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Convert images to webp by calling gmagick binary (gm). |
|
16
|
|
|
* |
|
17
|
|
|
* @package WebPConvert |
|
18
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
19
|
|
|
* @since Class available since Release 2.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class GmagickBinary extends AbstractConverter |
|
22
|
|
|
{ |
|
23
|
|
|
use ExecTrait; |
|
24
|
|
|
use EncodingAutoTrait; |
|
25
|
|
|
|
|
26
|
2 |
|
protected function getUnsupportedDefaultOptions() |
|
27
|
|
|
{ |
|
28
|
2 |
|
return [ |
|
29
|
2 |
|
'auto-filter', |
|
30
|
|
|
'near-lossless', |
|
31
|
|
|
'preset', |
|
32
|
|
|
'size-in-percentage', |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
2 |
|
|
|
36
|
|
|
private static function getGmagickPath() |
|
37
|
2 |
|
{ |
|
38
|
2 |
|
if (empty(getenv('GMAGICK_PATH'))) { |
|
39
|
|
|
return 'gm'; |
|
40
|
|
|
} else { |
|
41
|
|
|
return getenv('GMAGICK_PATH') . '/gm'; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function gmagickInstalled() |
|
46
|
|
|
{ |
|
47
|
|
|
exec(self::getGmagickPath() . ' -version', $output, $returnCode); |
|
48
|
|
|
return ($returnCode == 0); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function gmagickVersion() |
|
52
|
|
|
{ |
|
53
|
|
|
exec(self::getGmagickPath() . ' -version', $output, $returnCode); |
|
54
|
|
|
if (($returnCode == 0) && isset($output[0])) { |
|
55
|
|
|
return preg_replace('#http.*#', '', $output[0]); |
|
56
|
|
|
} |
|
57
|
|
|
return 'unknown'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Check if webp delegate is installed |
|
61
|
|
|
public static function webPDelegateInstalled() |
|
62
|
|
|
{ |
|
63
|
|
|
exec(self::getGmagickPath() . ' -version', $output, $returnCode); |
|
64
|
|
|
foreach ($output as $line) { |
|
65
|
|
|
if (preg_match('#WebP.*yes#i', $line)) { |
|
66
|
|
|
return true; |
|
67
|
2 |
|
} |
|
68
|
|
|
} |
|
69
|
2 |
|
return false; |
|
70
|
|
|
} |
|
71
|
2 |
|
|
|
72
|
2 |
|
/** |
|
73
|
|
|
* Check (general) operationality of imagack converter executable |
|
74
|
|
|
* |
|
75
|
|
|
* @throws SystemRequirementsNotMetException if system requirements are not met |
|
76
|
|
|
*/ |
|
77
|
|
|
public function checkOperationality() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->checkOperationalityExecTrait(); |
|
80
|
|
|
|
|
81
|
|
|
if (!self::gmagickInstalled()) { |
|
82
|
|
|
throw new SystemRequirementsNotMetException('gmagick is not installed'); |
|
83
|
|
|
} |
|
84
|
|
|
if (!self::webPDelegateInstalled()) { |
|
85
|
|
|
throw new SystemRequirementsNotMetException('webp delegate missing'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Build command line options |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
private function createCommandLineOptions() |
|
95
|
|
|
{ |
|
96
|
|
|
$commandArguments = []; |
|
97
|
|
|
|
|
98
|
|
|
// Unlike imagick binary, it seems gmagick binary uses a fixed |
|
99
|
|
|
// quality (75) when quality is omitted |
|
100
|
|
|
// $commandArguments[] = '-quality ' . escapeshellarg($this->getCalculatedQuality()); |
|
101
|
|
|
|
|
102
|
|
|
// encoding |
|
103
|
|
|
if ($this->options['encoding'] == 'lossless') { |
|
104
|
|
|
// Btw: |
|
105
|
|
|
// I am not sure if we should set "quality" for lossless. |
|
106
|
|
|
// Quality should not apply to lossless, but my tests shows that it does in some way for gmagick |
|
107
|
|
|
// setting it low, you get bad visual quality and small filesize. Setting it high, you get the opposite |
|
108
|
|
|
// Some claim it is a bad idea to set quality, but I'm not so sure. |
|
109
|
|
|
// https://stackoverflow.com/questions/4228027/ |
|
110
|
|
|
// First, I do not just get bigger images when setting quality, as toc777 does. |
|
111
|
|
|
// Secondly, the answer is very old and that bad behaviour is probably fixed by now. |
|
112
|
|
|
$commandArguments[] = '-define webp:lossless=true'; |
|
113
|
|
|
} else { |
|
114
|
|
|
$commandArguments[] = '-define webp:lossless=false'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($this->options['alpha-quality'] !== 100) { |
|
118
|
|
|
$commandArguments[] = '-define webp:alpha-quality=' . strval($this->options['alpha-quality']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($this->options['low-memory']) { |
|
122
|
|
|
$commandArguments[] = '-define webp:low-memory=true'; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($this->options['metadata'] == 'none') { |
|
126
|
|
|
$commandArguments[] = '-strip'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$commandArguments[] = '-define webp:method=' . $this->options['method']; |
|
130
|
|
|
|
|
131
|
|
|
$commandArguments[] = escapeshellarg($this->source); |
|
132
|
|
|
$commandArguments[] = escapeshellarg('webp:' . $this->destination); |
|
133
|
|
|
|
|
134
|
|
|
return implode(' ', $commandArguments); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
protected function doActualConvert() |
|
138
|
|
|
{ |
|
139
|
|
|
//$this->logLn('Using quality:' . $this->getCalculatedQuality()); |
|
140
|
|
|
|
|
141
|
|
|
$this->logLn('Version: ' . self::gmagickVersion()); |
|
142
|
|
|
|
|
143
|
|
|
$command = self::getGmagickPath() . ' convert ' . $this->createCommandLineOptions(); |
|
144
|
|
|
|
|
145
|
|
|
$useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false; |
|
146
|
|
|
if ($useNice) { |
|
147
|
|
|
$this->logLn('using nice'); |
|
148
|
|
|
$command = 'nice ' . $command; |
|
149
|
|
|
} |
|
150
|
|
|
$this->logLn('command: ' . $command); |
|
151
|
|
|
exec($command, $output, $returnCode); |
|
152
|
|
|
if ($returnCode == 127) { |
|
153
|
|
|
throw new SystemRequirementsNotMetException('gmagick is not installed'); |
|
154
|
|
|
} |
|
155
|
|
|
if ($returnCode != 0) { |
|
156
|
|
|
$this->logLn('command:' . $command); |
|
157
|
|
|
$this->logLn('return code:' . $returnCode); |
|
158
|
|
|
$this->logLn('output:' . print_r(implode("\n", $output), true)); |
|
159
|
|
|
throw new SystemRequirementsNotMetException('The exec call failed'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|