/
slir.class.php
1508 lines (1351 loc) · 36.6 KB
/
slir.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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class definition file for SLIR (Smart Lencioni Image Resizer)
*
* This file is part of SLIR (Smart Lencioni Image Resizer).
*
* SLIR is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SLIR is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SLIR. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright Copyright © 2010, Joe Lencioni
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public
* License version 3 (GPLv3)
* @since 2.0
* @package SLIR
*/
/* $Id: slir.class.php 129 2010-12-22 19:43:06Z joe.lencioni $ */
/**
* SLIR (Smart Lencioni Image Resizer)
* Resizes images, intelligently sharpens, crops based on width:height ratios,
* color fills transparent GIFs and PNGs, and caches variations for optimal
* performance.
*
* I love to hear when my work is being used, so if you decide to use this,
* feel encouraged to send me an email. I would appreciate it if you would
* include a link on your site back to Shifting Pixel (either the SLIR page or
* shiftingpixel.com), but don’t worry about including a big link on each page
* if you don’t want to–one will do just nicely. Feel free to contact me to
* discuss any specifics (joe@shiftingpixel.com).
*
* REQUIREMENTS:
* - PHP 5.1.0+
* - GD
*
* RECOMMENDED:
* - mod_rewrite
*
* USAGE:
* To use, place an img tag with the src pointing to the path of SLIR (typically
* "/slir/") followed by the parameters, followed by the path to the source
* image to resize. All parameters follow the pattern of a one-letter code and
* then the parameter value:
* - Maximum width = w
* - Maximum height = h
* - Crop ratio = c
* - Quality = q
* - Background fill color = b
* - Progressive = p
*
* Note: filenames that include special characters must be URL-encoded (e.g.
* plus sign, +, should be encoded as %2B) in order for SLIR to recognize them
* properly. This can be accomplished by passing your filenames through PHP's
* rawurlencode() or urlencode() function.
*
* EXAMPLES:
*
* Resizing a JPEG to a max width of 100 pixels and a max height of 100 pixels:
* <code><img src="/slir/w100-h100/path/to/image.jpg" alt="Don't forget your alt
* text" /></code>
*
* Resizing and cropping a JPEG into a square:
* <code><img src="/slir/w100-h100-c1:1/path/to/image.jpg" alt="Don't forget
* your alt text" /></code>
*
* Resizing a JPEG without interlacing (for use in Flash):
* <code><img src="/slir/w100-p0/path/to/image.jpg" alt="Don't forget your alt
* text" /></code>
*
* Matting a PNG with #990000:
* <code><img src="/slir/b900/path/to/image.png" alt="Don't forget your alt
* text" /></code>
*
* Without mod_rewrite (not recommended)
* <code><img src="/slir/?w=100&h=100&c=1:1&i=/path/to/image.jpg"
* alt="Don't forget your alt text" /></code>
*
* @author Joe Lencioni <joe@shiftingpixel.com>
* $Date: 2010-12-22 13:43:06 -0600 (Wed, 22 Dec 2010) $
* @version $Revision: 129 $
* @package SLIR
*
* @uses PEL
*
* @todo lock files when writing?
* @todo Prevent SLIR from calling itself
* @todo Percentage resizing?
* @todo Animated GIF resizing?
* @todo Seam carving?
* @todo Crop zoom?
* @todo Crop offsets?
* @todo Remote image fetching?
* @todo Alternative support for ImageMagick?
* @todo Prevent files in cache from being read directly?
* @todo split directory initialization into a separate
* install/upgrade script with friendly error messages, an opportunity to give a
* tip, and a button that tells me they are using it on their site if they like
* @todo document new code
* @todo clean up new code
*/
class SLIR
{
/**
* @since 2.0
* @var string
*/
const VERSION = '2.0b4';
/**
* @since 2.0
* @var string
*/
const CROP_CLASS_CENTERED = 'centered';
/**
* @since 2.0
* @var string
*/
const CROP_CLASS_TOP_CENTERED = 'topcentered';
/**
* @since 2.0
* @var string
*/
const CROP_CLASS_SMART = 'smart';
/**
* @since 2.0
* @var string
*/
const CROP_CLASS_FACE = 'face';
/**
* Request object
*
* @since 2.0
* @uses SLIRRequest
* @var object
*/
private $request;
/**
* Source image object
*
* @since 2.0
* @uses SLIRImage
* @var object
*/
private $source;
/**
* Rendered image object
*
* @since 2.0
* @uses SLIRImage
* @var object
*/
private $rendered;
/**
* Whether or not the cache has already been initialized
*
* @since 2.0
* @var boolean
*/
private $isCacheInitialized = FALSE;
/**
* The magic starts here
*
* @since 2.0
*/
final public function __construct()
{
// This helps prevent unnecessary warnings (which messes up images)
// on servers that are set to display E_STRICT errors.
$this->disableStrictErrorReporting();
// Prevents ob_start('ob_gzhandler') in auto_prepend files from messing
// up SLIR's output.
$this->escapeOutputBuffering();
$this->getConfig();
$this->initializeGarbageCollection();
$this->request = new SLIRRequest();
// Check the cache based on the request URI
if (SLIRConfig::$useRequestCache === TRUE && $this->isRequestCached())
{
$this->serveRequestCachedImage();
}
// Set up our error handler after the request cache to help keep
// everything humming along nicely
require 'slirexception.class.php';
set_error_handler(array('SLIRException', 'error'));
// Set all parameters for resizing
$this->setParameters();
// See if there is anything we actually need to do
if ($this->isSourceImageDesired())
{
$this->serveSourceImage();
}
// Determine rendered dimensions
$this->setRenderedProperties();
// Check the cache based on the properties of the rendered image
if (!$this->isRenderedCached() || !$this->serveRenderedCachedImage())
{
// Image is not cached in any way, so we need to render the image,
// cache it, and serve it up to the client
$this->render();
$this->serveRenderedImage();
} // if
}
/**
* Disables E_STRICT error reporting
*
* @since 2.0
* @return integer
*/
private function disableStrictErrorReporting()
{
return error_reporting(error_reporting() & ~E_STRICT);
}
/**
* Escapes from output buffering.
*
* @since 2.0
* @return void
*/
private function escapeOutputBuffering()
{
while ($level = ob_get_level())
{
ob_end_clean();
if ($level == ob_get_level()) // On some setups, ob_get_level() will return a 1 instead of a 0 when there are no more buffers
{
return;
}
}
}
/**
* Determines if the garbage collector should run for this request.
*
* @since 2.0
* @return boolean
*/
private function garbageCollectionShouldRun()
{
if (rand(1, SLIRConfig::$garbageCollectDivisor) <= SLIRConfig::$garbageCollectProbability)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Checks to see if the garbage collector should be initialized, and if it should, initializes it.
*
* @since 2.0
* @return void
*/
private function initializeGarbageCollection()
{
if ($this->garbageCollectionShouldRun())
{
// Register this as a shutdown function so the additional processing time
// will not affect the speed of the request
register_shutdown_function(array($this, 'collectGarbage'));
}
}
/**
* Deletes stale files from a directory.
*
* Used by the garbage collector to keep the cache directories from overflowing.
*
* @param string $path Directory to delete stale files from
*/
private function deleteStaleFilesFromDirectory($path, $useAccessedTime = TRUE)
{
$now = time();
$dir = new DirectoryIterator($path);
if ($useAccessedTime === TRUE)
{
$function = 'getATime';
}
else
{
$function = 'getCTime';
}
foreach ($dir as $file)
{
if (!$file->isDot() && ($now - $file->$function()) > SLIRConfig::$garbageCollectFileCacheMaxLifetime)
{
unlink($file->getPathName());
}
}
}
/**
* Garbage collector
*
* Clears out old files from the cache
*
* @since 2.0
* @return void
*/
public function collectGarbage()
{
$this->deleteStaleFilesFromDirectory($this->getRequestCacheDir(), FALSE);
$this->deleteStaleFilesFromDirectory($this->getRenderedCacheDir());
}
/**
* Includes the configuration file.
*
* If the configuration file cannot be included, this will throw an error that will hopefully explain what needs to be done.
*
* @since 2.0
* @return void
*/
private function getConfig()
{
if (file_exists(self::configFilename()))
{
require self::configFilename();
}
else if (file_exists('slirconfig-sample.class.php'))
{
if (copy('slirconfig-sample.class.php', self::configFilename()))
{
require self::configFilename();
}
else
{
throw new SLIRException('Could not load configuration file. '
. 'Please copy "slirconfig-sample.class.php" to '
. '"' . self::configFilename() . '".');
}
}
else
{
throw new SLIRException('Could not find "' . self::configFilename() . '" or '
. '"slirconfig-sample.class.php"');
} // if
}
/**
* Returns the configuration filename. Allows the developer to specify an alternate configuration file.
*
* @since 2.0
* @return string
*/
private function configFilename()
{
if (defined('SLIR_CONFIG_FILENAME'))
{
return SLIR_CONFIG_FILENAME;
}
else
{
return 'slirconfig.class.php';
}
}
/**
* Sets up parameters for image resizing
*
* @since 2.0
* @return void
*/
private function setParameters()
{
$this->source = new SLIRImage();
$this->source->path = $this->request->path;
// If either a max width or max height are not specified or larger than
// the source image we default to the dimension of the source image so
// they do not become constraints on our resized image.
if (!$this->request->width || $this->request->width > $this->source->width)
{
$this->request->width = $this->source->width;
}
if (!$this->request->height || $this->request->height > $this->source->height)
{
$this->request->height = $this->source->height;
}
}
/**
* Allocates memory for the request.
*
* Tries to dynamically guess how much memory will be needed for the request based on the dimensions of the source image.
*
* @since 2.0
* @return void
*/
private function allocateMemory()
{
// Multiply width * height * 5 bytes
$estimatedMemory = $this->source->width * $this->source->height * 5;
// Convert memory to Megabytes and add 15 in order to allow some slack
$estimatedMemory = round(($estimatedMemory / 1024) / 1024, 0) + 15;
$v = ini_set('memory_limit', min($estimatedMemory, SLIRConfig::$maxMemoryToAllocate) . 'M');
}
/**
* Renders requested changes to the image
*
* @since 2.0
* @return void
*/
private function render()
{
$this->allocateMemory();
$this->source->createImageFromFile();
$this->rendered->createBlankImage();
$this->rendered->background($this->isBackgroundFillOn());
$this->copySourceToRendered();
$this->rendered->setPath($this->source->path, FALSE);
$this->source->destroyImage();
$this->rendered->crop($this->isBackgroundFillOn());
$this->rendered->sharpen($this->calculateSharpnessFactor());
$this->rendered->interlace();
}
/**
* Copies the source image to the rendered image, resizing (resampling) it if resizing is requested
*
* @since 2.0
* @return void
*/
private function copySourceToRendered()
{
// Resample the original image into the resized canvas we set up earlier
if ($this->source->width != $this->rendered->width || $this->source->height != $this->rendered->height)
{
ImageCopyResampled(
$this->rendered->image,
$this->source->image,
0,
0,
0,
0,
$this->rendered->width,
$this->rendered->height,
$this->source->width,
$this->source->height
);
}
else // No resizing is needed, so make a clean copy
{
ImageCopy(
$this->rendered->image,
$this->source->image,
0,
0,
0,
0,
$this->source->width,
$this->source->height
);
} // if
}
/**
* Calculates how much to sharpen the image based on the difference in dimensions of the source image and the rendered image
*
* @since 2.0
* @return integer Sharpness factor
*/
private function calculateSharpnessFactor()
{
return $this->calculateASharpnessFactor($this->source->area(), $this->rendered->area());
}
/**
* Calculates sharpness factor to be used to sharpen an image based on the
* area of the source image and the area of the destination image
*
* @since 2.0
* @author Ryan Rud
* @link http://adryrun.com
*
* @param integer $sourceArea Area of source image
* @param integer $destinationArea Area of destination image
* @return integer Sharpness factor
*/
private function calculateASharpnessFactor($sourceArea, $destinationArea)
{
$final = sqrt($destinationArea) * (750.0 / sqrt($sourceArea));
$a = 52;
$b = -0.27810650887573124;
$c = .00047337278106508946;
$result = $a + $b * $final + $c * $final * $final;
return max(round($result), 0);
}
/**
* Copies IPTC data from the source image to the cached file
*
* @since 2.0
* @param string $cacheFilePath
* @return boolean
*/
private function copyIPTC($cacheFilePath)
{
$data = '';
$iptc = $this->source->iptc;
// Originating program
$iptc['2#065'] = array('Smart Lencioni Image Resizer');
// Program version
$iptc['2#070'] = array(SLIR::VERSION);
foreach($iptc as $tag => $iptcData)
{
$tag = substr($tag, 2);
$data .= $this->makeIPTCTag(2, $tag, $iptcData[0]);
}
// Embed the IPTC data
return iptcembed($data, $cacheFilePath);
}
/**
* @since 2.0
* @author Thies C. Arntzen
*/
final function makeIPTCTag($rec, $data, $value)
{
$length = strlen($value);
$retval = chr(0x1C) . chr($rec) . chr($data);
if ($length < 0x8000)
{
$retval .= chr($length >> 8) . chr($length & 0xFF);
}
else
{
$retval .= chr(0x80) .
chr(0x04) .
chr(($length >> 24) & 0xFF) .
chr(($length >> 16) & 0xFF) .
chr(($length >> 8) & 0xFF) .
chr($length & 0xFF);
}
return $retval . $value;
}
/**
* Checks parameters against the image's attributes and determines whether
* anything needs to be changed or if we simply need to serve up the source
* image
*
* @since 2.0
* @return boolean
* @todo Add check for JPEGs and progressiveness
*/
private function isSourceImageDesired()
{
if ($this->isWidthDifferent()
|| $this->isHeightDifferent()
|| $this->isBackgroundFillOn()
|| $this->isQualityOn()
|| $this->isCroppingNeeded()
)
{
return FALSE;
}
else
{
return TRUE;
}
}
/**
* Determines if the requested width is different than the width of the source image
*
* @since 2.0
* @return boolean
*/
private function isWidthDifferent()
{
if ($this->request->width !== NULL && $this->request->width < $this->source->width)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Determines if the requested height is different than the height of the source image
*
* @since 2.0
* @return boolean
*/
private function isHeightDifferent()
{
if ($this->request->height !== NULL && $this->request->height < $this->source->height)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Determines if a background fill has been requested and if the image is able to have transparency (not for JPEG files)
*
* @since 2.0
* @return boolean
*/
private function isBackgroundFillOn()
{
if ($this->request->isBackground() && $this->source->isAbleToHaveTransparency())
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Determines if the user included image quality in the request
*
* @since 2.0
* @return boolean
*/
private function isQualityOn()
{
return $this->request->isQuality();
}
/**
* Determines if the image should be cropped based on the requested crop ratio and the width:height ratio of the source image
*
* @since 2.0
* @return boolean
*/
private function isCroppingNeeded()
{
if ($this->request->isCropping() && $this->request->cropRatio['ratio'] != $this->source->ratio())
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Computes and sets properties of the rendered image, such as the actual
* width, height, and quality
*
* @since 2.0
*/
private function setRenderedProperties()
{
$this->rendered = new SLIRImage();
// Set default properties of the rendered image
$this->rendered->width = $this->source->width;
$this->rendered->height = $this->source->height;
// Cropping
/*
To determine the width and height of the rendered image, the following
should occur.
If cropping an image is required, we need to:
1. Compute the dimensions of the source image after cropping before
resizing.
2. Compute the dimensions of the resized image before cropping. One of
these dimensions may be greater than maxWidth or maxHeight because
they are based on the dimensions of the final rendered image, which
will be cropped to fit within the specified maximum dimensions.
3. Compute the dimensions of the resized image after cropping. These
must both be less than or equal to maxWidth and maxHeight.
4. Then when rendering, the image needs to be resized, crop offsets
need to be computed based on the desired method (smart or centered),
and the image needs to be cropped to the specified dimensions.
If cropping an image is not required, we need to compute the dimensions
of the image without cropping. These must both be less than or equal to
maxWidth and maxHeight.
*/
if ($this->isCroppingNeeded())
{
// Determine the dimensions of the source image after cropping and
// before resizing
if ($this->request->cropRatio['ratio'] > $this->source->ratio())
{
// Image is too tall so we will crop the top and bottom
$this->source->cropHeight = $this->source->width / $this->request->cropRatio['ratio'];
$this->source->cropWidth = $this->source->width;
}
else
{
// Image is too wide so we will crop off the left and right sides
$this->source->cropWidth = $this->source->height * $this->request->cropRatio['ratio'];
$this->source->cropHeight = $this->source->height;
} // if
$this->source->cropper = $this->request->cropper;
$this->rendered->cropper = $this->source->cropper;
} // if
if ($this->shouldResizeBasedOnWidth())
{
$this->rendered->height = round($this->resizeWidthFactor() * $this->source->height);
$this->rendered->width = round($this->resizeWidthFactor() * $this->source->width);
// Determine dimensions after cropping
if ($this->isCroppingNeeded())
{
$this->rendered->cropHeight = round($this->resizeWidthFactor() * $this->source->cropHeight);
$this->rendered->cropWidth = round($this->resizeWidthFactor() * $this->source->cropWidth);
} // if
}
else if ($this->shouldResizeBasedOnHeight())
{
$this->rendered->width = round($this->resizeHeightFactor() * $this->source->width);
$this->rendered->height = round($this->resizeHeightFactor() * $this->source->height);
// Determine dimensions after cropping
if ($this->isCroppingNeeded())
{
$this->rendered->cropHeight = round($this->resizeHeightFactor() * $this->source->cropHeight);
$this->rendered->cropWidth = round($this->resizeHeightFactor() * $this->source->cropWidth);
} // if
}
else if ($this->isCroppingNeeded()) // No resizing is needed but we still need to crop
{
$ratio = ($this->resizeUncroppedWidthFactor() > $this->resizeUncroppedHeightFactor())
? $this->resizeUncroppedWidthFactor() : $this->resizeUncroppedHeightFactor();
$this->rendered->width = round($ratio * $this->source->width);
$this->rendered->height = round($ratio * $this->source->height);
$this->rendered->cropWidth = round($ratio * $this->source->cropWidth);
$this->rendered->cropHeight = round($ratio * $this->source->cropHeight);
} // if
// Determine the quality of the output image
$this->rendered->quality = ($this->request->quality !== NULL)
? $this->request->quality : SLIRConfig::$defaultQuality;
// Set up the appropriate image handling parameters based on the original
// image's mime type
// @todo some of this code should be moved to the SLIRImage class
$this->rendered->mime = $this->source->mime;
if ($this->source->isGIF())
{
// We need to convert GIFs to PNGs
$this->rendered->mime = 'image/png';
$this->rendered->progressive = FALSE;
// We are converting the GIF to a PNG, and PNG needs a
// compression level of 0 (no compression) through 9
$this->rendered->quality = round(10 - ($this->rendered->quality / 10));
}
else if ($this->source->isPNG())
{
$this->rendered->progressive = FALSE;
// PNG needs a compression level of 0 (no compression) through 9
$this->rendered->quality = round(10 - ($this->rendered->quality / 10));
}
else if ($this->source->isJPEG())
{
$this->rendered->progressive = ($this->request->progressive !== NULL)
? $this->request->progressive : SLIRConfig::$defaultProgressiveJPEG;
$this->rendered->background = NULL;
}
else
{
throw new SLIRException("Unable to determine type of source image");
} // if
if ($this->isBackgroundFillOn())
{
$this->rendered->background = $this->request->background;
}
}
/**
* Detemrines if the image should be resized based on its width (i.e. the width is the constraining dimension for this request)
*
* @since 2.0
* @return boolean
*/
private function shouldResizeBasedOnWidth()
{
if (floor($this->resizeWidthFactor() * $this->source->height) <= $this->request->height)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* Detemrines if the image should be resized based on its height (i.e. the height is the constraining dimension for this request)
* @since 2.0
* @return boolean
*/
private function shouldResizeBasedOnHeight()
{
if (floor($this->resizeHeightFactor() * $this->source->width) <= $this->request->width)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* @since 2.0
* @return float
*/
private function resizeWidthFactor()
{
if ($this->source->cropWidth !== NULL)
{
return $this->resizeCroppedWidthFactor();
}
else
{
return $this->resizeUncroppedWidthFactor();
}
}
/**
* @since 2.0
* @return float
*/
private function resizeUncroppedWidthFactor()
{
return $this->request->width / $this->source->width;
}
/**
* @since 2.0
* @return float
*/
private function resizeCroppedWidthFactor()
{
return $this->request->width / $this->source->cropWidth;
}
/**
* @since 2.0
* @return float
*/
private function resizeHeightFactor()
{
if ($this->source->cropHeight !== NULL)
{
return $this->resizeCroppedHeightFactor();
}
else
{
return $this->resizeUncroppedHeightFactor();
}
}
/**
* @since 2.0
* @return float
*/
private function resizeUncroppedHeightFactor()
{
return $this->request->height / $this->source->height;
}
/**
* @since 2.0
* @return float
*/
private function resizeCroppedHeightFactor()
{
return $this->request->height / $this->source->cropHeight;
}
/**
* Determines if the rendered file is in the rendered cache
*
* @since 2.0
* @return boolean
*/
private function isRenderedCached()
{
return $this->isCached($this->renderedCacheFilePath());
}
/**
* Determines if the request is symlinked to the rendered file
*
* @since 2.0
* @return boolean
*/
private function isRequestCached()
{
return $this->isCached($this->requestCacheFilePath());
}
/**
* Determines if a given file exists in the cache
*
* @since 2.0
* @param string $cacheFilePath
* @return boolean
*/
private function isCached($cacheFilePath)
{
if (!file_exists($cacheFilePath))
{
return FALSE;
}
$cacheModified = filemtime($cacheFilePath);
if (!$cacheModified)
{
return FALSE;
}
$imageModified = filectime($this->request->fullPath());
if ($imageModified >= $cacheModified)
{
return FALSE;
}
else
{
return TRUE;
}
}
/**
* @since 2.0
* @return string
*/
private function getRenderedCacheDir()
{
return SLIRConfig::$cacheDir . '/rendered';
}