This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathImageTool.php
1455 lines (1222 loc) · 38.4 KB
/
ImageTool.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
/**
* Image tool 1.4.2
*
* Different tools/functions to perform various tasks w/ images
*/
class ImageTool {
/**
* Place watermark on image
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'watermark' Watermark file (path or gd resource)
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'chmod' What permissions should be applied to destination image
* - 'scale' If true, watermark will be scaled fullsize ('position' and 'repeat' won't be taken into account)
* - 'strech' If true and scale also set to true, strech watermark to cover whole image
* - 'repeat' Should watermark be repeated? This is ignored if 'scale' is set to true or 'position' is custom (array)
* - 'position' Watermark position. Possible values: 'top-left', 'top-right', 'bottom-right', 'bottom-left', 'center' or [x, y]
* - 'opacity' Watermark image's opacity (0-100). Default = 100
* - 'afterCallbacks' Functions to be executed after this one
*
* @param array $options An array of options.
* @return mixed boolean or GD resource if output was set to null
*/
public static function watermark($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'scale' => false,
'strech' => false,
'repeat' => false,
'watermark' => null,
'output' => null,
'input' => null,
'position' => 'center',
'compression' => 9,
'quality' => 100,
'chmod' => null,
'opacity' => 100
], $options);
// if output path (directories) doesn't exist, try to make whole path
if (!self::createPath($options['output'])) {
return false;
}
$img = self::openImage($options['input']);
unset($options['input']);
if (empty($img)) {
return false;
}
$src_wm = self::openImage($options['watermark']);
unset($options['watermark']);
if (empty($src_wm)) {
return false;
}
// image size
$img_im_w = imagesx($img);
$img_im_h = imagesy($img);
// watermark size
$img_wm_w = imagesx($src_wm);
$img_wm_h = imagesy($src_wm);
if ($options['scale']) {
if ($options['strech']) {
$r = imagecopyresampled($img, $src_wm, 0, 0, 0, 0, $img_im_w, $img_im_h, $img_wm_w, $img_wm_h);
} else {
$x = 0;
$y = 0;
$w = $img_im_w;
$h = $img_im_h;
if (($img_im_w / $img_im_h) > ($img_wm_w / $img_wm_h)) {
$ratio = $img_im_h / $img_wm_h;
$w = $ratio * $img_wm_w;
$x = round(($img_im_w - $w) / 2);
} else {
$ratio = $img_im_w / $img_wm_w;
$h = $ratio * $img_wm_h;
$y = round(($img_im_h - $h) / 2);
}
$r = imagecopyresampled($img, $src_wm, $x, $y, 0, 0, $w, $h, $img_wm_w, $img_wm_h);
}
} else if ($options['repeat']) {
if (is_array($options['position'])) {
$options['position'] = 5;
}
switch ($options['position']) {
case 'top-left':
for ($y=0; $y<$img_im_h; $y+=$img_wm_h) {
for ($x=0; $x<$img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'top-right':
for ($y=0; $y<$img_im_h; $y+=$img_wm_h) {
for ($x=$img_im_w; $x>-$img_wm_w; $x-=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'bottom-right':
for ($y=$img_im_h; $y>-$img_wm_h; $y-=$img_wm_h) {
for ($x=$img_im_w; $x>-$img_wm_w; $x-=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'bottom-left':
for ($y=$img_im_h; $y>-$img_wm_h; $y-=$img_wm_h) {
for ($x=0; $x<$img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
case 'center':
default:
$pos_x = -(($img_im_w%$img_wm_w)/2);
$pos_y = -(($img_im_h%$img_wm_h)/2);
for ($y=$pos_y; $y<$img_im_h; $y+=$img_wm_h) {
for ($x=$pos_x; $x<$img_im_w; $x+=$img_wm_w) {
$r = self::imagecopymerge_alpha($img, $src_wm, $x, $y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
}
break;
}
} else {
// custom location
if (is_array($options['position'])) {
list($pos_x, $pos_y) = $options['position'];
} else {
// predefined location
switch ($options['position']) {
case 'top-left':
$pos_x = 0;
$pos_y = 0;
break;
case 'top-right':
$pos_x = $img_im_w - $img_wm_w;
$pos_y = 0;
break;
case 'bottom-right':
$pos_x = $img_im_w - $img_wm_w;
$pos_y = $img_im_h - $img_wm_h;
break;
case 'bottom-left':
$pos_x = 0;
$pos_y = $img_im_h - $img_wm_h;
break;
case 'center':
default:
$pos_x = round(($img_im_w - $img_wm_w) / 2);
$pos_y = round(($img_im_h - $img_wm_h) / 2);
break;
}
}
$r = self::imagecopymerge_alpha($img, $src_wm, $pos_x, $pos_y, 0, 0, $img_wm_w, $img_wm_h, $options['opacity']);
}
if (!$r) {
return false;
}
if (!self::afterCallbacks($img, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($img, $options);
}
/**
* Resize image
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'units' Scale units. Percents ('%') and pixels ('px') are avalaible
* - 'enlarge' if set to false and width or height of the destination image is bigger than source image's width or height, then leave source image's dimensions untouched
* - 'chmod' What permissions should be applied to destination image
* - 'paddings' If not empty and both output width and height is specified and mode=fit, padding borders are applied. You can specify color here. If true, then white color will be applied
* - 'mode' How to handle image size. Possible options: crop, fit and stretch
* - 'afterCallbacks' Functions to be executed after resize. Example: [['unsharpMask', 70, 3.9, 0]]; First passed argument is f-ion name. Executed function's first argument must be gd image instance
* - 'height' Output image's height. If left empty, this is auto calculated (if possible)
* - 'width' Output image's width. If left empty, this is auto calculated (if possible)
*
* @param array $options An array of options
* @return mixed boolean or GD resource if output was set to null
*/
public static function resize($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'compression' => null,
'paddings' => false,
'enlarge' => true,
'quality' => null,
'chmod' => null,
'mode' => 'crop',
'units' => 'px',
'height' => null,
'output' => null,
'width' => null,
'input' => null
], $options);
// if output path (directories) doesn't exist, try to make whole path
if (!self::createPath($options['output'])) {
return false;
}
$input_extension = self::getImageType($options['input']);
$output_extension = self::getExtension($options['output']);
$src_im = self::openImage($options['input']);
if (!$src_im) {
return false;
}
// calculate new w, h, x and y
if (!empty($options['width']) && !is_numeric($options['width'])) {
return false;
}
if (!empty($options['height']) && !is_numeric($options['height'])) {
return false;
}
// get size of the original image
$input_width = imagesx($src_im);
$input_height = imagesy($src_im);
//calculate destination image w/h
// turn % into px
if ($options['units'] == '%') {
if ($options['height'] != null) {
$options['height'] = round($input_height * $options['height'] / 100);
}
if ($options['width'] != null) {
$options['width'] = round($input_width * $options['width'] / 100);
}
}
// if mode=fit, check output width/height and update them as neccessary
if ($options['mode'] === 'fit' && $options['width'] != null && $options['height'] != null) {
$input_ratio = $input_width / $input_height;
$output_ratio = $options['width'] / $options['height'];
$original_width = $options['width'];
$original_height = $options['height'];
if ($input_ratio > $output_ratio) {
$options['height'] = $input_height * $options['width'] / $input_width;
} else {
$options['width'] = $input_width * $options['height'] / $input_height;
}
}
// calculate missing width/height (if any)
if ($options['width'] == null && $options['height'] == null) {
$options['width'] = $input_width;
$options['height'] = $input_height;
} else if ($options['height'] == null) {
$options['height'] = round(($options['width'] * $input_height) / $input_width);
} else if ($options['width'] == null) {
$options['width'] = round(($options['height'] * $input_width) / $input_height);
}
$src_x = 0;
$src_y = 0;
$src_w = $input_width;
$src_h = $input_height;
if ($options['enlarge'] == false && ($options['width'] > $input_width || $options['height'] > $input_height)) {
$options['width'] = $input_width;
$options['height'] = $input_height;
} else if ($options['mode'] === 'crop') {
if (($input_width / $input_height) > ($options['width'] / $options['height'])) {
$ratio = $input_height / $options['height'];
$src_w = $ratio * $options['width'];
$src_x = round(($input_width - $src_w) / 2);
} else {
$ratio = $input_width / $options['width'];
$src_h = $ratio * $options['height'];
$src_y = round(($input_height - $src_h) / 2);
}
}
// if possible, just move file w/o modifying it
$is_local = is_string($options['input']) && !preg_match('/^https?:\/\//', $options['input']);
$is_same_type = $input_extension === $output_extension;
$is_same_size = $input_width === $options['width'] && $input_height === $options['height'];
if ($is_same_size && $is_same_type && $is_local && empty($options['afterCallbacks'])) {
$r = copy($options['input'], $options['output']);
if (!empty($options['chmod'])) {
chmod($options['output'], $options['chmod']);
}
return $r;
}
$dst_im = imagecreatetruecolor($options['width'], $options['height']);
if (!$dst_im) {
imagedestroy($src_im);
return false;
}
// transparency or white bg instead of black
if (in_array($input_extension, ['png', 'gif'])) {
if (in_array($output_extension, ['png', 'gif'])) {
imagealphablending($dst_im, false);
imagesavealpha($dst_im, true);
$transparent = imagecolorallocatealpha($dst_im, 255, 255, 255, 127);
imagefilledrectangle($dst_im, 0, 0,$options['width'], $options['height'], $transparent);
} else {
$white = imagecolorallocate($dst_im, 255, 255, 255);
imagefilledrectangle($dst_im, 0, 0, $options['width'], $options['height'], $white);
}
}
$r = imagecopyresampled($dst_im, $src_im, 0, 0, $src_x, $src_y, $options['width'], $options['height'], $src_w, $src_h);
if (!$r) {
imagedestroy($src_im);
return false;
}
if ($options['mode'] === 'fit' && $options['paddings']) {
if ($options['width'] != $original_width || $options['height'] != $original_height) {
$bg_im = imagecreatetruecolor($original_width, $original_height);
if (!$bg_im) {
imagedestroy($bg_im);
return false;
}
if ($options['paddings'] === true) {
$rgb = [255, 255, 255];
} else {
$rgb = self::readColor($options['paddings']);
if (!$rgb) {
$rgb = [255, 255, 255];
}
}
$color = imagecolorallocate($bg_im, $rgb[0], $rgb[1], $rgb[2]);
imagefilledrectangle($bg_im, 0, 0, $original_width, $original_height, $color);
$x = round(($original_width - $options['width']) / 2);
$y = round(($original_height - $options['height']) / 2);
imagecopy($bg_im, $dst_im, $x, $y, 0, 0, $options['width'], $options['height']);
$dst_im = $bg_im;
}
}
if (!self::afterCallbacks($dst_im, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($dst_im, $options);
}
/**
* Apply unsharp mask to image
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Unsharp Mask for PHP - version 2.1.1
* Unsharp mask algorithm by Torstein Hønsi 2003-07.
* thoensi_at_netcom_dot_no.
* Please leave this notice.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'afterCallbacks' Functions to be executed after this one
* - 'chmod' What permissions should be applied to destination image
* - 'threshold'
* - 'amount'
* - 'radius'
*
* @param array $options An array of options.
* @return mixed boolean or GD resource if output was set to null
*/
public static function unsharpMask($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'compression' => null,
'quality' => null,
'threshold' => 3,
'amount' => 50,
'radius' => 0.5,
'output' => null,
'input' => null,
'chmod' => null
], $options);
$img = self::openImage($options['input']);
unset($options['input']);
if (!$img) {
return false;
}
// Attempt to calibrate the parameters to Photoshop:
if ($options['amount'] > 500) {
$options['amount'] = 500;
}
$options['amount'] = $options['amount'] * 0.016;
if ($options['radius'] > 50) {
$options['radius'] = 50;
}
$options['radius'] = $options['radius'] * 2;
if ($options['threshold'] > 255) {
$options['threshold'] = 255;
}
// Only integers make sense.
$options['radius'] = abs(round($options['radius']));
if ($options['radius'] == 0) {
return self::saveImage($img, $options);
}
$w = imagesx($img);
$h = imagesy($img);
$imgCanvas = imagecreatetruecolor($w, $h);
$imgBlur = imagecreatetruecolor($w, $h);
// PHP >= 5.1
if (function_exists('imageconvolution')) {
$matrix = [
[1, 2, 1],
[2, 4, 2],
[1, 2, 1]
];
imagecopy ($imgBlur, $img, 0, 0, 0, 0, $w, $h);
imageconvolution($imgBlur, $matrix, 16, 0);
} else {
// Move copies of the image around one pixel at the time and merge them with weight
// according to the matrix. The same matrix is simply repeated for higher radii.
for ($i = 0; $i < $options['radius']; $i++) {
imagecopy ($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h); // left
imagecopymerge ($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50); // right
imagecopymerge ($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50); // center
imagecopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333 ); // up
imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); // down
}
}
if($options['threshold'] > 0) {
// Calculate the difference between the blurred pixels and the original
// and set the pixels
for ($x = 0; $x < $w-1; $x++) { // each row
for ($y = 0; $y < $h; $y++) { // each pixel
$rgbOrig = ImageColorAt($img, $x, $y);
$rOrig = (($rgbOrig >> 16) & 0xFF);
$gOrig = (($rgbOrig >> 8) & 0xFF);
$bOrig = ($rgbOrig & 0xFF);
$rgbBlur = ImageColorAt($imgBlur, $x, $y);
$rBlur = (($rgbBlur >> 16) & 0xFF);
$gBlur = (($rgbBlur >> 8) & 0xFF);
$bBlur = ($rgbBlur & 0xFF);
// When the masked pixels differ less from the original
// than the threshold specifies, they are set to their original value.
$rNew = (abs($rOrig - $rBlur) >= $options['threshold']) ? max(0, min(255, ($options['amount'] * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
$gNew = (abs($gOrig - $gBlur) >= $options['threshold']) ? max(0, min(255, ($options['amount'] * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
$bNew = (abs($bOrig - $bBlur) >= $options['threshold']) ? max(0, min(255, ($options['amount'] * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
$pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
ImageSetPixel($img, $x, $y, $pixCol);
}
}
}
} else {
for ($x = 0; $x < $w; $x++) { // each row
for ($y = 0; $y < $h; $y++) { // each pixel
$rgbOrig = ImageColorAt($img, $x, $y);
$rOrig = (($rgbOrig >> 16) & 0xFF);
$gOrig = (($rgbOrig >> 8) & 0xFF);
$bOrig = ($rgbOrig & 0xFF);
$rgbBlur = ImageColorAt($imgBlur, $x, $y);
$rBlur = (($rgbBlur >> 16) & 0xFF);
$gBlur = (($rgbBlur >> 8) & 0xFF);
$bBlur = ($rgbBlur & 0xFF);
$rNew = ($options['amount'] * ($rOrig - $rBlur)) + $rOrig;
if($rNew>255){$rNew=255;}
elseif($rNew<0){$rNew=0;}
$gNew = ($options['amount'] * ($gOrig - $gBlur)) + $gOrig;
if($gNew>255){$gNew=255;}
elseif($gNew<0){$gNew=0;}
$bNew = ($options['amount'] * ($bOrig - $bBlur)) + $bOrig;
if($bNew>255){$bNew=255;}
elseif($bNew<0){$bNew=0;}
$rgbNew = ($rNew << 16) + ($gNew <<8) + $bNew;
ImageSetPixel($img, $x, $y, $rgbNew);
}
}
}
if (!self::afterCallbacks($img, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($img, $options);
}
/**
* Get file extension
*
* @param string $filename Filename
* @return string
*/
public static function getExtension($filename) {
if (!is_string($filename)) {
return '';
}
$pos = strrpos($filename, '.');
if ($pos === false) {
return '';
}
return strtolower(substr($filename, $pos + 1));
}
/**
* Open image as gd resource
*
* @param string $input Input (path) image
* @return mixed
*/
public static function openImage($input) {
if (is_resource($input)) {
if (get_resource_type($input) == 'gd') {
return $input;
}
} else {
if (is_string($input) && preg_match('/^https?:\/\//', $input)) {
$image = file_get_contents($input);
if (!$image) {
return false;
}
return imagecreatefromstring($image);
}
switch (self::getImageType($input)) {
case 'jpg':
return imagecreatefromjpeg($input);
break;
case 'png':
return imagecreatefrompng($input);
break;
case 'gif':
return imagecreatefromgif($input);
break;
case 'webp':
return imagecreatefromwebp($input);
break;
}
}
return false;
}
/**
* Get image type from file
*
* @param string $input Input (path) image
* @param string $extension (optional) Extension (type)
* @param boolean $extension If true, check by extension
* @return string
*/
public static function getImageType($input, $extension = false) {
if ($extension) {
switch (self::getExtension($input)) {
case 'jpg':
case 'jpeg':
return 'jpg';
break;
case 'png':
return 'png';
break;
case 'gif':
return 'gif';
break;
case 'webp':
return 'webp';
break;
}
} else if (is_string($input) && is_file($input)) {
$info = getimagesize($input);
switch ($info['mime']) {
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
return 'jpg';
break;
case 'image/x-png':
case 'image/png':
return 'png';
break;
case 'image/gif':
return 'gif';
break;
case 'image/webp':
return 'webp';
break;
}
}
return '';
}
/**
* Save image gd resource as image
*
* Image type is determined by $output extension so it must be present.
*
* Options:
* - 'compression' Output image's compression. Currently only PNG (value 0-9) supports this
* - 'quality' Output image's quality. Currently only JPG (value 0-100) supports this
* - 'output' Output path. If not specified, image resource is returned
*
* @param mixed $im Image resource
* @param string $output Output path
* @param mixed $options An array of additional options
* @return boolean
*/
public static function saveImage(&$im, $options = []) {
foreach (['compression', 'quality', 'chmod'] as $v) {
if (is_null($options[$v])) {
unset($options[$v]);
}
}
$options = array_merge([
'compression' => 9,
'quality' => 100,
'output' => null
], $options);
switch (self::getImageType($options['output'], true)) {
case 'jpg':
if (ImageJPEG($im, $options['output'], $options['quality'])) {
if (!empty($options['chmod'])) {
chmod($options['output'], $options['chmod']);
}
return true;
}
break;
case 'png':
if (ImagePNG($im, $options['output'], $options['compression'])) {
if (!empty($options['chmod'])) {
chmod($options['output'], $options['chmod']);
}
return true;
}
break;
case 'gif':
if (ImageGIF($im, $options['output'])) {
if (!empty($options['chmod'])) {
chmod($options['output'], $options['chmod']);
}
return true;
}
break;
case '':
return $im;
break;
}
unset($im);
return false;
}
/**
* Try to create specified path
*
* If specified path is empty, return true
*
* @param string $output_path
* @param mixed $chmod Each folder's permissions
* @return boolean
*/
public static function createPath($output_path, $chmod = 0777) {
if (empty($output_path)) {
return true;
}
$arr_output_path = explode(DIRECTORY_SEPARATOR, $output_path);
unset($arr_output_path[count($arr_output_path)-1]);
$dir_path = implode(DIRECTORY_SEPARATOR, $arr_output_path).DIRECTORY_SEPARATOR;
if (!file_exists($dir_path)) {
if (!mkdir($dir_path, $chmod, true)) {
return false;
}
}
return true;
}
/**
* Autorotate image
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'afterCallbacks' Functions to be executed after this one
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'chmod' What permissions should be applied to destination image
*
* @param mixed $options An array of options
* @return mixed boolean or GD resource if output was set to null
*/
public static function autorotate($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'compression' => null,
'quality' => null,
'input' => null,
'output' => null,
'chmod' => null
], $options);
$type = self::getImageType($options['input']);
if ($type == 'jpg' && function_exists('exif_read_data')) {
$exif = exif_read_data($options['input']);
}
$src_im = self::openImage($options['input']);
unset($options['input']);
if (!$src_im) {
return false;
}
if (!empty($exif['Orientation'])) {
$orientation = $exif['Orientation'];
} else if (!empty($exif['IFD0']['Orientation'])) {
$orientation = $exif['IFD0']['Orientation'];
} else {
return self::saveImage($src_im, $options);
}
switch ($orientation) {
case 1:
return self::saveImage($src_im, $options);
break;
case 2: // horizontal flip
$dst_im = self::flip(['input' => $src_im, 'mode' => 'horizontal']);
break;
case 3: // 180 rotate left
$dst_im = self::rotate(['input' => $src_im, 'degrees' => 180]);
break;
case 4: // vertical flip
$dst_im = self::flip(['input' => $src_im, 'mode' => 'vertical']);
break;
case 5: // vertical flip + 90 rotate right
$dst_im = self::flip(['input' => $src_im, 'mode' => 'vertical']);
$dst_im = self::rotate(['input' => $src_im, 'degrees' => 90]);
break;
case 6: // 90 rotate right
$dst_im = self::rotate(['input' => $src_im, 'degrees' => 90]);
break;
case 7: // horizontal flip + 90 rotate right
$dst_im = self::flip(['input' => $src_im, 'mode' => 'horizontal']);
$dst_im = self::rotate(['input' => $src_im, 'degrees' => 90]);
break;
case 8: // 90 rotate left
$dst_im = self::rotate(['input' => $src_im, 'degrees' => 270]);
break;
default:
return self::saveImage($src_im, $options);
}
if (!$dst_im) {
return false;
}
if (!self::afterCallbacks($dst_im, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($dst_im, $options);
}
/**
* Rotate image by specified angle (only agles divisable by 90 are supported)
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'degrees' Degrees to rotate by (divisible by 90)
* - 'afterCallbacks' Functions to be executed after this one
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'chmod' What permissions should be applied to destination image
*
* @param mixed $options An array of options
* @return mixed boolean or GD resource if output was set to null
*/
public static function rotate($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'compression' => null,
'quality' => null,
'input' => null,
'output' => null,
'degrees' => 'horizontal',
'chmod' => null
], $options);
$src_im = self::openImage($options['input']);
unset($options['input']);
if (!$src_im) {
return false;
}
$w = imagesx($src_im);
$h = imagesy($src_im);
switch ($options['degrees']) {
case 90:
$dst_im = imagecreatetruecolor($h, $w);
break;
case 180:
$dst_im = imagecreatetruecolor($w, $h);
break;
case 270:
$dst_im = imagecreatetruecolor($h, $w);
break;
case 360:
case 0:
return self::saveImage($src_im, $options);
break;
default:
return false;
}
if (!$dst_im) {
return false;
}
for ($i=0; $i<$w; $i++) {
for ($j=0; $j<$h; $j++) {
$reference = imagecolorat($src_im, $i, $j);
switch ($options['degrees']) {
case 90:
if (!@imagesetpixel($dst_im, ($h-1)-$j, $i, $reference)) {
return false;
}
break;
case 180:
if (!@imagesetpixel($dst_im, $w-$i, ($h-1)-$j, $reference)) {
return false;
}
break;
case 270:
if (!@imagesetpixel($dst_im, $j, $w-$i, $reference)) {
return false;
}
break;
}
}
}
if (!self::afterCallbacks($dst_im, $options['afterCallbacks'])) {
return false;
}
return self::saveImage($dst_im, $options);
}
/**
* Flip image
*
* Options:
* - 'input' Input file (path or gd resource)
* - 'output' Output path. If not specified, gd resource is returned
* - 'mode' Flip mode: horizontal, vertical, both
* - 'afterCallbacks' Functions to be executed after this one
* - 'quality' Output image quality (JPG only). Value from 0 to 100
* - 'compression' Output image compression (PNG only). Value from 0 to 9
* - 'chmod' What permissions should be applied to destination image
*
* @param mixed $options An array of options
* @return mixed boolean or GD resource if output was set to null
*/
public static function flip($options = []) {
$options = array_merge([
'afterCallbacks' => null,
'compression' => null,
'quality' => null,
'input' => null,
'output' => null,
'mode' => 'horizontal',
'chmod' => null
], $options);
$src_im = self::openImage($options['input']);
unset($options['input']);
if (!$src_im) {
return false;
}
$w = imagesx($src_im);
$h = imagesy($src_im);
$dst_im = imagecreatetruecolor($w, $h);
switch ($options['mode']) {
case 'horizontal':
for ($x=0 ; $x<$w ; $x++) {
for ($y=0 ; $y<$h ; $y++) {
imagecopy($dst_im, $src_im, $w-$x-1, $y, $x, $y, 1, 1);
}
}
break;