-
Notifications
You must be signed in to change notification settings - Fork 78
/
fImage.php
1511 lines (1280 loc) · 47 KB
/
fImage.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
/**
* Represents an image on the filesystem, also provides image manipulation functionality
*
* @copyright Copyright (c) 2007-2011 Will Bond, others
* @author Will Bond [wb] <will@flourishlib.com>
* @author Will Bond, iMarc LLC [wb-imarc] <will@imarc.net>
* @author Jeff Turcotte, iMarc LLC [jt] <jeff@imarc.net>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fImage
*
* @version 1.0.0b35
* @changes 1.0.0b35 Fixed an issue with ImageMagick 6.7.5+ and colorspace argument [jt, 2014-01-09]
* @changes 1.0.0b34 Fixed a bug in getImageType() where the fread was not reading enough bytes [jt, 2012-06-05]
* @changes 1.0.0b33 Fixed a method signature [wb, 2011-08-24]
* @changes 1.0.0b32 Added a call to clearstatcache() to ::saveChanges() to solve a bug when fFile::output() is called in the same script execution [wb, 2011-05-23]
* @changes 1.0.0b31 Fixed a bug in using ImageMagick to convert files with a colon in the filename [wb, 2011-03-20]
* @changes 1.0.0b30 Added a check for systems using the GD extension and no memory limit, plus a check for ImageMagick's convert command failing [wb, 2011-03-20]
* @changes 1.0.0b29 Added checks for AIX [wb, 2011-01-19]
* @changes 1.0.0b28 Added the ::rotate() method, added code to try and prevent fatal errors due to hitting the memory limit when using GD [wb, 2010-11-29]
* @changes 1.0.0b27 Backwards Compatibility Break - changed the parameter order in ::crop() from `$crop_from_x`, `$crop_from_y`, `$new_width`, `$new_height` to `$new_width`, `$new_height`, `$crop_from_x`, `$crop_from_y` - added `$horizontal_position` and `$vertical_position` parameters to ::cropToRatio() [wb-imarc, 2010-11-09]
* @changes 1.0.0b26 Fixed a bug where processing via ImageMagick was not properly setting the default RGB colorspace [wb, 2010-10-19]
* @changes 1.0.0b25 Fixed the class to not generate multiple files when saving a JPG from an animated GIF or a TIF with a thumbnail [wb, 2010-09-12]
* @changes 1.0.0b24 Updated class to use fCore::startErrorCapture() instead of `error_reporting()` [wb, 2010-08-09]
* @changes 1.0.0b23 Fixed the class to detect when exec() is disabled and the function has a space before or after in the list [wb, 2010-07-21]
* @changes 1.0.0b22 Fixed ::isImageCompatible() to handle certain JPGs created with Photoshop [wb, 2010-04-03]
* @changes 1.0.0b21 Fixed ::resize() to allow dimensions to be numeric strings instead of just integers [wb, 2010-04-09]
* @changes 1.0.0b20 Added ::append() [wb, 2010-03-15]
* @changes 1.0.0b19 Updated for the new fFile API [wb-imarc, 2010-03-05]
* @changes 1.0.0b18 Fixed a bug in ::saveChanges() that would incorrectly cause new filenames to be created, added the $overwrite parameter to ::saveChanges(), added the $allow_upsizing parameter to ::resize() [wb, 2010-03-03]
* @changes 1.0.0b17 Fixed a couple of bug with using ImageMagick on Windows and BSD machines [wb, 2010-03-02]
* @changes 1.0.0b16 Fixed some bugs with GD not properly handling transparent backgrounds and desaturation of .gif files [wb, 2009-10-27]
* @changes 1.0.0b15 Added ::getDimensions() [wb, 2009-08-07]
* @changes 1.0.0b14 Performance updates for checking image type and compatiblity [wb, 2009-07-31]
* @changes 1.0.0b13 Updated class to work even if the file extension is wrong or not present, ::saveChanges() detects files that aren't writable [wb, 2009-07-29]
* @changes 1.0.0b12 Fixed a bug where calling ::saveChanges() after unserializing would throw an exception related to the image processor [wb, 2009-05-27]
* @changes 1.0.0b11 Added a ::crop() method [wb, 2009-05-27]
* @changes 1.0.0b10 Fixed a bug with GD not saving changes to files ending in .jpeg [wb, 2009-03-18]
* @changes 1.0.0b9 Changed ::processWithGD() to explicitly free the image resource [wb, 2009-03-18]
* @changes 1.0.0b8 Updated for new fCore API [wb, 2009-02-16]
* @changes 1.0.0b7 Changed @ error suppression operator to `error_reporting()` calls [wb, 2009-01-26]
* @changes 1.0.0b6 Fixed ::cropToRatio() and ::resize() to always return the object even if nothing is to be done [wb, 2009-01-05]
* @changes 1.0.0b5 Added check to see if exec() is disabled, which causes ImageMagick to not work [wb, 2009-01-03]
* @changes 1.0.0b4 Fixed ::saveChanges() to not delete the image if no changes have been made [wb, 2008-12-18]
* @changes 1.0.0b3 Fixed a bug with $jpeg_quality in ::saveChanges() from 1.0.0b2 [wb, 2008-12-16]
* @changes 1.0.0b2 Changed some int casts to round() to fix ::resize() dimension issues [wb, 2008-12-11]
* @changes 1.0.0b The initial implementation [wb, 2007-12-19]
*/
class fImage extends fFile
{
// The following constants allow for nice looking callbacks to static methods
const create = 'fImage::create';
const getCompatibleMimetypes = 'fImage::getCompatibleMimetypes';
const isImageCompatible = 'fImage::isImageCompatible';
const reset = 'fImage::reset';
const setImageMagickDirectory = 'fImage::setImageMagickDirectory';
const setImageMagickTempDir = 'fImage::setImageMagickTempDir';
/**
* If we are using the ImageMagick processor, this stores the path to the binaries
*
* @var string
*/
static private $imagemagick_dir = NULL;
/**
* A custom tmp path to use for ImageMagick
*
* @var string
*/
static private $imagemagick_temp_dir = NULL;
/**
* The version of ImageMagick
*
* @var string
*/
static private $imagemagick_version = NULL;
/**
* The processor to use for the image manipulation
*
* @var string
*/
static private $processor = NULL;
/**
* Checks to make sure we can get to and execute the ImageMagick convert binary
*
* @param string $path The path to ImageMagick on the filesystem
* @return void
*/
static private function checkImageMagickBinary($path)
{
// Make sure we can execute the convert binary
if (self::isSafeModeExecDirRestricted($path)) {
throw new fEnvironmentException(
'Safe mode is turned on and the ImageMagick convert binary is not in the directory defined by the safe_mode_exec_dir ini setting or safe_mode_exec_dir is not set - safe_mode_exec_dir is currently %s.',
ini_get('safe_mode_exec_dir')
);
}
$binary = $path . (fCore::checkOS('windows') ? 'convert.exe' : 'convert');
if (self::isOpenBaseDirRestricted($path)) {
exec($binary . ' -version', $executable);
} else {
$executable = is_executable($binary);
}
if (!$executable) {
throw new fEnvironmentException(
'The ImageMagick convert binary located in the directory %s does not exist or is not executable',
$path
);
}
// determine version
exec($binary, $output);
if (preg_match('/\d+\.\d+\.\d+/', $output[0], $matches)) {
self::$imagemagick_version = $matches[0];
}
}
/**
* Creates an image on the filesystem and returns an object representing it
*
* This operation will be reverted by a filesystem transaction being rolled
* back.
*
* @throws fValidationException When no image was specified or when the image already exists
*
* @param string $file_path The path to the new image
* @param string $contents The contents to write to the image
* @return fImage
*/
static public function create($file_path, $contents)
{
if (empty($file_path)) {
throw new fValidationException('No filename was specified');
}
if (file_exists($file_path)) {
throw new fValidationException(
'The image specified, %s, already exists',
$file_path
);
}
$directory = fFilesystem::getPathInfo($file_path, 'dirname');
if (!is_writable($directory)) {
throw new fEnvironmentException(
'The file path specified, %s, is inside of a directory that is not writable',
$file_path
);
}
file_put_contents($file_path, $contents);
$image = new fImage($file_path);
fFilesystem::recordCreate($image);
return $image;
}
/**
* Determines what processor to use for image manipulation
*
* @return void
*/
static public function determineProcessor()
{
// Determine what processor to use
if (self::$processor === NULL) {
// Look for imagemagick first since it can handle more than GD
try {
// If exec is disabled we can't use imagemagick
if (in_array('exec', array_map('trim', explode(',', ini_get('disable_functions'))))) {
throw new Exception();
}
if (fCore::checkOS('windows')) {
$win_search = 'dir /B "C:\Program Files\ImageMagick*" 2> NUL';
exec($win_search, $win_output);
$win_output = trim(join("\n", $win_output));
if (!$win_output || stripos($win_output, 'File not found') !== FALSE) {
throw new Exception();
}
$path = 'C:\\Program Files\\' . $win_output . '\\';
} elseif (fCore::checkOS('linux', 'bsd', 'solaris', 'osx', 'aix')) {
$found = FALSE;
if (fCore::checkOS('solaris')) {
$locations = array(
'/opt/local/bin/',
'/opt/bin/',
'/opt/csw/bin/'
);
} else {
$locations = array(
'/usr/local/bin/',
'/usr/bin/'
);
}
foreach($locations as $location) {
if (self::isSafeModeExecDirRestricted($location)) {
continue;
}
if (self::isOpenBaseDirRestricted($location)) {
exec($location . 'convert -version', $output);
if ($output) {
$found = TRUE;
$path = $location;
break;
}
} elseif (is_executable($location . 'convert')) {
$found = TRUE;
$path = $location;
break;
}
}
// We have no fallback in solaris
if (!$found && fCore::checkOS('solaris')) {
throw new Exception();
}
if (!$found && fCore::checkOS('linux', 'freebsd', 'aix')) {
$nix_search = 'whereis -b convert';
exec($nix_search, $nix_output);
$nix_output = trim(str_replace('convert:', '', join("\n", $nix_output)));
if (!$nix_output) {
throw new Exception();
}
$path = preg_replace('#^(.*)convert$#i', '\1', $nix_output);
}
if (!$found && fCore::checkOS('osx', 'netbsd', 'openbsd')) {
$osx_search = 'whereis convert';
exec($osx_search, $osx_output);
$osx_output = trim(join("\n", $osx_output));
if (!$osx_output) {
throw new Exception();
}
if (preg_match('#^(.*)convert#i', $osx_output, $matches)) {
$path = $matches[1];
}
}
} else {
$path = NULL;
}
self::checkImageMagickBinary($path);
self::$imagemagick_dir = $path;
self::$processor = 'imagemagick';
} catch (Exception $e) {
// Look for GD last since it does not support tiff files
if (function_exists('gd_info')) {
self::$processor = 'gd';
} else {
self::$processor = 'none';
}
}
}
}
/**
* Returns an array of acceptable mime types for the processor that was detected
*
* @internal
*
* @return array The mime types that the detected image processor can manipulate
*/
static public function getCompatibleMimetypes()
{
self::determineProcessor();
$mimetypes = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
if (self::$processor == 'imagemagick') {
$mimetypes[] = 'image/tiff';
}
return $mimetypes;
}
/**
* Gets the dimensions and type of an image stored on the filesystem
*
* The `'type'` key will have one of the following values:
*
* - `{null}` (File type is not supported)
* - `'jpg'`
* - `'gif'`
* - `'png'`
* - `'tif'`
*
* @throws fValidationException When the file specified is not an image
*
* @param string $image_path The path to the image to get stats for
* @param string $element The element to retrieve: `'type'`, `'width'`, `'height'`
* @return mixed An associative array: `'type' => {mixed}, 'width' => {integer}, 'height' => {integer}`, or the element specified
*/
static protected function getInfo($image_path, $element=NULL)
{
$extension = strtolower(fFilesystem::getPathInfo($image_path, 'extension'));
if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff'))) {
$type = self::getImageType($image_path);
if ($type === NULL) {
throw new fValidationException(
'The file specified, %s, does not appear to be an image',
$image_path
);
}
}
fCore::startErrorCapture(E_WARNING);
$image_info = getimagesize($image_path);
fCore::stopErrorCapture();
if ($image_info == FALSE) {
throw new fValidationException(
'The file specified, %s, is not an image',
$image_path
);
}
$valid_elements = array('type', 'width', 'height');
if ($element !== NULL && !in_array($element, $valid_elements)) {
throw new fProgrammerException(
'The element specified, %1$s, is invalid. Must be one of: %2$s.',
$element,
join(', ', $valid_elements)
);
}
$types = array(IMAGETYPE_GIF => 'gif',
IMAGETYPE_JPEG => 'jpg',
IMAGETYPE_PNG => 'png',
IMAGETYPE_TIFF_II => 'tif',
IMAGETYPE_TIFF_MM => 'tif');
$output = array();
$output['width'] = $image_info[0];
$output['height'] = $image_info[1];
if (isset($types[$image_info[2]])) {
$output['type'] = $types[$image_info[2]];
} else {
$output['type'] = NULL;
}
if ($element !== NULL) {
return $output[$element];
}
return $output;
}
/**
* Gets the image type from a file by looking at the file contents
*
* @param string $image The image path to get the type for
* @return string|NULL The type of the image - `'jpg'`, `'gif'`, `'png'` or `'tif'` - NULL if not one of those
*/
static private function getImageType($image)
{
$handle = fopen($image, 'r');
$contents = fread($handle, 32);
fclose($handle);
$_0_8 = substr($contents, 0, 8);
$_0_4 = substr($contents, 0, 4);
$_6_4 = substr($contents, 6, 4);
$_20_4 = substr($contents, 20, 4);
if ($_0_4 == "MM\x00\x2A" || $_0_4 == "II\x2A\x00") {
return 'tif';
}
if ($_0_8 == "\x89PNG\x0D\x0A\x1A\x0A") {
return 'png';
}
if ($_0_4 == 'GIF8') {
return 'gif';
}
if ($_6_4 == 'JFIF' || $_6_4 == 'Exif' || ($_0_4 == "\xFF\xD8\xFF\xED" && $_20_4 == "8BIM")) {
return 'jpg';
}
return NULL;
}
/**
* Checks to make sure the class can handle the image file specified
*
* @internal
*
* @throws fValidationException When the image specified does not exist
*
* @param string $image The image to check for incompatibility
* @return boolean If the image is compatible with the detected image processor
*/
static public function isImageCompatible($image)
{
self::determineProcessor();
if (!file_exists($image)) {
throw new fValidationException(
'The image specified, %s, does not exist',
$image
);
}
$type = self::getImageType($image);
if ($type === NULL || ($type == 'tif' && self::$processor == 'gd')) {
return FALSE;
}
return TRUE;
}
/**
* Checks if the path specified is restricted by open basedir
*
* @param string $path The path to check
* @return boolean If the path is restricted by the `open_basedir` ini setting
*/
static private function isOpenBaseDirRestricted($path)
{
if (ini_get('open_basedir')) {
$open_basedirs = explode((fCore::checkOS('windows')) ? ';' : ':', ini_get('open_basedir'));
$found = FALSE;
foreach ($open_basedirs as $open_basedir) {
if (strpos($path, $open_basedir) === 0) {
$found = TRUE;
}
}
if (!$found) {
return TRUE;
}
}
return FALSE;
}
/**
* Checks if the path specified is restricted by the safe mode exec dir restriction
*
* @param string $path The path to check
* @return boolean If the path is restricted by the `safe_mode_exec_dir` ini setting
*/
static private function isSafeModeExecDirRestricted($path)
{
if (!in_array(strtolower(ini_get('safe_mode')), array('0', '', 'off'))) {
$exec_dir = ini_get('safe_mode_exec_dir');
if (!$exec_dir || stripos($path, $exec_dir) === FALSE) {
return TRUE;
}
}
return FALSE;
}
/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$imagemagick_dir = NULL;
self::$imagemagick_temp_dir = NULL;
self::$processor = NULL;
}
/**
* Sets the directory the ImageMagick binary is installed in and tells the class to use ImageMagick even if GD is installed
*
* @param string $directory The directory ImageMagick is installed in
* @return void
*/
static public function setImageMagickDirectory($directory)
{
$directory = fDirectory::makeCanonical($directory);
self::checkImageMagickBinary($directory);
self::$imagemagick_dir = $directory;
self::$processor = 'imagemagick';
}
/**
* Sets a custom directory to use for the ImageMagick temporary files
*
* @param string $temp_dir The directory to use for the ImageMagick temp dir
* @return void
*/
static public function setImageMagickTempDir($temp_dir)
{
$temp_dir = new fDirectory($temp_dir);
if (!$temp_dir->isWritable()) {
throw new fEnvironmentException(
'The ImageMagick temp directory specified, %s, does not appear to be writable',
$temp_dir->getPath()
);
}
self::$imagemagick_temp_dir = $temp_dir->getPath();
}
/**
* The modifications to perform on the image when it is saved
*
* @var array
*/
private $pending_modifications = array();
/**
* Creates an object to represent an image on the filesystem
*
* @throws fValidationException When no image was specified, when the image does not exist or when the path specified is not an image
*
* @param string $file_path The path to the image
* @param boolean $skip_checks If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere
* @return fImage
*/
public function __construct($file_path, $skip_checks=FALSE)
{
self::determineProcessor();
parent::__construct($file_path, $skip_checks);
if (!self::isImageCompatible($file_path)) {
$valid_image_types = array('GIF', 'JPG', 'PNG');
if (self::$processor == 'imagemagick') {
$valid_image_types[] = 'TIF';
}
throw new fValidationException(
'The image specified, %1$s, is not a valid %2$s file',
$file_path,
fGrammar::joinArray($valid_image_types, 'or')
);
}
}
/**
* Prevents a programmer from trying to append an image
*
* @param mixed $data The data to append to the image
* @return void
*/
public function append($data)
{
throw new fProgrammerException('It is not possible to append an image');
}
/**
* Crops the image by the exact pixel dimensions specified
*
* The crop does not occur until ::saveChanges() is called.
*
* @param numeric $new_width The width in pixels to crop the image to
* @param numeric $new_height The height in pixels to crop the image to
* @param numeric|string $crop_from_x The number of pixels from the left of the image to start the crop from, or a horizontal position of `'left'`, `'center'` or `'right'`
* @param numeric|string $crop_from_y The number of pixels from the top of the image to start the crop from, or a vertical position of `'top'`, `'center'` or `'bottom'`
* @return fImage The image object, to allow for method chaining
*/
public function crop($new_width, $new_height, $crop_from_x, $crop_from_y)
{
$this->tossIfDeleted();
// Get the original dimensions for our parameter checking
$dim = $this->getCurrentDimensions();
$orig_width = $dim['width'];
$orig_height = $dim['height'];
if (is_string($crop_from_x) && !is_numeric($crop_from_x)) {
switch (strtolower($crop_from_x)) {
case 'left':
$crop_from_x = 0;
break;
case 'center':
$crop_from_x = floor(max($orig_width-$new_width, 0)/2);
break;
case 'right':
$crop_from_x = max($orig_width-$new_width, 0);
break;
default:
throw new fProgrammerException(
'The crop-from x specified, %1$s, is not a valid horizontal position. Must be one of: %2$s.',
$crop_from_x,
array('left', 'center', 'right')
);
}
}
if (is_string($crop_from_y) && !is_numeric($crop_from_y)) {
switch (strtolower($crop_from_y)) {
case 'top':
$crop_from_y = 0;
break;
case 'center':
$crop_from_y = floor(max($orig_height-$new_height, 0)/2);
break;
case 'bottom':
$crop_from_y = max($orig_height-$new_height, 0);
break;
default:
throw new fProgrammerException(
'The crop-from y specified, %1$s, is not a valid vertical position. Must be one of: %2$s.',
$crop_from_y,
array('top', 'center', 'bottom')
);
}
}
// Make sure the user input is valid
if (!is_numeric($crop_from_x) || $crop_from_x < 0 || $crop_from_x > $orig_width - 1) {
throw new fProgrammerException(
'The crop-from x specified, %s, is not a number, is less than zero, or would result in a zero-width image',
$crop_from_x
);
}
if (!is_numeric($crop_from_y) || $crop_from_y < 0 || $crop_from_y > $orig_height - 1) {
throw new fProgrammerException(
'The crop-from y specified, %s, is not a number, is less than zero, or would result in a zero-height image',
$crop_from_y
);
}
if (!is_numeric($new_width) || $new_width <= 0 || $crop_from_x + $new_width > $orig_width) {
throw new fProgrammerException(
'The new width specified, %1$s, is not a number, is less than or equal to zero, or is larger than can be cropped with the specified crop-from x of %2$s',
$new_width,
$crop_from_x
);
}
if (!is_numeric($new_height) || $new_height <= 0 || $crop_from_y + $new_height > $orig_height) {
throw new fProgrammerException(
'The new height specified, %1$s, is not a number, is less than or equal to zero, or is larger than can be cropped with the specified crop-from y of %2$s',
$new_height,
$crop_from_y
);
}
// If nothing changed, don't even record the modification
if ($orig_width == $new_width && $orig_height == $new_height) {
return $this;
}
// Record what we are supposed to do
$this->pending_modifications[] = array(
'operation' => 'crop',
'start_x' => $crop_from_x,
'start_y' => $crop_from_y,
'width' => $new_width,
'height' => $new_height,
'old_width' => $orig_width,
'old_height' => $orig_height
);
return $this;
}
/**
* Crops the biggest area possible from the center of the image that matches the ratio provided
*
* The crop does not occur until ::saveChanges() is called.
*
* @param numeric $ratio_width The width ratio to crop the image to
* @param numeric $ratio_height The height ratio to crop the image to
* @param string $horizontal_position A horizontal position of `'left'`, `'center'` or `'right'`
* @param string $vertical_position A vertical position of `'top'`, `'center'` or `'bottom'`
* @return fImage The image object, to allow for method chaining
*/
public function cropToRatio($ratio_width, $ratio_height, $horizontal_position='center', $vertical_position='center')
{
$this->tossIfDeleted();
// Make sure the user input is valid
if ((!is_numeric($ratio_width) && $ratio_width !== NULL) || $ratio_width < 0) {
throw new fProgrammerException(
'The ratio width specified, %s, is not a number or is less than or equal to zero',
$ratio_width
);
}
if ((!is_numeric($ratio_height) && $ratio_height !== NULL) || $ratio_height < 0) {
throw new fProgrammerException(
'The ratio height specified, %s, is not a number or is less than or equal to zero',
$ratio_height
);
}
// Make sure
$valid_horizontal_positions = array('left', 'center', 'right');
if (!in_array(strtolower($horizontal_position), $valid_horizontal_positions)) {
throw new fProgrammerException(
'The horizontal position specified, %1$s, is not valid. Must be one of: %2$s.',
$horizontal_position,
$valid_horizontal_positions
);
}
$valid_vertical_positions = array('top', 'center', 'bottom');
if (!in_array(strtolower($vertical_position), $valid_vertical_positions)) {
throw new fProgrammerException(
'The vertical position specified, %1$s, is not valid. Must be one of: %2$s.',
$vertical_position,
$valid_vertical_positions
);
}
// Get the new dimensions
$dim = $this->getCurrentDimensions();
$orig_width = $dim['width'];
$orig_height = $dim['height'];
$orig_ratio = $orig_width / $orig_height;
$new_ratio = $ratio_width / $ratio_height;
if ($orig_ratio > $new_ratio) {
$new_height = $orig_height;
$new_width = round($new_ratio * $new_height);
} else {
$new_width = $orig_width;
$new_height = round($new_width / $new_ratio);
}
return $this->crop($new_width, $new_height, $horizontal_position, $vertical_position);
}
/**
* Converts the image to grayscale
*
* Desaturation does not occur until ::saveChanges() is called.
*
* @return fImage The image object, to allow for method chaining
*/
public function desaturate()
{
$this->tossIfDeleted();
$dim = $this->getCurrentDimensions();
// Record what we are supposed to do
$this->pending_modifications[] = array(
'operation' => 'desaturate',
'width' => $dim['width'],
'height' => $dim['height'],
'old_width' => $dim['width'],
'old_height' => $dim['height']
);
return $this;
}
/**
* Gets the dimensions of the image as of the last modification
*
* @return array An associative array: `'width' => {integer}, 'height' => {integer}`
*/
private function getCurrentDimensions()
{
if (empty($this->pending_modifications)) {
$output = self::getInfo($this->file);
unset($output['type']);
} else {
$last_modification = $this->pending_modifications[sizeof($this->pending_modifications)-1];
$output['width'] = $last_modification['width'];
$output['height'] = $last_modification['height'];
}
return $output;
}
/**
* Returns the width and height of the image as a two element array
*
* @return array In the format `0 => (integer) {width}, 1 => (integer) {height}`
*/
public function getDimensions()
{
$info = self::getInfo($this->file);
return array($info['width'], $info['height']);
}
/**
* Returns the height of the image
*
* @return integer The height of the image in pixels
*/
public function getHeight()
{
return self::getInfo($this->file, 'height');
}
/**
* Returns the type of the image
*
* @return string The type of the image: `'jpg'`, `'gif'`, `'png'`, `'tif'`
*/
public function getType()
{
return self::getImageType($this->file);
}
/**
* Returns the width of the image
*
* @return integer The width of the image in pixels
*/
public function getWidth()
{
return self::getInfo($this->file, 'width');
}
/**
* Checks if the current image is an animated gif
*
* @return boolean If the image is an animated gif
*/
private function isAnimatedGif()
{
$type = self::getImageType($this->file);
if ($type == 'gif') {
if (preg_match('#\x00\x21\xF9\x04.{4}\x00\x2C#s', file_get_contents($this->file))) {
return TRUE;
}
}
return FALSE;
}
/**
* Processes the current image using GD
*
* @param string $output_file The file to save the image to
* @param integer $jpeg_quality The JPEG quality to use
* @return void
*/
private function processWithGD($output_file, $jpeg_quality)
{
$type = self::getImageType($this->file);
$save_alpha = FALSE;
$path_info = fFilesystem::getPathInfo($output_file);
$new_type = $path_info['extension'];
$new_type = ($type == 'jpeg') ? 'jpg' : $type;
if (!in_array($new_type, array('gif', 'jpg', 'png'))) {
$new_type = $type;
}
if (ini_get('memory_limit') != '-1') {
// We will estimate memory usage at 3MB if we can't actually check it
$beginning_memory_usage = 3145728;
if (function_exists('memory_get_usage')) {
$beginning_memory_usage = memory_get_usage();
}
$memory_limit_bytes = fFilesystem::convertToBytes(ini_get('memory_limit'));
// Estimate the memory usage and throw an exception if we will run out
$load_byte_usage = $this->pending_modifications[0]['old_width'] * $this->pending_modifications[0]['old_height'] * 4;
if ($load_byte_usage + $beginning_memory_usage > $memory_limit_bytes) {
throw new fEnvironmentException(
'The predicted memory usage to complete the image modifications using the GD extension, %1$s, will most likely exceed the memory limit of %2$s',
$load_byte_usage + $beginning_memory_usage,
$memory_limit_bytes
);
}
}
switch ($type) {
case 'gif':
$gd_res = imagecreatefromgif($this->file);
$save_alpha = TRUE;
break;
case 'jpg':
$gd_res = imagecreatefromjpeg($this->file);
break;
case 'png':
$gd_res = imagecreatefrompng($this->file);
$save_alpha = TRUE;
break;
}
foreach ($this->pending_modifications as $num => $mod) {
if (ini_get('memory_limit') != '-1') {
$old_byte_usage = $this->pending_modifications[0]['old_width'] * $this->pending_modifications[0]['old_height'] * 4;
$new_byte_usage = $this->pending_modifications[0]['width'] * $this->pending_modifications[0]['height'] * 4;
if ($old_byte_usage + $new_byte_usage + $beginning_memory_usage > $memory_limit_bytes) {
throw new fEnvironmentException(
'The predicted memory usage to complete the image modifications using the GD extension, %1$s, will most likely exceed the memory limit of %2$s',
$old_byte_usage + $new_byte_usage + $beginning_memory_usage,
$memory_limit_bytes
);
}
}
$new_gd_res = imagecreatetruecolor($mod['width'], $mod['height']);
if ($save_alpha) {
imagealphablending($new_gd_res, FALSE);
imagesavealpha($new_gd_res, TRUE);
if ($new_type == 'gif') {
$transparent = imagecolorallocatealpha($new_gd_res, 255, 255, 255, 127);
imagefilledrectangle($new_gd_res, 0, 0, $mod['width'], $mod['height'], $transparent);
imagecolortransparent($new_gd_res, $transparent);
}
}
// Perform the resize operation
if ($mod['operation'] == 'resize') {
imagecopyresampled($new_gd_res, $gd_res,
0, 0,
0, 0,
$mod['width'], $mod['height'],
$mod['old_width'], $mod['old_height']);
// Perform the crop operation
} elseif ($mod['operation'] == 'crop') {
imagecopyresampled($new_gd_res, $gd_res,
0, 0,
$mod['start_x'], $mod['start_y'],
$mod['width'], $mod['height'],
$mod['width'], $mod['height']);
// Perform the desaturate operation
} elseif ($mod['operation'] == 'desaturate') {
// Create a palette of grays
$grays = array();
for ($i=0; $i < 256; $i++) {
$grays[$i] = imagecolorallocate($new_gd_res, $i, $i, $i);
}
$transparent = imagecolorallocatealpha($new_gd_res, 255, 255, 255, 127);
// Loop through every pixel and convert the rgb values to grays
for ($x=0; $x < $mod['width']; $x++) {
for ($y=0; $y < $mod['height']; $y++) {
$color = imagecolorat($gd_res, $x, $y);