-
Notifications
You must be signed in to change notification settings - Fork 0
/
UespWebpHandler.php
207 lines (155 loc) · 5.86 KB
/
UespWebpHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/*
* UespWebpHandler.php -- Dave Humphrey (dave@uesp.net), April 2020
*
* Very simple handler for WEBP images that supports resizing to WEBP format (instead of PNG) as well as basic
* resizing of animated images. Note that resizing animated WEBPs can result in imperfect replicas.
*
* Uses the Google shell applications available in the webp-libtools package or at:
* https://developers.google.com/speed/webp/download
*
* Specifically created for the UESP.net wiki but can be customized using the $wgUespWebpBinPath and
* $wgUespWebpCustomConvert global variables.
*/
global $IP;
require_once "$IP/includes/media/WebP.php";
global $wgUespWebpBinPath;
$wgUespWebpBinPath = '/usr/bin';
$wgUespWebpBinPath = '/home/uesp/webp/bin';
global $wgUespWebpCustomConvert;
$wgUespWebpCustomConvert = 'cwebp -resize %w %h %s -o %d';
class UespWebpHandler extends WebPHandler
{
public $ENABLE_ANIMATED_THUMBNAILS = false;
public function canRender($file)
{
return true;
}
public function canAnimateThumbnail($file)
{
return true;
}
public function getThumbType($ext, $mime, $params = null)
{
return array('webp', 'image/webp');
}
protected function getScalerType($dstPath, $checkDstPath = true)
{
return 'custom';
}
protected function transformCustom($image, $params)
{
global $wgUespWebpCustomConvert;
global $wgUespWebpBinPath;
if ($this->isAnimatedImage($image)) return $this->transformCustomAnimated($image, $params);
# Variables: %s %d %w %h
$src = wfEscapeShellArg($params['srcPath']);
$dst = wfEscapeShellArg($params['dstPath']);
$cmd = $wgUespWebpBinPath . "/" . $wgUespWebpCustomConvert;
$cmd = str_replace('%s', $src, str_replace('%d', $dst, $cmd)); # Filenames
$cmd = str_replace(
'%h',
wfEscapeShellArg($params['physicalHeight']),
str_replace('%w', wfEscapeShellArg($params['physicalWidth']), $cmd)
); # Size
wfDebug(__METHOD__ . ": Running custom convert command $cmd\n");
$retval = 0;
$err = wfShellExecWithStderr($cmd, $retval);
if ($retval !== 0) {
$this->logErrorForExternalProcess($retval, $err, $cmd);
return $this->getMediaTransformError($params, $err);
}
return false; # No error
}
protected function transformCustomAnimated($image, $params)
{
global $wgUespWebpCustomConvert;
global $wgUespWebpBinPath;
//if (!$this->ENABLE_ANIMATED_THUMBNAILS) return transformCustomNonAnimated( $image, $params );
$src = wfEscapeShellArg($params['srcPath']);
$dst = wfEscapeShellArg($params['dstPath']);
$width = wfEscapeShellArg($params['physicalWidth']);
$height = wfEscapeShellArg($params['physicalHeight']);
$srcWidth = $params['srcWidth'];
$srcHeight = $params['srcHeight'];
$pctWidth = floatval($params['physicalWidth']) / floatval($srcWidth);
$pctHeight = floatval($params['physicalHeight']) / floatval($srcHeight);
$cmd = "$wgUespWebpBinPath/webpmux -info $src";
wfDebug(__METHOD__ . ": Running custom convert command $cmd\n");
$retval = 0;
$output = wfShellExecWithStderr($cmd, $retval);
if ($retval !== 0) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
$outputLines = explode("\n", $output);
$frames = array();
if (count($outputLines) <= 2) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
foreach ($outputLines as $line) {
$frame = array();
$cols = preg_split("# #", $line, -1, PREG_SPLIT_NO_EMPTY);
if (count($cols) < 10) continue;
$frame['no'] = $cols[0];
$frame['width'] = $cols[1];
$frame['height'] = $cols[2];
$frame['alpha'] = $cols[3];
$frame['xoffset'] = $cols[4];
$frame['yoffset'] = $cols[5];
$frame['duration'] = $cols[6];
$frame['dispose'] = $cols[7];
$frame['blend'] = $cols[8];
$frame['size'] = $cols[9];
$frame['compression'] = $cols[10];
$frames[] = $frame;
}
if (count($frames) <= 1) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
$createCmd = "";
for ($i = 1; $i < count($frames); ++$i) {
$frame = &$frames[$i];
$tmpFile = TempFSFile::factory("anitransform_{$i}_", 'webp');
$tmpFile->bind($frames[$i]);
$frames[$i]['tmpfile'] = $tmpFile;
$tmpName = $tmpFile->getPath();
$cmd = "$wgUespWebpBinPath/webpmux -get frame $i $src -o '$tmpName'";
wfDebug(__METHOD__ . ": Running custom convert command $cmd\n");
$retval = 0;
$output = wfShellExecWithStderr($cmd, $retval);
if ($retval !== 0) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
$frameWidth = round(floatval($frame['width']) * $pctWidth);
$frameHeight = round(floatval($frame['height']) * $pctHeight);
$cmd = "$wgUespWebpBinPath/cwebp -resize $frameWidth $frameHeight '$tmpName' -o '$tmpName'";
wfDebug(__METHOD__ . ": Running custom convert command $cmd\n");
$retval = 0;
$output = wfShellExecWithStderr($cmd, $retval);
if ($retval !== 0) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
$di = $frame['duration'];
$xi = round(floatval($frame['xoffset']) * $pctWidth);
$yi = round(floatval($frame['yoffset']) * $pctHeight);
$mi = $frame['dispose'] == "none" ? "0" : "1";
$bi = $frame['blend'] == 'yes' ? '+b' : '-b';
$createCmd .= " -frame '$tmpName' +$di+$xi+$yi+$mi$bi";
if (!$this->ENABLE_ANIMATED_THUMBNAILS) break;
}
$cmd = "$wgUespWebpBinPath/webpmux $createCmd -o $dst";
wfDebug(__METHOD__ . ": Running custom convert command $cmd\n");
$retval = 0;
$output = wfShellExecWithStderr($cmd, $retval);
if ($retval !== 0) {
$this->logErrorForExternalProcess($retval, $output, $cmd);
return $this->getMediaTransformError($params, $output);
}
return false; # No error
}
};