This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
thumb.php
361 lines (285 loc) · 9.4 KB
/
thumb.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* Thumb
*
* @package Kirby Toolkit
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class Thumb extends Obj {
const ERROR_INVALID_IMAGE = 0;
const ERROR_INVALID_DRIVER = 1;
public static $drivers = array();
public static $defaults = array(
'destination' => false,
'filename' => '{safeName}-{hash}.{extension}',
'url' => '/thumbs',
'root' => '/thumbs',
'driver' => 'im',
'memory' => '128M',
'quality' => 90,
'blur' => false,
'blurpx' => 10,
'width' => null,
'height' => null,
'upscale' => false,
'crop' => false,
'grayscale' => false,
'overwrite' => false,
'autoOrient' => false,
'interlace' => false
);
public $source = null;
public $result = null;
public $destination = null;
public $options = array();
public $error = null;
/**
* Constructor
*
* @param mixed $source
* @param array $params
*/
public function __construct($source, $params = array()) {
$this->source = $this->result = is_a($source, 'Media') ? $source : new Media($source);
$this->options = array_merge(static::$defaults, $this->params($params));
$this->destination = $this->destination();
// don't create the thumbnail if it's not necessary
if($this->isObsolete()) return;
// don't create the thumbnail if it exists
if(!$this->isThere()) {
// try to create the thumb folder if it is not there yet
dir::make(dirname($this->destination->root));
// check for a valid image
if(!$this->source->exists() || $this->source->type() != 'image') {
throw new Error('The given image is invalid', static::ERROR_INVALID_IMAGE);
}
// check for a valid driver
if(!array_key_exists($this->options['driver'], static::$drivers)) {
throw new Error('Invalid thumbnail driver', static::ERROR_INVALID_DRIVER);
}
// create the thumbnail
$this->create();
// check if creating the thumbnail failed
if(!file_exists($this->destination->root)) return;
}
// create the result object
$this->result = new Media($this->destination->root, $this->destination->url);
}
/**
* Build the destination object
*
* @return Obj
*/
public function destination() {
if(is_callable($this->options['destination'])) {
return call($this->options['destination'], $this);
} else {
$destination = new Obj();
$safeName = f::safeName($this->source->name());
$destination->filename = str::template($this->options['filename'], array(
'extension' => $this->source->extension(),
'name' => $this->source->name(),
'filename' => $this->source->filename(),
'safeName' => $safeName,
'safeFilename' => $safeName . '.' . $this->extension(),
'width' => $this->options['width'],
'height' => $this->options['height'],
'hash' => md5($this->source->root() . $this->settingsIdentifier()),
));
$destination->url = $this->options['url'] . '/' . $destination->filename;
$destination->root = $this->options['root'] . DS . $destination->filename;
return $destination;
}
}
/**
* Returns the source media object
*
* @return Media
*/
public function source() {
return $this->source;
}
/**
* Returns the exception if available
*
* @return Exception
*/
public function error() {
return $this->error;
}
/**
* Makes it possible to pass a string of params
* which is shorter and more convenient than
* passing a full array of keys and values:
* width:300|height:200|crop:true
*
* @param array $params
* @return array
*/
public function params($params) {
if(is_array($params)) return $params;
$result = array();
foreach(explode('|', $params) as $param) {
$pos = strpos($param, ':');
$result[trim(substr($param, 0, $pos))] = trim(substr($param, $pos+1));
}
return $result;
}
/**
* Builds a hash for all relevant settings
*
* @return string
*/
public function settingsIdentifier() {
// build the settings string
return implode('-', array(
($this->options['width']) ? $this->options['width'] : 0,
($this->options['height']) ? $this->options['height'] : 0,
($this->options['upscale']) ? $this->options['upscale'] : 0,
($this->options['crop']) ? $this->options['crop'] : 0,
$this->options['blur'],
$this->options['grayscale'],
$this->options['quality']
));
}
/**
* Checks if the thumbnail already exists
* and is newer than the original file
*
* @return boolean
*/
public function isThere() {
if($this->options['overwrite'] === true) return false;
// if the thumb already exists and the source hasn't been updated
// we don't need to generate a new thumbnail
if(file_exists($this->destination->root) && f::modified($this->destination->root) >= $this->source->modified()) return true;
return false;
}
/**
* Checks if the thumbnail is not needed
* because the original image is small enough
*
* @return boolean
*/
public function isObsolete() {
if($this->options['overwrite'] === true) return false;
// try to use the original if resizing is not necessary
if($this->options['width'] >= $this->source->width() &&
$this->options['height'] >= $this->source->height() &&
$this->options['crop'] == false &&
$this->options['blur'] == false &&
$this->options['upscale'] == false) return true;
return false;
}
/**
* Calls the driver function and
* creates the thumbnail
*/
protected function create() {
return call_user_func_array(static::$drivers[$this->options['driver']], array($this));
}
/**
* Makes all public methods of the result object
* available to the thumb class
*
* @param string $method
* @param mixed $arguments
* @return mixed
*/
public function __call($method, $arguments) {
if(method_exists($this->result, $method)) {
return call_user_func_array(array($this->result, $method), $arguments);
}
}
/**
* Generates and returns the full html tag for the thumbnail
*
* @param array $attr An optional array of attributes, which should be added to the image tag
* @return string
*/
public function tag($attr = array()) {
// don't return the tag if the url is not available
if(!$this->result->url()) return false;
return html::img($this->result->url(), array_merge(array(
'alt' => isset($this->options['alt']) ? $this->options['alt'] : ' ',
'class' => isset($this->options['class']) ? $this->options['class'] : null,
), $attr));
}
/**
* Makes it possible to echo the entire object
*/
public function __toString() {
return $this->tag();
}
}
/**
* ImageMagick Driver
*/
thumb::$drivers['im'] = function($thumb) {
$command = array();
$command[] = isset($thumb->options['bin']) ? $thumb->options['bin'] : 'convert';
$command[] = '"' . $thumb->source->root() . '"';
$command[] = '-strip';
if($thumb->options['interlace']) {
$command[] = '-interlace line';
}
if($thumb->source->extension() === 'gif') {
$command[] = '-coalesce';
}
if($thumb->options['grayscale']) {
$command[] = '-colorspace gray';
}
if($thumb->options['autoOrient']) {
$command[] = '-auto-orient';
}
$command[] = '-resize';
if($thumb->options['crop']) {
if(empty($thumb->options['height'])) {
$thumb->options['height'] = $thumb->options['width'];
}
$command[] = $thumb->options['width'] . 'x' . $thumb->options['height'] . '^';
$command[] = '-gravity Center -crop ' . $thumb->options['width'] . 'x' . $thumb->options['height'] . '+0+0';
} else {
$dimensions = clone $thumb->source->dimensions();
$dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']);
$command[] = $dimensions->width() . 'x' . $dimensions->height() . '!';
}
$command[] = '-quality ' . $thumb->options['quality'];
if($thumb->options['blur']) {
$command[] = '-blur 0x' . $thumb->options['blurpx'];
}
$command[] = '-limit thread 1';
$command[] = '"' . $thumb->destination->root . '"';
exec(implode(' ', $command));
};
/**
* GDLib Driver
*/
thumb::$drivers['gd'] = function($thumb) {
try {
$img = new abeautifulsite\SimpleImage($thumb->root());
$img->quality = $thumb->options['quality'];
if($thumb->options['crop']) {
@$img->thumbnail($thumb->options['width'], $thumb->options['height']);
} else {
$dimensions = clone $thumb->source->dimensions();
$dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']);
@$img->resize($dimensions->width(), $dimensions->height());
}
if($thumb->options['grayscale']) {
$img->desaturate();
}
if($thumb->options['blur']) {
$img->blur('gaussian', $thumb->options['blurpx']);
}
if($thumb->options['autoOrient']) {
$img->auto_orient();
}
@$img->save($thumb->destination->root);
} catch(Exception $e) {
$thumb->error = $e;
}
};