-
Notifications
You must be signed in to change notification settings - Fork 1
/
imthumb.class.php
787 lines (649 loc) · 22 KB
/
imthumb.class.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<?php
/**
* Main image generation class
*
* @package ImThumb
* @author Sam Pospischil <[email protected]>
* @since 2013-10-18
*/
if (!defined('IMTHUMB_BASE')) {
define('IMTHUMB_BASE', dirname(__FILE__));
}
class ImThumbException extends Exception {}
class ImThumbCriticalException extends ImThumbException {}
class ImThumbNotFoundException extends ImThumbException {}
class ImThumb
{
const VERSION = 1.1;
const ERR_SERVER_CONFIG = 1; // exception codes
const ERR_SRC_IMAGE = 2;
const ERR_OUTPUTTING = 3;
const ERR_CACHE = 4;
const ERR_FILTER = 5;
const ERR_CONFIGURATION = 6;
const ERR_RATE_EXCEEDED = 7;
const ERR_HACK_ATTEMPT = 8;
public static $HAS_MBSTRING; // used for reliable image length determination. Initialised below class def'n.
public static $MBSTRING_SHADOW;
const DEFAULT_SIZE = 100; // if invalid dimensions are passed, this will be used
//--------------------------------------------------------------------------
// Initialisation & configuration
public static $defaultImageSource = 'ImThumbSource_Local';
private $params;
public $meta; // ImThumbMeta instance
private $imageHandle = null;
public $cache; // ImThumbCache instance
public $sourceHandler; // ImThumbSource instance
private $startTime; // stats
private $startCPU;
public function __construct(Array $params = null)
{
if (!class_exists('Imagick')) {
throw new ImThumbException("Could not load ImThumb: ImageMagick is not installed. Please contact your webhost and ask them to install the ImageMagick library", self::ERR_SERVER_CONFIG);
}
$this->startTime = microtime(true);
if (defined('MEMORY_LIMIT') && MEMORY_LIMIT) {
ini_set('memory_limit', MEMORY_LIMIT);
}
ksort($params); // :NOTE: order the parameters to get consistent cache filenames, as this is factored into filename hashes
$this->params = $params;
// store initial CPU stats if we are debugging
if ($this->param('debug')) {
$data = getrusage();
$this->startCPU = array((double)($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000), (double)($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000));
}
// set default width / height if none provided
if (!$this->param('width') && !$this->param('height') && !$this->params('cropRect')) {
$this->params['width'] = $this->params['height'] = self::DEFAULT_SIZE;
}
}
public function __destruct()
{
if ($this->imageHandle) {
$this->imageHandle->destroy();
unset($this->imageHandle);
}
}
// -- simple accessors --
public function param($name, $val = null)
{
if (isset($val)) {
$this->params[$name] = $val;
return;
}
return isset($this->params[$name]) ? $this->params[$name] : null;
}
public function params()
{
return $this->params;
}
public function isValid()
{
if (!isset($this->meta)) {
return false;
}
return $this->meta->valid;
}
public function getMime()
{
if (!isset($this->meta)) {
return null;
}
return $this->meta->mimeType;
}
public function getSrc()
{
return $this->meta->src;
}
/**
* Forcibly set the source image for generation, even if it lies outside of the configured
* base directory. Not to be connected in any way to user input as this is an obvious security point.
* Primarily used by source handlers to override directory permissions when caching their assets elsewhere.
*/
public function forceSrc($filePath)
{
$this->param('src', $filePath);
$this->param('baseDir', dirname($filePath));
}
public function getImagick()
{
return $this->imageHandle;
}
public function getTimingStats()
{
return array(
$this->startTime,
$this->startCPU[0],
$this->startCPU[1]
);
}
// -- simple mutators --
// reset all parameters related to externally configurable image output
public function resetImageParams()
{
unset(
$this->params['src'],
$this->params['width'],
$this->params['height'],
$this->params['quality'],
$this->params['align'],
$this->params['cropMode'],
$this->params['sharpen'],
$this->params['canvasColor'],
$this->params['canvasTransparent'],
$this->params['filters'],
$this->params['cropRect'],
$this->params['jpgProgressive']
);
$this->params['width'] = self::DEFAULT_SIZE;
$this->params['height'] = self::DEFAULT_SIZE;
}
public function configureUnalteredFitImage()
{
$prevW = $this->param('width');
$prevH = $this->param('height');
$prevQ = $this->param('quality');
$this->resetImageParams();
$this->params['width'] = $prevW;
$this->params['height'] = $prevH;
$this->params['quality'] = $prevQ;
$this->params['cropMode'] = 2;
$this->params['canvasTransparent'] = 1;
}
//--------------------------------------------------------------------------
// Loading
public function loadImage($src = null)
{
if ($src === null) {
$src = $this->param('src');
}
// prep the cache
$cacheOk = $this->initCache();
// find appropriate handler for this image
if (!$this->determineImageSource($src)) {
// fallback to default source handling (local filesystem unless overridden)
$this->setDefaultSourceHandler();
}
// read image metadata
try {
$this->meta = $this->sourceHandler->readMetadata($src, $this);
$this->meta->validateWith($this);
} catch (ImThumbNotFoundException $e) {
$this->loadFallbackImage();
$this->doResize();
return;
}
// check the cache and flag for generation where necessary
if ($cacheOk) {
// cache ftw. load it up
$this->cache->load($this);
// check cache time against file time.
if ($this->meta->mtime > $this->cache->mtime) {
// if cache has been invalidated, flag it as empty so we regenerate on our next call to writeCache()
$this->cache->isCached(false);
} else {
// otherwise we have no need to do anything else, cached image is already loaded
return;
}
}
// load the source image into memory
$this->imageHandle = $this->sourceHandler->readResource($this->meta, $this);
// load up the fallback image if we couldn't find the source
if (!$this->imageHandle) {
$this->loadFallbackImage();
}
// process the action
$this->doResize();
}
public function loadFallbackImage()
{
list($w, $h) = $this->getTargetSize();
return $this->loadFallbackImgOrColor($this->param('fallbackImg'), $this->param('fallbackColor'), $w, $h);
}
public function loadErrorImage()
{
list($w, $h) = $this->getTargetSize();
return $this->loadFallbackImgOrColor($this->param('errorImg'), $this->param('errorColor'), $w, $h);
}
private function loadFallbackImgOrColor($imagePath, $fallbackColor, $fallbackW = 32, $fallbackH = 32)
{
$this->imageHandle = new Imagick();
if (is_string($imagePath)) {
if (!$this->imageHandle->readImage($imagePath)) {
// throw exception if the configured fallback is incorrect
throw new ImThumbException("Cannot display error image", self::ERR_CONFIGURATION);
}
$this->setDefaultSourceHandler();
$this->meta = $this->sourceHandler->readMetadata($imagePath, $this);
$this->meta->valid = false;
return true;
}
if ($imagePath === false) {
return false;
}
if (!$fallbackW) {
$fallbackW = self::DEFAULT_SIZE;
}
if (!$fallbackH) {
$fallbackH = self::DEFAULT_SIZE;
}
// not configured.. show coloured square
$this->imageHandle->newImage($fallbackW, $fallbackH, new ImagickPixel($fallbackColor));
$this->imageHandle->setImageFormat('jpg');
$this->meta->mimeType = 'image/jpeg';
return false;
}
protected function determineImageSource($src)
{
if ($handlerClass = $this->checkExternalSource($src)) {
$this->sourceHandler = new $handlerClass();
return true;
}
return false;
}
protected function setDefaultSourceHandler()
{
if (!self::loadSourceHandler(self::$defaultImageSource)) {
throw new ImThumbCriticalException('Could not load default source handler! Is ImThumb installed correctly?', self::ERR_SERVER_CONFIG);
}
$hClass = self::$defaultImageSource; // oldPHP
$this->sourceHandler = new $hClass();
}
//--------------------------------------------------------------------------
// Generation
public function doResize()
{
if (!$this->isValid()) {
// force showing the entire image, unaltered, if we are displaying a fallback
$this->configureUnalteredFitImage();
}
// check if the image is OK to be handled first
try {
$isValid = $this->imageHandle->valid();
} catch (ImagickException $e) {
// :NOTE: this is a non-critical error because its main symptom is disabling
// generated fallback images and so we need to jump out
return false;
}
// set the image output format based on input
// :TODO: this needs to be separate when converting non-image mimetypes
$this->meta->assignMimeType($this->imageHandle);
// each filetype needs to have some extra handling done
$isGIF = strpos($this->meta->mimeType, 'gif') !== false;
$isJPEG = strpos($this->meta->mimeType, 'jpeg') !== false;
$isPNG = strpos($this->meta->mimeType, 'png') !== false;
// are we dealing with transparency?
$canvas_trans = (bool)$this->param('canvasTransparent') && ($isPNG || $isGIF);
$canvas_color = $this->param('canvasColor');
list($new_width, $new_height) = $this->getTargetSize();
// run crop / resize operation
if ($this->param('cropRect')) {
// Explicitly set crop coordinates.
// If width and height are provided, the resulting image is adjusted to fit within those dimensions as normal.
@list($startX, $startY, $endX, $endY) = explode(',', $this->param('cropRect'));
$this->processResizeCrop($new_width, $new_height, $startX, $startY, $endX, $endY,
(int)$this->param('cropMode'), $this->param('align'),
(bool)$this->param('sharpen'), $canvas_trans, $canvas_color);
} else {
// TimThumb style simplified crop operation
$this->processZoomCrop($new_width, $new_height,
(int)$this->param('cropMode'), $this->param('align'),
(bool)$this->param('sharpen'), $canvas_trans, $canvas_color);
}
// process any configured image filters
if ($this->param('filters')) {
$this->runFilters($this->param('filters'));
}
// GIFs need final page dimensions processed or they retain original image canvas size
if ($isGIF) {
$this->imageHandle->setImagePage($new_width, $new_height, 0, 0);
}
// set progressive JPEG if desired
if ($isJPEG && $this->param('jpgProgressive')) {
$this->imageHandle->setInterlaceScheme(Imagick::INTERLACE_PLANE);
}
// set background colour if PNG transparency is disabled
if ($isPNG && $canvas_trans && !$this->param('pngTransparency')) {
$canvas = $this->generateNewCanvas($new_width, $new_height, $canvas_color, $this->meta->mimeType);
$canvas->compositeImage($this->imageHandle, Imagick::COMPOSITE_OVER, 0, 0);
$this->imageHandle = $canvas;
}
$this->compress();
return true;
}
protected function generateNewCanvas($new_width, $new_height, $bgColor = null, $mimeType = 'image/jpeg')
{
$canvas = new Imagick();
$canvas->newImage($new_width, $new_height, new ImagickPixel($bgColor ? '#' . $bgColor : 'transparent'));
$canvas->setImageFormat(str_replace('image/', '', $mimeType));
return $canvas;
}
protected function processZoomCrop($new_width, $new_height, $zoom_crop = 3, $align = 'c', $sharpen = false, $canvas_trans = true, $canvas_color = 'ffffff')
{
list($width, $height, $new_width, $new_height) = $this->getSourceAndTargetDims($new_width, $new_height);
// check for upscaling
if (!$this->param('upscale')) {
if ($width < $new_width) {
$new_width = $width;
}
if ($height < $new_height) {
$new_height = $height;
}
}
if ($width == $new_width && $height == $new_height) {
// already the correct size
return;
}
// perform requested cropping
switch ($zoom_crop) {
case 3: // inner-fit
$new_width = min($new_width, $width);
$new_height = min($new_height, $height);
$this->imageHandle->resizeImage($new_width, $new_height, Imagick::FILTER_LANCZOS, $sharpen ? 0.7 : 1, true);
break;
case 2: // inner-fill
$this->imageHandle->resizeImage($new_width, $new_height, Imagick::FILTER_LANCZOS, $sharpen ? 0.7 : 1, true);
$canvas = $this->generateNewCanvas($new_width, $new_height, $canvas_trans ? null : $canvas_color, $this->meta->mimeType);
$xOffset = ($new_width - $this->imageHandle->getImageWidth()) / 2;
$yOffset = ($new_height - $this->imageHandle->getImageHeight()) / 2;
$canvas->compositeImage($this->imageHandle, Imagick::COMPOSITE_OVER, $xOffset, $yOffset);
$this->imageHandle = $canvas;
break;
case 1: // outer-fill
// resize first to set scale as necessary
$final_height = $height * ($new_width / $width);
if ($final_height > $new_height) {
$ratio = $new_width / $width;
} else {
$ratio = $new_height / $height;
}
$tempW = $width * $ratio;
$tempH = $height * $ratio;
$this->imageHandle->resizeImage($tempW, $tempH, Imagick::FILTER_LANCZOS, $sharpen ? 0.7 : 1);
list($x, $y) = $this->getCropCoords($align, $tempW, $tempH, $new_width, $new_height);
$this->imageHandle->cropImage($new_width, $new_height, $x, $y);
break;
default: // exact dimensions
$this->imageHandle->resizeImage($new_width, $new_height, Imagick::FILTER_LANCZOS, $sharpen ? 0.7 : 1);
break;
}
}
protected function processResizeCrop($new_width, $new_height, $startX, $startY, $endX, $endY, $zoom_crop = 3, $align = 'c', $sharpen = false, $canvas_trans = true, $canvas_color = 'ffffff')
{
list($width, $height, $new_width, $new_height) = $this->getSourceAndTargetDims($new_width, $new_height);
if ($startX < 0 || $startY < 0 || $endX > $width || $endY > $height) {
// if crop coords are outside of original image, we will need to add a background
$cropW = abs($endX - $startX);
$cropH = abs($endY - $startY);
$canvas = $this->generateNewCanvas($cropW, $cropH, $canvas_trans ? null : $canvas_color, $this->meta->mimeType);
$xOffset = $startX < 0 ? abs($startX) : 0;
$yOffset = $startY < 0 ? abs($startY) : 0;
$startX = max($startX, 0);
$endX = min($endX, $width);
$startY = max($startY, 0);
$endY = min($endY, $height);
$this->imageHandle->cropImage(abs($endX - $startX), abs($endY - $startY), $startX, $startY);
$canvas->compositeImage($this->imageHandle, Imagick::COMPOSITE_OVER, $xOffset, $yOffset);
$this->imageHandle = $canvas;
} else {
// otherwise we can just crop
$this->imageHandle->cropImage(abs($endX - $startX), abs($endY - $startY), $startX, $startY);
}
// handle the adjustments for W/H inside the target dimensions if the cropped area is not the size of the specified output
$this->processZoomCrop($new_width, $new_height, $zoom_crop, $align, $sharpen, $canvas_trans, $canvas_color);
}
protected function runFilters($filters)
{
if (!class_exists('ImThumbFilters')) {
require_once(IMTHUMB_BASE . '/imthumb-filters.php');
}
$filterHandler = new ImThumbFilters($this->imageHandle);
$filters = explode('|', $filters);
foreach ($filters as &$filterArgs) {
$filterArgs = explode(',', $filterArgs);
$filterName = trim(array_shift($filterArgs));
try {
if (is_numeric($filterName)) {
// process TimThumb filters
$filterHandler->timthumbFilter($filterName, $filterArgs);
} else {
// process Imagick filters
call_user_func_array(array($filterHandler, $filterName), $filterArgs);
}
} catch (ImThumbException $e) {
throw new ImThumbException("Problem running filter '{$filterName}': " . $e->getMessage());
} catch (ImagickException $e) {
throw new ImThumbException("Error in filter '{$filterName}': " . $e->getMessage());
}
}
}
protected function compress()
{
if (strpos($this->meta->mimeType, 'jpeg') !== false) {
if ($this->param('quality') == 100) {
$this->imageHandle->setImageCompression(Imagick::COMPRESSION_LOSSLESSJPEG);
} else {
$this->imageHandle->setImageCompression(Imagick::COMPRESSION_JPEG);
}
} else if (strpos($this->meta->mimeType, 'png') !== false) {
$this->imageHandle->setImageCompression(Imagick::COMPRESSION_ZIP);
}
$this->imageHandle->setImageCompressionQuality((double)$this->param('quality'));
$this->imageHandle->stripImage();
}
//--------------------------------------------------------------------------
public function getTargetSize()
{
$w = $this->param('width');
$h = $this->param('height');
if (!$w && !$h && $this->param('cropRect')) {
// if there are no target dimensions but there is a crop rect, use that size
@list($startX, $startY, $endX, $endY) = explode(',', $this->param('cropRect'));
$w = min(abs($endX - $startX), $this->param('maxw'));
$h = min(abs($endY - $startY), $this->param('maxh'));
} else {
$w = min(abs($w), $this->param('maxw'));
$h = min(abs($h), $this->param('maxh'));
}
if (!$w && !$h) {
$w = $h = self::DEFAULT_SIZE;
}
return array($w, $h);
}
protected function getSourceAndTargetDims($new_width, $new_height)
{
// Get original width and height
$width = $this->imageHandle->getImageWidth();
$height = $this->imageHandle->getImageHeight();
// generate new w/h if not provided
if ($new_width && !$new_height) {
$new_height = floor($height * ($new_width / $width));
} else if ($new_height && !$new_width) {
$new_width = floor($width * ($new_height / $height));
}
if (!$width && !$height) {
$width = $height = self::DEFAULT_SIZE;
}
return array($width, $height, $new_width, $new_height);
}
protected function getCropCoords($align, $origW, $origH, $destW, $destH)
{
// read alignment for crop operation
if (!$align || $align == 'c') {
$gravity = 'center';
} else if ($align == 'l') {
$gravity = 'west';
} else if ($align == 'r') {
$gravity = 'east';
} else {
$gravity = '';
if (strpos($align, 't') !== false) {
$gravity .= 'north';
}
if (strpos($align, 'b') !== false) {
$gravity .= 'south';
}
if (strpos($align, 'l') !== false) {
$gravity .= 'west';
}
if (strpos($align, 'r') !== false) {
$gravity .= 'east';
}
}
// :NOTE: ImageMagick gravity doesn't work with cropping. A shame.
$x = $y = 0;
switch ($gravity) {
case 'center':
$x = ($origW - $destW) / 2;
$y = ($origH - $destH) / 2;
break;
case 'northwest':
break;
case 'north':
$x = ($origW - $destW) / 2;
break;
case 'northeast':
$x = $origW - $destW;
break;
case 'west':
$y = ($origH - $destH) / 2;
break;
case 'east':
$x = $origW - $destW;
$y = ($origH - $destH) / 2;
break;
case 'southwest':
$x = 0;
$y = $origH - $destH;
break;
case 'south':
$x = ($origW - $destW) / 2;
$y = $origH - $destH;
break;
case 'southeast':
$x = $origW - $destW;
$y = $origH - $destH;
break;
}
return array($x, $y);
}
//--------------------------------------------------------------------------
// Output
public function getImage()
{
$cachedImage = $this->cache ? $this->cache->getImage($this) : false;
if ($cachedImage) {
return $cachedImage;
}
try {
return $this->imageHandle->getImageBlob();
} catch (ImagickException $e) {
return false;
}
}
public function getImageSize()
{
$data = $this->getImage();
if (self::$HAS_MBSTRING && (self::$MBSTRING_SHADOW & 2)) {
return mb_strlen($data, 'latin1');
} else {
return strlen($data);
}
}
//--------------------------------------------------------------------------
// Caching
public function initCache()
{
if (!($cachePath = $this->param('cache'))) {
return false;
}
if (!class_exists('ImThumbCache')) {
require_once(IMTHUMB_BASE . '/imthumb-cache.class.php');
}
$this->cache = new ImThumbCache($cachePath, array(
'cacheCleanPeriod' => $this->param('cacheCleanPeriod'),
'cacheMaxAge' => $this->param('cacheMaxAge'),
'cachePrefix' => $this->param('cachePrefix'),
'cacheSalt' => $this->param('cacheSalt'),
'cacheSuffix' => $this->param('cacheSuffix'),
'cacheFilenameFormat' => $this->param('cacheFilenameFormat'),
));
return true;
}
public function getCache()
{
return $this->cache;
}
public function writeCache()
{
if (!$this->cache) {
return false;
}
return $this->cache->write($this);
}
public function hasbrowserCache($modifiedSince = null)
{
if (!$modifiedSince) {
$modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
}
if (!$this->meta || $this->meta->mtime <= 0 || !$this->param('doBrowserCache') || empty($modifiedSince)) {
return false;
}
if (!is_numeric($modifiedSince)) {
$modifiedSince = strtotime($modifiedSince);
}
return $modifiedSince >= $this->meta->mtime;
}
//--------------------------------------------------------------------------
// External assets
public function checkExternalSource($src)
{
// no whitelist or global permission, can't do it
if (!$whitelist = $this->params['sourceHandlers']) {
return false;
}
// process all URI matches & return an appropriate handler if one is found
foreach ($whitelist as $uri => $handlerClass) {
if (preg_match($uri, $src)) {
if (!self::loadSourceHandler($handlerClass)) {
throw new ImThumbCriticalException('Could not load source handler \'' . $handlerClass . '\'', self::ERR_SERVER_CONFIG);
}
return $handlerClass;
}
}
return false;
}
public static function loadSourceHandler($class)
{
if (class_exists($class)) {
return true;
}
$tryFile = '/imthumb-source-' . strtolower(str_replace('ImThumbSource_', '', $class)) . '.class.php';
if (file_exists(IMTHUMB_BASE . $tryFile)) {
require_once(IMTHUMB_BASE . $tryFile);
return true;
}
if ($this->params['extraSourceHandlerPath'] && file_exists($this->params['extraSourceHandlerPath'] . $tryFile)) {
require_once($this->params['extraSourceHandlerPath'] . $tryFile);
return true;
}
return false;
}
//--------------------------------------------------------------------------
// Rate limiting
/**
* @return true if rate is OK, false if exceeded
*/
public function checkRateLimits()
{
if ($limiter = $this->param('rateLimiter')) {
$limiter->setGenerator($this);
if (!$limiter->checkRateLimits()) {
return false;
}
}
return true;
}
}
ImThumb::$HAS_MBSTRING = extension_loaded('mbstring');
ImThumb::$MBSTRING_SHADOW = (int)ini_get('mbstring.func_overload');