[OOP] Thumbnail Class
Hoe te gebruiken:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$thumbnail = new Thumbnail(array(
'path' => 'tmp/', // Waar moeten de thumbs heen geschreven worden
'storage' => 'app/uploads/', // Eventuele map waar de afbeeldingen in staan.
'fallback' => 'app/interface/images/no-preview.gif', // Fallback als de afbeelding niet bestaat
'options' => array(
'method' => 'crop', // Type van de resize (scale of crop)
'width' => 200, // Breedte van de thumbnail
'height' => 200, // Hoogte van de thumbnail
'quality' => 100, // Kwaliteit van de thumbnail,
'flip' => false, // Flip afbeelding (false, both, horizontal, vertical)
'filter' => false, // Filters (false, string, array),
'background' => '#ffffff' // Achtergrond kleur
)
));
echo $thumbnail->display('afbeelding.gif'); // Geeft de thumbnail url/path weer.
?>
$thumbnail = new Thumbnail(array(
'path' => 'tmp/', // Waar moeten de thumbs heen geschreven worden
'storage' => 'app/uploads/', // Eventuele map waar de afbeeldingen in staan.
'fallback' => 'app/interface/images/no-preview.gif', // Fallback als de afbeelding niet bestaat
'options' => array(
'method' => 'crop', // Type van de resize (scale of crop)
'width' => 200, // Breedte van de thumbnail
'height' => 200, // Hoogte van de thumbnail
'quality' => 100, // Kwaliteit van de thumbnail,
'flip' => false, // Flip afbeelding (false, both, horizontal, vertical)
'filter' => false, // Filters (false, string, array),
'background' => '#ffffff' // Achtergrond kleur
)
));
echo $thumbnail->display('afbeelding.gif'); // Geeft de thumbnail url/path weer.
?>
Thumnail.php
Code (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<?php
class Thumbnail {
const NEGATE = IMG_FILTER_NEGATE;
const GRAYSCALE = IMG_FILTER_GRAYSCALE;
const BRIGHTNESS = IMG_FILTER_BRIGHTNESS;
const CONTRAST = IMG_FILTER_CONTRAST;
const COLORIZE = IMG_FILTER_COLORIZE;
const EDGEDETECT = IMG_FILTER_EDGEDETECT;
const EMBOSS = IMG_FILTER_EMBOSS;
const GAUSSIAN_BLUR = IMG_FILTER_GAUSSIAN_BLUR;
const SELECTIVE_BLUR = IMG_FILTER_SELECTIVE_BLUR;
const REMOVAL = IMG_FILTER_MEAN_REMOVAL;
const SMOOTH = IMG_FILTER_SMOOTH;
const PIXELATE = IMG_FILTER_PIXELATE;
/**
* @acces protected.
* @var string.
*/
protected $path = '';
/**
* @acces protected.
* @var string.
*/
protected $storage = '';
/**
* @acces protected.
* @var string.
*/
protected $fallback = null;
/**
* @acces protected.
* @var boolean.
*/
protected $cache = true;
/**
* @acces protected.
* @var string.
*/
protected $hash = 'md5';
/**
* @acces public.
* @var array.
*/
protected $options = array(
'method' => 'crop',
'width' => '100',
'height' => '100',
'quality' => 100,
'flip' => false,
'filter' => false,
'background' => '#ffffff'
);
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
}
if (isset($options['path'])) {
$this->setPath($options['path']);
}
if (isset($options['storage'])) {
$this->setStorage($options['storage']);
}
if (isset($options['fallback'])) {
$this->setFallback($options['fallback']);
}
if (isset($options['cache'])) {
$this->setCache($options['cache']);
}
if (isset($options['hash'])) {
$this->setHash($options['hash']);
}
if (isset($options['options'])) {
$this->setOptions($options['options']);
}
}
/**
* @acces public.
* @param string $path.
* @throws RuntimeException|InvalidArgumentException.
* @return self.
*/
public function setPath($path) {
$path = rtrim($path, '\\/').DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
if (!mkdir($path, 0770, true)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), could not create thumbnail path "%s" with permissions "0770".', __CLASS__, __FUNCTION__, $path));
}
}
if (!is_dir($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail path "%s" does not exists.', __CLASS__, __FUNCTION__, $path));
}
if (!is_writeable($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail path "%s" is not writeable.', __CLASS__, __FUNCTION__, $path));
}
$this->path = $path;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getPath() {
return $this->path;
}
/**
* @acces public.
* @param string $storage.
* @throws InvalidArgumentException.
* @return self.
*/
public function setStorage($storage) {
if (!is_dir($storage)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail storage "%s" does not exists.', __CLASS__, __FUNCTION__, $storage));
}
$this->storage = rtrim($storage, '\\/').DIRECTORY_SEPARATOR;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getStorage() {
return $this->storage;
}
/**
* @acces public.
* @param string $fallback.
* @throws InvalidArgumentException.
* @return self.
*/
public function setFallback($fallback) {
if (!is_file($fallback)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail fallback "%s" does not exists.', __CLASS__, __FUNCTION__, $fallback));
}
if (!is_readable($fallback)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail fallback "%s" is not readable.', __CLASS__, __FUNCTION__, $fallback));
}
$this->fallback = $fallback;
return $this;
}
/**
* @acces public.
* @throws RuntimeException.
* @return string.
*/
public function getFallback() {
if (null === $this->fallback) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), thumbnail fallback is not specified.', __CLASS__, __FUNCTION__));
}
return $this->fallback;
}
/**
* @acces public.
* @param boolean $cache.
* @return self.
*/
public function setCache($cache) {
$this->cache = $cache;
return $this;
}
/**
* @acces public.
* @return boolean.
*/
public function getCache() {
return $this->cache;
}
/**
* @acces public.
* @param string $hash.
* @throws RuntimeException.
* @return self.
*/
public function setHash($hash) {
if (!function_exists($hash)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), hash function %s does not exists.', __CLASS__, __FUNCTION__, $hash));
}
$this->hash = $hash;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getHash() {
return $this->hash;
}
/**
* @acces public.
* @param array $options.
* @return self.
*/
public function setOptions($options) {
$this->options = array_merge($this->getOptions(), $options);
return $this;
}
/**
* @acces public.
* @return array.
*/
public function getOptions() {
return $this->options;
}
/**
* @acces protected.
* @param string $image.
* @param array $options.
* @return array.
*/
protected function getImage($image, $options = array()) {
$image = $this->getStorage().$image;
if (!is_file($image) || !is_readable($image)) {
$image = $this->getFallback();
$options = array_merge($options, array(
'method' => 'scale',
'flip' => false,
'filter' => false
));
}
$info = pathinfo($image);
return array_merge(array(
'base' => $image,
'mime' => function_exists('mime_content_type') ? mime_content_type($image) : sprintf('image/%s', $info['extension']),
'temp' => $this->getPath().hash($this->getHash(), implode_multidimensional(array_merge(array_slice($info, 1, 1), $options))).'.'.$info['extension']
), $this->getImageSize($image, $options), $info);
return $this;
}
/**
* @acces protected.
* @param string $image.
* @param array $options.
* @return array.
*/
protected function getImageSize($image, $options = array()) {
$size = $newSize = getimagesize($image);
if ($newSize[0] > ($width = $options['width'])) {
$newSize[1] = ceil($newSize[1] / ($newSize[0] / $width));
$newSize[0] = $width;
}
if ($newSize[1] > ($height = $options['height'])) {
$newSize[0] = ceil($newSize[0] / ($newSize[1] / $height));
$newSize[1] = $height;
}
if ('crop' == $options['method']) {
if ($newSize[0] < ($width = $options['width'])) {
$newSize[1] = floor($newSize[1] * ($width / $newSize[0]));
$newSize[0] = $width;
}
if ($newSize[1] < ($height = $options['height'])) {
$newSize[0] = floor($newSize[0] * ($height / $newSize[1]));
$newSize[1] = $height;
}
}
return array(
'width' => $size[0],
'height' => $size[1],
'new_width' => $newSize[0],
'new_height' => $newSize[1],
'margin_top' => ceil(($height - $newSize[1]) / 2),
'margin_left' => ceil(($width - $newSize[0]) / 2)
);
}
/**
* @acces protected.
* @param string $base.
* @param string $temp.
* @return boolean.
*/
protected function isCached($base, $temp) {
return !is_file($temp) || filemtime($base) > filemtime($temp) || !$this->getCache() ? true : false;
}
/**
* @acces protected.
* @param string $image.
* @param string $mime.
* @return resource.
*/
protected function imageCreateFromImage($image, $mime) {
switch ($mime) {
case 'image/gif':
$resource = imagecreatefromgif($image);
break;
case 'image/png':
$resource = imagecreatefrompng($image);
$resource = $this->setOpacity($resource);
break;
default:
$resource = imagecreatefromjpeg($image);
break;
}
return $resource;
}
/**
* @acces protected.
* @param string $resource.
* @param string $mime.
* @param string $temporary.
* @param integer $quality.
* @return resource.
*/
protected function imageCreateSource($resource, $mime, $temporary, $quality = 100) {
if (is_resource($resource)) {
switch ($mime) {
case 'image/gif':
imagegif($resource, $temporary);
break;
case 'image/png':
if (90 < $quality) {
$quality = 9;
} else if (9 > $quality) {
$quality = $quality / 100;
}
imagepng($resource, $temporary, $quality);
break;
default:
if (100 < $quality) {
$quality = 100;
}
imagejpeg($resource, $temporary, $quality);
break;
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @return resource.
*/
protected function setOpacity($resource) {
if (is_resource($resource)) {
imagealphablending($resource, false);
imagefill($resource, 0, 0, imagecolorallocatealpha($resource, 0, 0, 0, 127));
imagesavealpha($resource, true);
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $filter.
* @return resource.
*/
protected function setFlip($resource, $flip) {
if (is_resource($resource) && $flip) {
if ('vertical' === $flip || 'both' === $flip) {
$flip = imagecreatetruecolor(1, $height);
for ($i = floor(($width - 1) / 2); $i >= 0; $i--) {
imagecopy($flip, $resource, 0, 0, $width - $i, 0, 1, $height);
imagecopy($resource, $resource, $width - $i, 0, $i, 0, 1, $height);
imagecopy($resource, $flip, $i, 0, 0, 0, 1, $height);
}
imagedestroy($flip);
}
if ('horizontal' === $flip || 'both' === $flip) {
$flip = imagecreatetruecolor($width, 1);
for ($i = floor(($height - 1) / 2); $i >= 0; $i--) {
imagecopy($flip, $resource, 0, 0, 0, $height - $i, $width, 1);
imagecopy($resource, $resource, 0, $height - $i, 0, $i, $width, 1);
imagecopy($resource, $flip, 0, $i, 0, 0, $width, 1);
}
imagedestroy($flip);
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $filter.
* @return resource.
*/
protected function setFilter($resource, $filters = false) {
if (is_resource($resource) && $filters) {
$avaibleFilters = array(
'negate' => static::NEGATE,
'grayscale' => static::GRAYSCALE,
'brightness' => static::BRIGHTNESS,
'contrast' => static::CONTRAST,
'colorize' => static::COLORIZE,
'edgedetect' => static::EDGEDETECT,
'emboss' => static::EMBOSS,
'gaussian_blur' => static::GAUSSIAN_BLUR,
'selective_blur' => static::SELECTIVE_BLUR,
'removal' => static::REMOVAL,
'smooth' => static::SMOOTH,
'pixelate' => static::PIXELATE
);
if (is_string($filters)) {
$filters = array($filters);
}
foreach ($filters as $key => $value) {
if (is_string($value)) {
$value = array($value);
}
$filter = $value + array(null, null, null, null, null);
if (isset($avaibleFilters[$filter[0]])) {
imagefilter($resource, $avaibleFilters[$filter[0]], $filter[1], $filter[2], $filter[3], $filter[4]);
}
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $background.
* @return self.
*/
protected function setBackground($resource, $background = false) {
if (is_resource($resource) && $background) {
$red = hexdec(substr($background, 1, 2));
$green = hexdec(substr($background, 3, 2));
$blue = hexdec(substr($background, 5, 2));
imagefill($resource, 0, 0, imagecolorallocate($resource, $red, $green, $blue));
}
return $resource;
}
/**
* @acces public.
* @param string $image.
* @param array $options.
* @return string.
*/
public function display($image, $options = array()) {
$options = array_merge($this->getOptions(), $options);
$image = $this->getImage($image, $options);
if ($this->isCached($image['base'], $image['temp'])) {
$thumbnail = imagecreatetruecolor($options['width'], $options['height']);
$resource = $this->imageCreateFromImage($image['base'], $image['mime']);
$resource = $this->setFlip($resource, $options['flip']);
$resource = $this->setFilter($resource, $options['filter']);
$thumbnail = $this->setBackground($thumbnail, $options['background']);
imagecopyresampled($thumbnail, $resource, $image['margin_left'], $image['margin_top'], 0, 0, $image['new_width'], $image['new_height'], $image['width'], $image['height']);
$this->imageCreateSource($thumbnail, $image['mime'], $image['temp'], $options['quality']);
imagedestroy($thumbnail);
imagedestroy($resource);
}
return $image['temp'];
}
}
function implode_multidimensional($array = array(), $glue = ';') {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = sprintf('%s=(%s)', $key, implode_multidimensional($value, '|'));
} else if (is_string($value) || is_integer($value)) {
$array[$key] = sprintf('%s=%s', $key, $value);
}
}
return strtolower(implode($glue, $array));
}
?>
class Thumbnail {
const NEGATE = IMG_FILTER_NEGATE;
const GRAYSCALE = IMG_FILTER_GRAYSCALE;
const BRIGHTNESS = IMG_FILTER_BRIGHTNESS;
const CONTRAST = IMG_FILTER_CONTRAST;
const COLORIZE = IMG_FILTER_COLORIZE;
const EDGEDETECT = IMG_FILTER_EDGEDETECT;
const EMBOSS = IMG_FILTER_EMBOSS;
const GAUSSIAN_BLUR = IMG_FILTER_GAUSSIAN_BLUR;
const SELECTIVE_BLUR = IMG_FILTER_SELECTIVE_BLUR;
const REMOVAL = IMG_FILTER_MEAN_REMOVAL;
const SMOOTH = IMG_FILTER_SMOOTH;
const PIXELATE = IMG_FILTER_PIXELATE;
/**
* @acces protected.
* @var string.
*/
protected $path = '';
/**
* @acces protected.
* @var string.
*/
protected $storage = '';
/**
* @acces protected.
* @var string.
*/
protected $fallback = null;
/**
* @acces protected.
* @var boolean.
*/
protected $cache = true;
/**
* @acces protected.
* @var string.
*/
protected $hash = 'md5';
/**
* @acces public.
* @var array.
*/
protected $options = array(
'method' => 'crop',
'width' => '100',
'height' => '100',
'quality' => 100,
'flip' => false,
'filter' => false,
'background' => '#ffffff'
);
/**
* @acces public.
* @param array $options.
* @throws InvalidArgumentException.
*/
public function __construct($options = array()) {
if (!is_array($options)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), options is not an array; recieved "%s".', __CLASS__, __FUNCTION__, gettype($options)));
}
if (isset($options['path'])) {
$this->setPath($options['path']);
}
if (isset($options['storage'])) {
$this->setStorage($options['storage']);
}
if (isset($options['fallback'])) {
$this->setFallback($options['fallback']);
}
if (isset($options['cache'])) {
$this->setCache($options['cache']);
}
if (isset($options['hash'])) {
$this->setHash($options['hash']);
}
if (isset($options['options'])) {
$this->setOptions($options['options']);
}
}
/**
* @acces public.
* @param string $path.
* @throws RuntimeException|InvalidArgumentException.
* @return self.
*/
public function setPath($path) {
$path = rtrim($path, '\\/').DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
if (!mkdir($path, 0770, true)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), could not create thumbnail path "%s" with permissions "0770".', __CLASS__, __FUNCTION__, $path));
}
}
if (!is_dir($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail path "%s" does not exists.', __CLASS__, __FUNCTION__, $path));
}
if (!is_writeable($path)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail path "%s" is not writeable.', __CLASS__, __FUNCTION__, $path));
}
$this->path = $path;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getPath() {
return $this->path;
}
/**
* @acces public.
* @param string $storage.
* @throws InvalidArgumentException.
* @return self.
*/
public function setStorage($storage) {
if (!is_dir($storage)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail storage "%s" does not exists.', __CLASS__, __FUNCTION__, $storage));
}
$this->storage = rtrim($storage, '\\/').DIRECTORY_SEPARATOR;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getStorage() {
return $this->storage;
}
/**
* @acces public.
* @param string $fallback.
* @throws InvalidArgumentException.
* @return self.
*/
public function setFallback($fallback) {
if (!is_file($fallback)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail fallback "%s" does not exists.', __CLASS__, __FUNCTION__, $fallback));
}
if (!is_readable($fallback)) {
throw new Exception\InvalidArgumentException(sprintf('Failed to initialize %s (%s), thumbnail fallback "%s" is not readable.', __CLASS__, __FUNCTION__, $fallback));
}
$this->fallback = $fallback;
return $this;
}
/**
* @acces public.
* @throws RuntimeException.
* @return string.
*/
public function getFallback() {
if (null === $this->fallback) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), thumbnail fallback is not specified.', __CLASS__, __FUNCTION__));
}
return $this->fallback;
}
/**
* @acces public.
* @param boolean $cache.
* @return self.
*/
public function setCache($cache) {
$this->cache = $cache;
return $this;
}
/**
* @acces public.
* @return boolean.
*/
public function getCache() {
return $this->cache;
}
/**
* @acces public.
* @param string $hash.
* @throws RuntimeException.
* @return self.
*/
public function setHash($hash) {
if (!function_exists($hash)) {
throw new Exception\RuntimeException(sprintf('Failed to initialize %s (%s), hash function %s does not exists.', __CLASS__, __FUNCTION__, $hash));
}
$this->hash = $hash;
return $this;
}
/**
* @acces public.
* @return string.
*/
public function getHash() {
return $this->hash;
}
/**
* @acces public.
* @param array $options.
* @return self.
*/
public function setOptions($options) {
$this->options = array_merge($this->getOptions(), $options);
return $this;
}
/**
* @acces public.
* @return array.
*/
public function getOptions() {
return $this->options;
}
/**
* @acces protected.
* @param string $image.
* @param array $options.
* @return array.
*/
protected function getImage($image, $options = array()) {
$image = $this->getStorage().$image;
if (!is_file($image) || !is_readable($image)) {
$image = $this->getFallback();
$options = array_merge($options, array(
'method' => 'scale',
'flip' => false,
'filter' => false
));
}
$info = pathinfo($image);
return array_merge(array(
'base' => $image,
'mime' => function_exists('mime_content_type') ? mime_content_type($image) : sprintf('image/%s', $info['extension']),
'temp' => $this->getPath().hash($this->getHash(), implode_multidimensional(array_merge(array_slice($info, 1, 1), $options))).'.'.$info['extension']
), $this->getImageSize($image, $options), $info);
return $this;
}
/**
* @acces protected.
* @param string $image.
* @param array $options.
* @return array.
*/
protected function getImageSize($image, $options = array()) {
$size = $newSize = getimagesize($image);
if ($newSize[0] > ($width = $options['width'])) {
$newSize[1] = ceil($newSize[1] / ($newSize[0] / $width));
$newSize[0] = $width;
}
if ($newSize[1] > ($height = $options['height'])) {
$newSize[0] = ceil($newSize[0] / ($newSize[1] / $height));
$newSize[1] = $height;
}
if ('crop' == $options['method']) {
if ($newSize[0] < ($width = $options['width'])) {
$newSize[1] = floor($newSize[1] * ($width / $newSize[0]));
$newSize[0] = $width;
}
if ($newSize[1] < ($height = $options['height'])) {
$newSize[0] = floor($newSize[0] * ($height / $newSize[1]));
$newSize[1] = $height;
}
}
return array(
'width' => $size[0],
'height' => $size[1],
'new_width' => $newSize[0],
'new_height' => $newSize[1],
'margin_top' => ceil(($height - $newSize[1]) / 2),
'margin_left' => ceil(($width - $newSize[0]) / 2)
);
}
/**
* @acces protected.
* @param string $base.
* @param string $temp.
* @return boolean.
*/
protected function isCached($base, $temp) {
return !is_file($temp) || filemtime($base) > filemtime($temp) || !$this->getCache() ? true : false;
}
/**
* @acces protected.
* @param string $image.
* @param string $mime.
* @return resource.
*/
protected function imageCreateFromImage($image, $mime) {
switch ($mime) {
case 'image/gif':
$resource = imagecreatefromgif($image);
break;
case 'image/png':
$resource = imagecreatefrompng($image);
$resource = $this->setOpacity($resource);
break;
default:
$resource = imagecreatefromjpeg($image);
break;
}
return $resource;
}
/**
* @acces protected.
* @param string $resource.
* @param string $mime.
* @param string $temporary.
* @param integer $quality.
* @return resource.
*/
protected function imageCreateSource($resource, $mime, $temporary, $quality = 100) {
if (is_resource($resource)) {
switch ($mime) {
case 'image/gif':
imagegif($resource, $temporary);
break;
case 'image/png':
if (90 < $quality) {
$quality = 9;
} else if (9 > $quality) {
$quality = $quality / 100;
}
imagepng($resource, $temporary, $quality);
break;
default:
if (100 < $quality) {
$quality = 100;
}
imagejpeg($resource, $temporary, $quality);
break;
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @return resource.
*/
protected function setOpacity($resource) {
if (is_resource($resource)) {
imagealphablending($resource, false);
imagefill($resource, 0, 0, imagecolorallocatealpha($resource, 0, 0, 0, 127));
imagesavealpha($resource, true);
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $filter.
* @return resource.
*/
protected function setFlip($resource, $flip) {
if (is_resource($resource) && $flip) {
if ('vertical' === $flip || 'both' === $flip) {
$flip = imagecreatetruecolor(1, $height);
for ($i = floor(($width - 1) / 2); $i >= 0; $i--) {
imagecopy($flip, $resource, 0, 0, $width - $i, 0, 1, $height);
imagecopy($resource, $resource, $width - $i, 0, $i, 0, 1, $height);
imagecopy($resource, $flip, $i, 0, 0, 0, 1, $height);
}
imagedestroy($flip);
}
if ('horizontal' === $flip || 'both' === $flip) {
$flip = imagecreatetruecolor($width, 1);
for ($i = floor(($height - 1) / 2); $i >= 0; $i--) {
imagecopy($flip, $resource, 0, 0, 0, $height - $i, $width, 1);
imagecopy($resource, $resource, 0, $height - $i, 0, $i, $width, 1);
imagecopy($resource, $flip, 0, $i, 0, 0, $width, 1);
}
imagedestroy($flip);
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $filter.
* @return resource.
*/
protected function setFilter($resource, $filters = false) {
if (is_resource($resource) && $filters) {
$avaibleFilters = array(
'negate' => static::NEGATE,
'grayscale' => static::GRAYSCALE,
'brightness' => static::BRIGHTNESS,
'contrast' => static::CONTRAST,
'colorize' => static::COLORIZE,
'edgedetect' => static::EDGEDETECT,
'emboss' => static::EMBOSS,
'gaussian_blur' => static::GAUSSIAN_BLUR,
'selective_blur' => static::SELECTIVE_BLUR,
'removal' => static::REMOVAL,
'smooth' => static::SMOOTH,
'pixelate' => static::PIXELATE
);
if (is_string($filters)) {
$filters = array($filters);
}
foreach ($filters as $key => $value) {
if (is_string($value)) {
$value = array($value);
}
$filter = $value + array(null, null, null, null, null);
if (isset($avaibleFilters[$filter[0]])) {
imagefilter($resource, $avaibleFilters[$filter[0]], $filter[1], $filter[2], $filter[3], $filter[4]);
}
}
}
return $resource;
}
/**
* @acces protected.
* @param resource $resource.
* @param mixed $background.
* @return self.
*/
protected function setBackground($resource, $background = false) {
if (is_resource($resource) && $background) {
$red = hexdec(substr($background, 1, 2));
$green = hexdec(substr($background, 3, 2));
$blue = hexdec(substr($background, 5, 2));
imagefill($resource, 0, 0, imagecolorallocate($resource, $red, $green, $blue));
}
return $resource;
}
/**
* @acces public.
* @param string $image.
* @param array $options.
* @return string.
*/
public function display($image, $options = array()) {
$options = array_merge($this->getOptions(), $options);
$image = $this->getImage($image, $options);
if ($this->isCached($image['base'], $image['temp'])) {
$thumbnail = imagecreatetruecolor($options['width'], $options['height']);
$resource = $this->imageCreateFromImage($image['base'], $image['mime']);
$resource = $this->setFlip($resource, $options['flip']);
$resource = $this->setFilter($resource, $options['filter']);
$thumbnail = $this->setBackground($thumbnail, $options['background']);
imagecopyresampled($thumbnail, $resource, $image['margin_left'], $image['margin_top'], 0, 0, $image['new_width'], $image['new_height'], $image['width'], $image['height']);
$this->imageCreateSource($thumbnail, $image['mime'], $image['temp'], $options['quality']);
imagedestroy($thumbnail);
imagedestroy($resource);
}
return $image['temp'];
}
}
function implode_multidimensional($array = array(), $glue = ';') {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = sprintf('%s=(%s)', $key, implode_multidimensional($value, '|'));
} else if (is_string($value) || is_integer($value)) {
$array[$key] = sprintf('%s=%s', $key, $value);
}
}
return strtolower(implode($glue, $array));
}
?>
Gewijzigd op 05/05/2013 15:24:43 door Joakim Broden
Eerste wat me opvalt is waarom protected ipv private?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// standaard thumbnail:
$image = '/pad/naar/het/plaatje.jpg';
$thumb = new Thumbnail($image); // standaard afmetingen 200 x 200 pixels
$thumb->generate();
// thumbnail met afwijkende afmetingen:
$image2 = '/pad/naar/het/plaatje2.jpg';
$thumb2 = new Thumbnail($image, 150, 150); // afwijkende afmetingen: 150 x 150 pixels
$thumb2->generate();
// thumbnail met afwijkende achtergrond:
$image3 = '/pad/naar/het/plaatje3.jpg';
$thumb3 = new Thumbnail($image);
$thimb3->changeBackground('#000'); // zwarte achtergrond
$thumb3->generate();
?>
// standaard thumbnail:
$image = '/pad/naar/het/plaatje.jpg';
$thumb = new Thumbnail($image); // standaard afmetingen 200 x 200 pixels
$thumb->generate();
// thumbnail met afwijkende afmetingen:
$image2 = '/pad/naar/het/plaatje2.jpg';
$thumb2 = new Thumbnail($image, 150, 150); // afwijkende afmetingen: 150 x 150 pixels
$thumb2->generate();
// thumbnail met afwijkende achtergrond:
$image3 = '/pad/naar/het/plaatje3.jpg';
$thumb3 = new Thumbnail($image);
$thimb3->changeBackground('#000'); // zwarte achtergrond
$thumb3->generate();
?>
Volgens mij werkt dit een stuk prettiger.
Gewijzigd op 05/05/2013 16:07:35 door Ozzie PHP
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$thumbnail = new Thumbnail();
$thumbnail->display('/pad/naar/het/plaatje.jpg'); // Default options
$thumbnail->display('/pad/naar/het/plaatje2.jpg', array(
'width' => 200,
'height' => 200
)); // Formaat van 200 x 200 met default options
?>
$thumbnail = new Thumbnail();
$thumbnail->display('/pad/naar/het/plaatje.jpg'); // Default options
$thumbnail->display('/pad/naar/het/plaatje2.jpg', array(
'width' => 200,
'height' => 200
)); // Formaat van 200 x 200 met default options
?>
Gewijzigd op 05/05/2013 17:00:42 door Joakim Broden
Dit kan
Code (php)
1
2
3
4
5
2
3
4
5
<?php
$thumbnail->display('/pad/naar/het/plaatje2.jpg', array(
'width' => 200,
'height' => 200));
?>
$thumbnail->display('/pad/naar/het/plaatje2.jpg', array(
'width' => 200,
'height' => 200));
?>
Maar waarom niet zo?
$thumbnail->display('/pad/naar/het/plaatje2.jpg', 200, 200);
Ook vraag ik me af of het nodig is om een display() functie te hebben.
Kun je niet gewoon de thumb weergeven en zorgen dat er altijd een thumbnail aanwezig is?
<img src="http://www.jouwsite.nl/thumb2.jpg">
Nu moet je iedere keer een functie aanroepen om een thumb te tonen. Wat nu als je een overzichtspagina hebt met 200 thumbs? Dat is niet bepaald lekker voor je performance.
2 ) Hoe wil je anders de thumbnails maken? Je zult toch ergens de opdracht moeten geven tot het maken van een thumbnail. Vandaar de display() functie, maar deze word niet altijd getoond, alleen als de thumbnail nog gemaakt moet worden. Bestaat de thumbnail al? Dan word alleen de path van de thumbnail terug gegeven.
Klopt ik weet dat je me probeert te helpen ;-) ik probeer alleen men keuzes toe te lichten. ;-)
Jawel, maar ik denk dat je het allemaal te "zwaar" aan het maken bent. Je roept een display functie aan (dat is al 1 aanroep) en die roept weer een isCached functie aan waarin 3 controles worden uitgevoerd. Dit is killing voor je performance lijkt mij. Maak dan 1 functie die controleert of er een cachefile is en die die dan ook teruggeeft. Is de file er niet, dan de file genereren.
Zelf zie ik niet heel veel verkeerds. Behalve dat hij teveel dingen doet, cachen hoort naar mijn mening door een andere klasse gedaan te worden. Evenals het ophalen en opslaan.
En vervolgens zou ik het decorator pattern gebruiken: