-
Notifications
You must be signed in to change notification settings - Fork 191
/
ImageSizerEngine.php
executable file
·1566 lines (1389 loc) · 38.5 KB
/
ImageSizerEngine.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 namespace ProcessWire;
/**
* ImageSizer Engine Module (Abstract)
*
* Copyright (C) 2016 by Horst Nogajski and Ryan Cramer
* This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*
* @property bool $autoRotation
* @property bool $upscaling
* @property array|string|bool $cropping
* @property int $quality
* @property string $sharpening
* @property float $defaultGamma
* @property float $scale
* @property int $rotate
* @property string $flip
* @property bool $useUSM
* @property int $enginePriority Priority for use among other ImageSizerEngine modules (0=disabled, 1=first, 2=second, 3=and so on)
*
*/
abstract class ImageSizerEngine extends WireData implements Module, ConfigurableModule {
/**
* Filename to be resized
*
* @var string
*
*/
protected $filename;
/**
* Temporary filename as used by the resize() method
*
* @var string
*
*/
protected $tmpFile;
/**
* Extension of filename
*
* @var string
*
*/
protected $extension;
/**
* Type of image
*
*/
protected $imageType = null;
/**
* Image quality setting, 1..100
*
* @var int
*
*/
protected $quality = 90;
/**
* Information about the image (width/height)
*
* @var array
*
*/
protected $image = array(
'width' => 0,
'height' => 0
);
/**
* Allow images to be upscaled / enlarged?
*
* @var bool
*
*/
protected $upscaling = true;
/**
* Directions that cropping may gravitate towards
*
* Beyond those included below, TRUE represents center and FALSE represents no cropping.
*
*/
static protected $croppingValues = array(
'nw' => 'northwest',
'n' => 'north',
'ne' => 'northeast',
'w' => 'west',
'e' => 'east',
'sw' => 'southwest',
's' => 'south',
'se' => 'southeast',
);
/**
* Allow images to be cropped to achieve necessary dimension? If so, what direction?
*
* Possible values: northwest, north, northeast, west, center, east, southwest, south, southeast
* or TRUE to crop to center, or FALSE to disable cropping.
* Default is: TRUE
*
* @var bool
*
*/
protected $cropping = true;
/**
* This can be populated on a per image basis. It provides cropping first and then resizing, the opposite of the
* default behavior
*
* It needs an array with 4 params: x y w h for the cropping rectangle
*
* Default is: null
*
* @var null|array
*
*/
protected $cropExtra = null;
/**
* Was the given image modified?
*
* @var bool
*
*/
protected $modified = false;
/**
* enable auto_rotation according to EXIF-Orientation-Flag
*
* @var bool
*
*/
protected $autoRotation = true;
/**
* default sharpening mode: [ none | soft | medium | strong ]
*
* @var string
*
*/
protected $sharpening = 'soft';
/**
* Degrees to rotate: -270, -180, -90, 90, 180, 270
*
* @var int
*
*/
protected $rotate = 0;
/**
* Flip image: Specify 'v' for vertical or 'h' for horizontal
*
* @var string
*
*/
protected $flip = '';
/**
* default gamma correction: 0.5 - 4.0 | -1 to disable gammacorrection, default = 2.0
*
* can be overridden by setting it to $config->imageSizerOptions['defaultGamma']
* or passing it along with image options array
*
*/
protected $defaultGamma = 2.2;
/**
* @var bool
*
*/
protected $useUSM = false;
/**
* Other options for 3rd party use
*
* @var array
*
*/
protected $options = array();
/**
* Options allowed for sharpening
*
* @var array
*
*/
static protected $sharpeningValues = array(
0 => 'none', // none
1 => 'soft',
2 => 'medium',
3 => 'strong'
);
/**
* List of valid option Names from config.php (@horst)
*
* @var array
*
*/
protected $optionNames = array(
'autoRotation',
'upscaling',
'cropping',
'quality',
'sharpening',
'defaultGamma',
'scale',
'rotate',
'flip',
'useUSM',
);
/**
* Supported image types (@teppo)
*
* @var array
*
*/
protected $supportedImageTypes = array(
'gif' => IMAGETYPE_GIF,
'jpg' => IMAGETYPE_JPEG,
'jpeg' => IMAGETYPE_JPEG,
'png' => IMAGETYPE_PNG,
);
/**
* Indicates how much an image should be sharpened
*
* @var int
*
*/
protected $usmValue = 100;
/**
* Result of iptcparse(), if available
*
* @var mixed
*
*/
protected $iptcRaw = null;
/**
* List of valid IPTC tags (@horst)
*
* @var array
*
*/
protected $validIptcTags = array(
'005', '007', '010', '012', '015', '020', '022', '025', '030', '035', '037', '038', '040', '045', '047', '050', '055', '060',
'062', '063', '065', '070', '075', '080', '085', '090', '092', '095', '100', '101', '103', '105', '110', '115', '116', '118',
'120', '121', '122', '130', '131', '135', '150', '199', '209', '210', '211', '212', '213', '214', '215', '216', '217');
/**
* Information about the image from getimagesize (width, height, imagetype, channels, etc.)
*
* @var array|null
*
*/
protected $info = null;
/**
* HiDPI scale value (2.0 = hidpi, 1.0 = normal)
*
* @var float
*
*/
protected $scale = 1.0;
/**
* the resize wrapper method sets this to the width of the given source image
*
* @var integer
*
*/
protected $fullWidth;
/**
* the resize wrapper method sets this to the height of the given source image
*
* @var integer
*
*/
protected $fullHeight;
/**
* the resize wrapper method sets this to the width of the target image
* therefore it also may calculate it in regard of hiDpi
*
* @var integer
*
*/
protected $finalWidth;
/**
* the resize wrapper method sets this to the height of the target image
* therefor it also may calculate it in regard of hiDpi
*
* @var integer
*
*/
protected $finalHeight;
/**
* Data received from the setConfigData() method
*
* @var array
*
*/
protected $moduleConfigData = array();
/**
* Collection of Imagefile and -format Informations from ImageInspector
*
* @var null|array
*
*/
protected $inspectionResult = null;
/******************************************************************************************************/
public function __construct() {
$this->set('enginePriority', 1);
parent::__construct();
}
/**
* Prepare the ImageSizer (this should be the first method you call)
*
* This is used as a replacement for __construct() since modules can't have required arguments
* to their constructor.
*
* @param string $filename
* @param array $options
* @param null|array $inspectionResult
*
*/
public function prepare($filename, $options = array(), $inspectionResult = null) {
// ensures the resize doesn't timeout the request (with at least 30 seconds)
$this->setTimeLimit();
// when invoked from Pageimage, $inspectionResult holds the InfoCollection from ImageInspector, otherwise NULL
// if it is invoked manually and its value is NULL, the method loadImageInfo() will fetch the infos
$this->inspectionResult = $inspectionResult;
// filling all options with global custom values from config.php
$options = array_merge($this->wire('config')->imageSizerOptions, $options);
$this->setOptions($options);
$this->loadImageInfo($filename, false);
}
/*************************************************************************************************
* ABSTRACT AND TEMPLATE METHODS
*
*/
/**
* Is this ImageSizer class ready only means: does the server / system provide all Requirements!
*
* @param string $action Optional type of action supported.
* @return bool
*
*/
abstract public function supported($action = 'imageformat');
/**
* Process the image resize
*
* Processing is as follows:
* 1. first do a check if the given image(type) can be processed, if not do an early return false
* 2. than (try) to process all required steps, if one failes, return false
* 3. if all is successful, finally return true
*
* @param string $srcFilename Source file
* @param string $dstFilename Destination file
* @param int $fullWidth Current width
* @param int $fullHeight Current height
* @param int $finalWidth Requested final width
* @param int $finalHeight Requested final height
* @return bool True if successful, false if not
* @throws WireException
*
*/
abstract protected function processResize($srcFilename, $dstFilename, $fullWidth, $fullHeight, $finalWidth, $finalHeight);
/**
* Get array of image file extensions this ImageSizerModule can process
*
* @return array of uppercase file extensions, i.e. ['PNG', 'JPG']
*
*/
abstract protected function validSourceImageFormats();
/**
* Get an array of image file extensions this ImageSizerModule can create
*
* @return array of uppercase file extensions, i.e. ['PNG', 'JPG']
*
*/
protected function validTargetImageFormats() {
return $this->validSourceImageFormats();
}
/*************************************************************************************************
* COMMON IMPLEMENTATION METHODS
*
*/
/**
* Load all image information from ImageInspector (Module)
*
* @param string $filename
* @param bool $reloadAll
* @throws WireException
*
*/
protected function loadImageInfo($filename, $reloadAll = false) {
// if the engine is invoked manually, we need to inspect the image first
if(empty($this->inspectionResult) || $reloadAll) {
$imageInspector = new ImageInspector($filename);
$this->inspectionResult = $imageInspector->inspect($filename, true);
}
if(null === $this->inspectionResult) throw new WireException("no valid filename passed to image inspector");
if(false === $this->inspectionResult) throw new WireException(basename($filename) . " - not a recognized image");
$this->filename = $this->inspectionResult['filename'];
$this->extension = $this->inspectionResult['extension'];
$this->imageType = $this->inspectionResult['imageType'];
if(!in_array($this->imageType, $this->supportedImageTypes)) {
throw new WireException(basename($filename) . " - not a supported image type");
}
$this->info = $this->inspectionResult['info'];
$this->iptcRaw = $this->inspectionResult['iptcRaw'];
// set width & height
$this->setImageInfo($this->info['width'], $this->info['height']);
}
/**
* ImageInformation from Image Inspector
* in short form or full RawInfoData
*
* @param bool $rawData
* @return string|array
* @todo this appears to be a duplicate of what's in ImageSizer class?
*
*/
protected function getImageInfo($rawData = false) {
if($rawData) return $this->inspectionResult;
$imageType = $this->inspectionResult['info']['imageType'];
$type = '';
$indexed = '';
$trans = '';
$animated = '';
switch($imageType) {
case \IMAGETYPE_JPEG:
$type = 'jpg';
$indexed = '';
$trans = '';
$animated = '';
break;
case \IMAGETYPE_GIF:
$type = 'gif';
$indexed = '';
$trans = $this->inspectionResult['info']['trans'] ? '-trans' : '';
$animated = $this->inspectionResult['info']['animated'] ? '-anim' : '';
break;
case \IMAGETYPE_PNG:
$type = 'png';
$indexed = 'Indexed' == $this->inspectionResult['info']['colspace'] ? '8' : '24';
$trans = is_array($this->inspectionResult['info']['trans']) ? '-trans' : '';
$trans = $this->inspectionResult['info']['alpha'] ? '-alpha' : $trans;
$animated = '';
break;
}
return $type . $indexed . $trans . $animated;
}
/**
* Default IPTC Handling
*
* If we've retrieved IPTC-Metadata from sourcefile, we write it into the variation here but we omit
* custom tags for internal use (@horst)
*
* @param string $filename the file we want write the IPTC data to
* @param bool $includeCustomTags default is FALSE
* @return bool|null
*
*/
public function writeBackIPTC($filename, $includeCustomTags = false) {
if($this->wire('config')->debug) {
// add a timestamp and the name of the image sizer engine to the IPTC tag number 217
$entry = $this->className() . '-' . date('Ymd:His');
if(!$this->iptcRaw) $this->iptcRaw = array();
$this->iptcRaw['2#217'] = array(0 => $entry);
}
if(!$this->iptcRaw) return null; // the sourceimage doesn't contain IPTC infos
$content = iptcembed($this->iptcPrepareData($includeCustomTags), $filename);
if(false === $content) return null;
$extension = pathinfo($filename, \PATHINFO_EXTENSION);
$dest = preg_replace('/\.' . $extension . '$/', '_tmp.' . $extension, $filename);
if(strlen($content) == @file_put_contents($dest, $content, \LOCK_EX)) {
// on success we replace the file
unlink($filename);
rename($dest, $filename);
wireChmod($filename);
return true;
} else {
// it was created a temp diskfile but not with all data in it
if(file_exists($dest)) @unlink($dest);
return false;
}
}
/**
* Save the width and height of the image
*
* @param int $width
* @param int $height
*
*/
protected function setImageInfo($width, $height) {
$this->image['width'] = $width;
$this->image['height'] = $height;
}
/**
* Return the image width
*
* @return int
*
*/
public function getWidth() {
return $this->image['width'];
}
/**
* Return the image height
*
* @return int
*
*/
public function getHeight() {
return $this->image['height'];
}
/**
* Given a target height, return the proportional width for this image
*
* @param int $targetHeight
*
* @return int
*
*/
protected function getProportionalWidth($targetHeight) {
$img =& $this->image;
return ceil(($targetHeight / $img['height']) * $img['width']); // @horst
}
/**
* Given a target width, return the proportional height for this image
*
* @param int $targetWidth
*
* @return int
*
*/
protected function getProportionalHeight($targetWidth) {
$img =& $this->image;
return ceil(($targetWidth / $img['width']) * $img['height']); // @horst
}
/**
* Get an array of the 4 dimensions necessary to perform the resize
*
* Note: Some code used in this method is adapted from code found in comments at php.net for the GD functions
*
* Intended for use by the resize() method
*
* @param int $targetWidth
* @param int $targetHeight
*
* @return array
*
*/
protected function getResizeDimensions($targetWidth, $targetHeight) {
$pWidth = $targetWidth;
$pHeight = $targetHeight;
$img =& $this->image;
if(!$targetHeight) $targetHeight = round(($targetWidth / $img['width']) * $img['height']);
if(!$targetWidth) $targetWidth = round(($targetHeight / $img['height']) * $img['width']);
$originalTargetWidth = $targetWidth;
$originalTargetHeight = $targetHeight;
if($img['width'] < $img['height']) {
$pHeight = $this->getProportionalHeight($targetWidth);
} else {
$pWidth = $this->getProportionalWidth($targetHeight);
}
if($pWidth < $targetWidth) {
// if the proportional width is smaller than specified target width
$pWidth = $targetWidth;
$pHeight = $this->getProportionalHeight($targetWidth);
}
if($pHeight < $targetHeight) {
// if the proportional height is smaller than specified target height
$pHeight = $targetHeight;
$pWidth = $this->getProportionalWidth($targetHeight);
}
if(!$this->upscaling) {
// we are going to shoot for something smaller than the target
while($pWidth > $img['width'] || $pHeight > $img['height']) {
// favor the smallest dimension
if($pWidth > $img['width']) {
$pWidth = $img['width'];
$pHeight = $this->getProportionalHeight($pWidth);
}
if($pHeight > $img['height']) {
$pHeight = $img['height'];
$pWidth = $this->getProportionalWidth($pHeight);
}
if($targetWidth > $pWidth) $targetWidth = $pWidth;
if($targetHeight > $pHeight) $targetHeight = $pHeight;
if(!$this->cropping) {
$targetWidth = $pWidth;
$targetHeight = $pHeight;
}
}
}
if(!$this->cropping) {
// we will make the image smaller so that none of it gets cropped
// this means we'll be adjusting either the targetWidth or targetHeight
// till we have a suitable dimension
if($pHeight > $originalTargetHeight) {
$pHeight = $originalTargetHeight;
$pWidth = $this->getProportionalWidth($pHeight);
$targetWidth = $pWidth;
$targetHeight = $pHeight;
}
if($pWidth > $originalTargetWidth) {
$pWidth = $originalTargetWidth;
$pHeight = $this->getProportionalHeight($pWidth);
$targetWidth = $pWidth;
$targetHeight = $pHeight;
}
}
return array(
0 => (int) $pWidth,
1 => (int) $pHeight,
2 => (int) $targetWidth,
3 => (int) $targetHeight
);
}
/**
* Was the image modified?
*
* @return bool
*
*/
public function isModified() {
return $this->modified;
}
/**
* Given an unknown cropping value, return the validated internal representation of it
*
* @param string|bool|array $cropping
*
* @return string|bool|array
*
*/
static public function croppingValue($cropping) {
if(is_string($cropping)) {
$cropping = strtolower($cropping);
if(strpos($cropping, ',')) {
$cropping = explode(',', $cropping);
if(strpos($cropping[0], '%') !== false) $cropping[0] = round(min(100, max(0, $cropping[0]))) . '%';
else $cropping[0] = (int) $cropping[0];
if(strpos($cropping[1], '%') !== false) $cropping[1] = round(min(100, max(0, $cropping[1]))) . '%';
else $cropping[1] = (int) $cropping[1];
}
}
if($cropping === true) $cropping = true; // default, crop to center
else if(!$cropping) $cropping = false;
else if(is_array($cropping)) {} // already took care of it above
else if(in_array($cropping, self::$croppingValues)) $cropping = array_search($cropping, self::$croppingValues);
else if(array_key_exists($cropping, self::$croppingValues)) {}
else $cropping = true; // unknown value or 'center', default to TRUE/center
return $cropping;
}
/**
* Given an unknown cropping value, return the string representation of it
*
* Okay for use in filenames
*
* @param string|bool|array $cropping
*
* @return string
*
*/
static public function croppingValueStr($cropping) {
$cropping = self::croppingValue($cropping);
// crop name if custom center point is specified
if(is_array($cropping)) {
// p = percent, d = pixel dimension
$cropping = (strpos($cropping[0], '%') !== false ? 'p' : 'd') . ((int) $cropping[0]) . 'x' . ((int) $cropping[1]);
}
// if crop is TRUE or FALSE, we don't reflect that in the filename, so make it blank
if(is_bool($cropping)) $cropping = '';
return $cropping;
}
/**
* Turn on/off cropping and/or set cropping direction
*
* @param bool|string|array $cropping Specify one of: northwest, north, northeast, west, center, east, southwest,
* south, southeast. Or a string of: 50%,50% (x and y percentages to crop from) Or an array('50%', '50%') Or to
* disable cropping, specify boolean false. To enable cropping with default (center), you may also specify
* boolean true.
*
* @return $this
*
*/
public function setCropping($cropping = true) {
$this->cropping = self::croppingValue($cropping);
return $this;
}
/**
* Set values for cropExtra rectangle, which enables cropping before resizing
*
* Added by @horst
*
* @param array $value containing 4 params (x y w h) indexed or associative
*
* @return $this
* @throws WireException when given invalid value
*
*/
public function setCropExtra($value) {
$this->cropExtra = null;
$x = null;
$y = null;
$w = null;
$h = null;
if(!is_array($value) || 4 != count($value)) {
throw new WireException('Missing or wrong param Array for ImageSizer-cropExtra!');
}
if(array_keys($value) === range(0, count($value) - 1)) {
// we have a zerobased sequential array, we assume this order: x y w h
list($x, $y, $w, $h) = $value;
} else {
// check for associative array
foreach(array('x', 'y', 'w', 'h') as $v) {
if(isset($value[$v])) $$v = $value[$v];
}
}
foreach(array('x', 'y', 'w', 'h') as $k) {
$v = isset($$k) ? $$k : -1;
if(!is_int($v) || $v < 0) throw new WireException("Missing or wrong param $k for ImageSizer-cropExtra!");
if(('w' == $k || 'h' == $k) && 0 == $v) throw new WireException("Wrong param $k for ImageSizer-cropExtra!");
}
$this->cropExtra = array($x, $y, $w, $h);
return $this;
}
/**
* Set the image quality 1-100, where 100 is highest quality
*
* @param int $n
*
* @return $this
*
*/
public function setQuality($n) {
$n = (int) $n;
if($n < 1) $n = 1;
if($n > 100) $n = 100;
$this->quality = (int) $n;
return $this;
}
/**
* Given an unknown sharpening value, return the string representation of it
*
* Okay for use in filenames. Method added by @horst
*
* @param string|bool $value
* @param bool $short
*
* @return string
*
*/
static public function sharpeningValueStr($value, $short = false) {
$sharpeningValues = self::$sharpeningValues;
if(is_string($value) && in_array(strtolower($value), $sharpeningValues)) {
$ret = strtolower($value);
} else if(is_int($value) && isset($sharpeningValues[$value])) {
$ret = $sharpeningValues[$value];
} else if(is_bool($value)) {
$ret = $value ? "soft" : "none";
} else {
// sharpening is unknown, return empty string
return '';
}
if(!$short) return $ret; // return name
$flip = array_flip($sharpeningValues);
return 's' . $flip[$ret]; // return char s appended with the numbered index
}
/**
* Set sharpening value: blank (for none), soft, medium, or strong
*
* @param mixed $value
*
* @return $this
* @throws WireException
*
*/
public function setSharpening($value) {
if(is_string($value) && in_array(strtolower($value), self::$sharpeningValues)) {
$ret = strtolower($value);
} else if(is_int($value) && isset(self::$sharpeningValues[$value])) {
$ret = self::$sharpeningValues[$value];
} else if(is_bool($value)) {
$ret = $value ? "soft" : "none";
} else {
throw new WireException("Unknown value for sharpening");
}
$this->sharpening = $ret;
return $this;
}
/**
* Turn on/off auto rotation
*
* @param bool $value Whether to auto-rotate or not (default = true)
*
* @return $this
*
*/
public function setAutoRotation($value = true) {
$this->autoRotation = $this->getBooleanValue($value);
return $this;
}
/**
* Turn on/off upscaling
*
* @param bool $value Whether to upscale or not (default = true)
*
* @return $this
*
*/
public function setUpscaling($value = true) {
$this->upscaling = $this->getBooleanValue($value);
return $this;
}
/**
* Set default gamma value: 0.5 - 4.0 | -1
*
* @param float|int $value 0.5 to 4.0 or -1 to disable
*
* @return $this
* @throws WireException when given invalid value
*
*/
public function setDefaultGamma($value = 2.2) {
if($value === -1 || ($value >= 0.5 && $value <= 4.0)) {
$this->defaultGamma = $value;
} else {
throw new WireException('Invalid defaultGamma value - must be 0.5 - 4.0 or -1 to disable gammacorrection');
}
return $this;
}
/**
* Set a time limit for manipulating one image (default is 30)
*
* If specified time limit is less than PHP's max_execution_time, then PHP's setting will be used instead.
*
* @param int $value 10 to 60 recommended, default is 30
*
* @return $this
*
*/
public function setTimeLimit($value = 30) {
// imagesizer can get invoked from different locations, including those that are inside of loops
// like the wire/modules/Inputfield/InputfieldFile/InputfieldFile.module :: ___renderList() method
$prevLimit = ini_get('max_execution_time');
// if unlimited execution time, no need to introduce one
if(!$prevLimit) return $this;
// don't override a previously set high time limit, just start over with it
$timeLimit = (int) ($prevLimit > $value ? $prevLimit : $value);
// restart time limit
set_time_limit($timeLimit);
return $this;
}
/**
* Set scale for hidpi (2.0=hidpi, 1.0=normal, or other value if preferred)
*
* @param float $scale
*
* @return $this
*
*/
public function setScale($scale) {
$this->scale = (float) $scale;
return $this;
}
/**
* Enable hidpi mode?
*
* Just a shortcut for calling $this->scale()
*
* @param bool $hidpi True or false (default=true)
*
* @return $this
*
*/
public function setHidpi($hidpi = true) {
return $this->setScale($hidpi ? 2.0 : 1.0);
}
/**
* Set rotation degrees
*
* Specify one of: -270, -180, -90, 90, 180, 270
*
* @param $degrees
*
* @return $this
*
*/
public function setRotate($degrees) {
$valid = array(-270, -180, -90, 90, 180, 270);
$degrees = (int) $degrees;
if(in_array($degrees, $valid)) $this->rotate = $degrees;
return $this;
}
/**