-
Notifications
You must be signed in to change notification settings - Fork 0
/
Imgurl.php
327 lines (288 loc) · 9.63 KB
/
Imgurl.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Depage\Graphics;
/*
* @todo test to stay inside 3MP as maximum image size for safety reasons and
* to be able to support all iOS devices:
* http://www.williammalone.com/articles/html5-javascript-ios-maximum-image-size/
*/
class Imgurl
{
protected $options = [];
protected $actions = [];
protected $srcImg = '';
protected $outImg = '';
protected $notFound = false;
public $id = "";
public $rendered = false;
protected $invalidAction = false;
protected $cachePath = '';
/*
* action aliases
*
* Note that the order is important so shoter action names should
* come after larger ones
*/
protected $aliases = array(
'quality' => "setQuality",
'q' => "setQuality",
'crop' => "addCrop",
'c' => "addCrop",
'resize' => "addResize",
'r' => "addResize",
'thumbfill' => "addThumbfill",
'tf' => "addThumbfill",
'thumb' => "addThumb",
't' => "addThumb",
'background' => "addBackground",
'bg' => "addBackground",
);
// {{{ constructor
/*
* @param $options hold the same options as the graphics class
*/
public function __construct($options = array())
{
$this->options = $options;
}
// }}}
// {{{ analyze
/*
* Analyzes the image url and set the path for srcImg and outImg
*/
protected function analyze($url)
{
$this->invalidAction = false;
$this->notFound = false;
$this->rendered = false;
$this->id = "";
$this->actions = [];
if (isset($this->options['baseUrl']) && isset($this->options['cachePath'])) {
$baseUrl = rtrim($this->options['baseUrl'], '/');
$this->cachePath = $this->options['cachePath'];
$relativePath = $this->options['relPath'] ?? '';
} else if (defined('DEPAGE_PATH') && defined('DEPAGE_CACHE_PATH')) {
// we are using depage-framework so use constants for paths
$info = parse_url(DEPAGE_BASE);
$baseUrl = rtrim($info['path'], '/');
$relativePath = "";
$this->cachePath = \DEPAGE_CACHE_PATH . "graphics/";
} else {
// we using the library plainly -> get path through url
$scriptParts = explode("/", $_SERVER["SCRIPT_NAME"]);
$uriParts = explode("/", $_SERVER["REQUEST_URI"]);
if (strpos($_SERVER["SCRIPT_NAME"], "/lib/") !== false) {
for ($i = 0; $i < count($uriParts); $i++) {
// find common parts of url up to lib parameter
if ($uriParts[$i] == "lib") {
break;
}
}
} else {
for ($i = 0; $i < count($uriParts); $i++) {
// find common parts of url up to lib parameter
if ($scriptParts[$i] != $uriParts[$i]) {
break;
}
}
}
$baseUrl = implode("/", array_slice($uriParts, 0, $i));
if (isset($this->options['relPath'])) {
$relativePath = $this->options['relPath'];
} else {
$relativePath = str_repeat("../", count($scriptParts) - $i - 1);
}
$this->cachePath = $relativePath . "lib/cache/graphics/";
}
$baseUrlStatic = $baseUrl;
if (isset($this->options['baseUrlStatic'])) {
$baseUrlStatic = rtrim($this->options['baseUrlStatic'], '/');
}
// get image name
$imgUrl = $url;
if ($baseUrl == "" && $baseUrlStatic == "") {
$imgUrl = substr($url, 1);
} else if (strpos($url, $baseUrlStatic) === 0) {
$imgUrl = substr($url, strlen($baseUrlStatic) + 1);
} else if (strpos($url, $baseUrl) === 0) {
$imgUrl = substr($url, strlen($baseUrl) + 1);
}
// get action parameters
$matches = [];
$success = preg_match("/(.*\.(jpg|jpeg|gif|png|webp|eps|tif|tiff|pdf|svg))\.([^\\\]*)\.(jpg|jpeg|gif|png|webp)/i", $imgUrl, $matches);
if (!$success) {
$this->invalidAction = true;
return;
}
$this->id = rawurldecode($matches[0]);
$this->srcImg = $relativePath . rawurldecode($matches[1]);
$this->outImg = $this->cachePath . rawurldecode($matches[0]);
$this->actions = $this->analyzeActions($matches[3]);
}
// }}}
// {{{ analyzeActions
/*
* Analyzes actions and replaces shortcuts with real actions
*/
protected function analyzeActions($actionString)
{
$actions = explode(".", $actionString);
foreach ($actions as &$action) {
$regex = implode("|", array_keys($this->aliases));
preg_match("/^($regex)/i", $action, $matches);
if (isset($matches[1]) && isset($this->aliases[$matches[1]])) {
$func = $this->aliases[$matches[1]];
$params = substr($action, strlen($matches[1]));
} else {
$func = "";
$params = "";
}
if (!empty($func)) {
$params = preg_split("/[-x,]+/", $params, -1, PREG_SPLIT_NO_EMPTY);
if ($func == "addBackground") {
if (!in_array($params[0], array("transparent", "checkerboard"))) {
$params[0] = "#{$params[0]}";
}
} else {
foreach ($params as $i => &$p) {
$p = intval($p);
if ($p == 0 && $i < 2) {
$p = null;
}
}
}
$this->actions[] = array($func, $params);
} else {
$this->invalidAction = true;
}
}
return $this->actions;
}
// }}}
// {{{ render
public function render($url = null)
{
if (is_null($url)) {
$url = $_SERVER["REQUEST_URI"];
}
$this->analyze($url);
if ($this->invalidAction) {
return $this;
}
// make cache diretories
$outDir = dirname($this->outImg);
if (!is_dir($outDir)) {
mkdir($outDir, 0755, true);
}
if (file_exists($this->outImg) && filemtime($this->outImg) >= filemtime($this->srcImg)) {
// rendered image does exist already
return $this;
}
try {
$graphics = Graphics::factory($this->options);
// add actions to graphics class
foreach ($this->actions as $action) {
list($func, $params) = $action;
if (is_callable(array($graphics, $func))) {
call_user_func_array(array($graphics, $func), $params);
}
}
// render image out
$graphics->render($this->srcImg, $this->outImg);
$this->rendered = true;
} catch (Exceptions\FileNotFound $e) {
$this->notFound = true;
return $this;
} catch (Exceptions\Exception $e) {
$this->invalidAction = true;
}
return $this;
}
// }}}
// {{{ display()
public function display()
{
$info = pathinfo($this->outImg);
$ext = strtolower($info['extension']);
if ($this->invalidAction) {
header("HTTP/1.1 500 Internal Server Error");
echo("invalid image action");
die();
}
if ($this->notFound) {
header("HTTP/1.1 404 Not Found");
echo("file not found");
die();
}
if ($ext == "jpg" || $ext == "jpeg") {
header("Content-type: image/jpeg");
} elseif ($ext == "png") {
header("Content-type: image/png");
} elseif ($ext == "gif") {
header("Content-type: image/gif");
} elseif ($ext == "webp") {
header("Content-type: image/webp");
}
readfile($this->outImg);
return $this;
}
// }}}
// {{{ getUrl()
public function getUrl($img)
{
$info = pathinfo($img);
$ext = $info['extension'];
if (count($this->actions) > 0) {
return $img . "." . implode(".", $this->actions) . "." . $ext;
} else {
return $img;
}
}
// }}}
// {{{ addBackground()
public function addBackground($background)
{
$this->actions[] = "bg{$background}";
return $this;
}
// }}}
// {{{ addCrop()
public function addCrop($width, $height, $x = 0, $y = 0)
{
$this->actions[] = "crop{$width}x{$height}-{$x}x{$y}";
return $this;
}
// }}}
// {{{ addResize()
public function addResize($width, $height)
{
$this->actions[] = "r{$width}x{$height}";
return $this;
}
// }}}
// {{{ addThumb()
public function addThumb($width, $height)
{
$this->actions[] = "t{$width}x{$height}";
return $this;
}
// }}}
// {{{ addThumbfill()
public function addThumbfill($width, $height, $centerX = 50, $centerY = 50)
{
$action = "tf{$width}x{$height}";
if ($centerX != 50 || $centerY != 50) {
$action .= "-{$centerX}x{$centerY}";
}
$this->actions[] = $action;
return $this;
}
// }}}
// {{{ setQuality()
public function setQuality($quality)
{
$this->actions[] = "q{$quality}";
return $this;
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */