-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathZebra_Image.php
2127 lines (1706 loc) · 86.4 KB
/
Zebra_Image.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
/**
* Methods used with the {@link resize()} method.
*/
define('ZEBRA_IMAGE_BOXED', 0);
define('ZEBRA_IMAGE_NOT_BOXED', 1);
define('ZEBRA_IMAGE_CROP_TOPLEFT', 2);
define('ZEBRA_IMAGE_CROP_TOPCENTER', 3);
define('ZEBRA_IMAGE_CROP_TOPRIGHT', 4);
define('ZEBRA_IMAGE_CROP_MIDDLELEFT', 5);
define('ZEBRA_IMAGE_CROP_CENTER', 6);
define('ZEBRA_IMAGE_CROP_MIDDLERIGHT', 7);
define('ZEBRA_IMAGE_CROP_BOTTOMLEFT', 8);
define('ZEBRA_IMAGE_CROP_BOTTOMCENTER', 9);
define('ZEBRA_IMAGE_CROP_BOTTOMRIGHT', 10);
// this enables handling of partially broken JPEG files without warnings/errors
ini_set('gd.jpeg_ignore_warning', '1');
/**
* A single-file, lightweight PHP library designed for efficient image manipulation featuring methods for modifying
* images and applying filters Supports WEBP format.
*
* Read more {@link https://github.com/stefangabos/Zebra_Image/ here}
*
* @author Stefan Gabos <[email protected]>
* @version 2.8.2 (last revision: January 25, 2023)
* @copyright © 2006 - 2023 Stefan Gabos
* @license https://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE
* @package Zebra_Image
*/
class Zebra_Image {
/**
* If set to `true`, JPEG images will be auto-rotated according to the {@link http://keyj.emphy.de/exif-orientation-rant/ Exif Orientation Tag}
* so that they are always shown correctly.
*
* > If set to `true` you must also enable exif-support with `--enable-exif`.<br>
* Windows users must enable both the `php_mbstring.dll` and `php_exif.dll` DLL's in `php.ini`.<br>
* `php_mbstring.dll` must be loaded before `php_exif.dll`, so adjust your php.ini accordingly.
* See {@link https://www.php.net/manual/en/exif.installation.php the manual}.
*
* Default is `false`
*
* @since 2.2.4
*
* @var boolean
*/
public $auto_handle_exif_orientation;
/**
* Indicates whether BMP images should be compressed with run-length encoding (RLE), or not.
*
* > Used only if PHP version is `7.2.0+` and the file at {@link target_path} is a `BMP` image, or it will be
* ignored otherwise.
*
* Default is `TRUE`
*
* @since 2.8.1
*
* @var boolean
*/
public $bmp_compressed;
/**
* Indicates the file system permissions to be set for newly created images.
*
* Better is to leave this setting as it is.
*
* If you know what you are doing, here is how you can calculate the permission levels:
*
* - 400 Owner Read
* - 200 Owner Write
* - 100 Owner Execute
* - 40 Group Read
* - 20 Group Write
* - 10 Group Execute
* - 4 Global Read
* - 2 Global Write
* - 1 Global Execute
*
* Default is `0755`
*
* @var integer
*/
public $chmod_value;
/**
* If set to `false`, images having both width and height smaller than the required width and height will be left
* untouched. {@link jpeg_quality} and {@link png_compression} will still apply!
*
* Available only for the {@link resize()} method.
*
* Default is `true`
*
* @var boolean
*/
public $enlarge_smaller_images;
/**
* In case of an error read this property's value to see the error's code.
*
* Possible error codes are:
*
* - `1` - source file could not be found
* - `2` - source file is not readable
* - `3` - could not write target file
* - `4` - unsupported source file type *(note that you will also get this for animated WEBP images!)*
* - `5` - unsupported target file type
* - `6` - GD library version does not support target file format
* - `7` - GD library is not installed!
* - `8` - "chmod" command is disabled via configuration
* - `9` - "exif_read_data" function is not available
*
* Default is `0` (no error)
*
* @var integer
*/
public $error;
/**
* Indicates whether the created image should be saved as a progressive JPEG.
*
* > Used only if the file at {@link target_path} is a `JPG/JPEG` image, or will be ignored otherwise.
*
* Default is `false`
*
* @since 2.5.0
*
* @var boolean
*/
public $jpeg_interlace;
/**
* Indicates the quality of the output image (better quality means bigger file size).
*
* > Used only if the file at {@link target_path} is a `JPG/JPEG` image, or it will be ignored otherwise.
*
* Range is `0` - `100`.
*
* Default is `85`
*
* @var integer
*/
public $jpeg_quality;
/**
* Indicates the compression level of the output image (lower compression means bigger file size).
*
* > Used only if PHP version is `5.1.2+` and the file at {@link target_path} is a `PNG` image, or it will be
* ignored otherwise.
*
* Range is `0` - `9`.
*
* Default is `9`
*
* @since 2.2
*
* @var integer
*/
public $png_compression;
/**
* Specifies whether upon resizing images should preserve their aspect ratio.
*
* > Available only for the {@link resize()} method.
*
* Default is `true`
*
* @var boolean
*/
public $preserve_aspect_ratio;
/**
* Indicates whether a target file should preserve the source file's date/time.
*
* Default is `true`
*
* @since 1.0.4
*
* @var boolean
*/
public $preserve_time;
/**
* Indicates whether the target image should have a `sharpen` filter applied to it.
*
* Can be very useful when creating thumbnails and should be used only when creating thumbnails.
*
* > The sharpen filter relies on the {@link https://www.php.net/manual/en/function.imageconvolution.php imageconvolution}
* function which is available for PHP version `5.1.0+`, and will leave the images unaltered for older versions!
*
* Default is `false`
*
* @since 2.2
*
* @var boolean
*/
public $sharpen_images;
/**
* Path to an image file to apply the transformations to.
*
* Supported file types are `BMP`, `GIF`, `JPEG`, `PNG` and `WEBP`.
*
* > `WEBP` support is available for PHP version `7.0.0+`.<br><br>
* Note that even though `WEBP` support was added to PHP in version `5.5.0`, it only started working with version
* `5.5.1`, while support for transparency was added with version `7.0.0`. As a result, I decided to make it
* available only if PHP version is at least `7.0.0`<br><br>
* Animated `WEBP` images are not currently supported by GD.
* See {@link https://github.com/libgd/libgd/issues/648 here} and {@link https://bugs.php.net/bug.php?id=79809&thanks=4 here}.
*
* > `BMP` support is available for PHP version `7.2.0+`
*
* @var string
*/
public $source_path;
/**
* Path (including file name) to where to save the transformed image.
*
* > Can be a different format than the file at {@link source_path}. The format of the transformed image will be
* determined by the file's extension. Supported file types are `BMP`, `GIF`, `JPEG`, `PNG` and `WEBP`.
*
* > `WEBP` support is available for PHP version `7.0.0+`.<br><br>
* Note that even though `WEBP` support was added to PHP in version `5.5.0`, it only started working with version
* `5.5.1`, while support for transparency was added with version `7.0.0`. As a result, I decided to make it
* available only if PHP version is at least `7.0.0`<br><br>
* Animated `WEBP` images are not currently supported by GD.
* See {@link https://github.com/libgd/libgd/issues/648 here} and {@link https://bugs.php.net/bug.php?id=79809&thanks=4 here}.
*
* > `BMP` support is available for PHP version `7.2.0+`
*
* @var string
*/
public $target_path;
/**
* Indicates the quality level of the output image.
*
* > Used only if PHP version is `7.0.0+` and the file at {@link target_path} is a `WEBP` image, or it will be
* ignored otherwise.
*
* Range is `0` - `100`
*
* Default is `80`
*
* @since 2.6.0
*
* @var integer
*/
public $webp_quality;
/**
* @var resource
*/
private $source_identifier;
/**
* @var mixed
*/
private $source_type;
/**
* @var int
*/
private $source_width;
/**
* @var int
*/
private $source_height;
/**
* @var array<int>
*/
private $source_transparent_color;
/**
* @var int
*/
private $source_transparent_color_index;
/**
* @var int
*/
private $source_time;
/**
* @var string
*/
private $target_type;
/**
* Constructor of the class.
*
* Initializes the class and the default properties.
*
* @return void
*/
public function __construct() {
// set default values for properties
$this->chmod_value = 0755;
$this->error = 0;
$this->jpeg_quality = 85;
$this->png_compression = 9;
$this->webp_quality = 80;
$this->preserve_aspect_ratio = $this->preserve_time = $this->enlarge_smaller_images = $this->bmp_compressed = true;
$this->sharpen_images = $this->auto_handle_exif_orientation = $this->jpeg_interlace = false;
$this->source_path = $this->target_path = '';
}
/**
* Applies one or more filters to the image given as {@link source_path} and outputs it as the file specified as
* {@link target_path}.
*
* > This method is available only if the {@link https://www.php.net/manual/en/function.imagefilter.php imagefilter}
* function is available (available from `PHP 5+`), and will leave images unaltered otherwise.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // apply the "grayscale" filter
* $img->apply_filter('grayscale');
*
* // apply the "contrast" filter
* $img->apply_filter('contrast', -20);
* </code>
*
* You can also apply multiple filters at once. In this case, the method requires a single argument, an array of
* arrays, containing the filters and associated arguments, where applicable:
*
* <code>
* // create a sepia effect
* // note how we're applying multiple filters at once
* // each filter is in its own array
* $img->apply_filter(array(
*
* // first we apply the "grayscale" filter
* array('grayscale'),
*
* // then we apply the "colorize" filter with 90, 60, 40 as
* // the values for red, green and blue
* array('colorize', 90, 60, 40),
*
* ));
* </code>
*
* @param mixed $filter The case-insensitive name of the filter to apply. Can be one of the following:
*
* - **brightness** - changes the brightness of the image; use `arg1` to set
* the level of brightness; the range of brightness is
* `-255` - `255`
* - **colorize** - adds specified RGB values to each pixel; use `arg1`,
* `arg2` and `arg3` in the form of `red`, `green` and
* `blue`, and `arg4` for the `alpha` channel; the range
* for each color is `-255` to `255` and `0` to `127` for
* the `alpha` where `0` indicates completely opaque
* while `127` indicates completely transparent; *alpha
* support is available for PHP 5.2.5+*
* - **contrast** - changes the contrast of the image; use `arg1` to set
* the level of contrast; the range of contrast is `-100`
* to `100`
* - **edgedetect** - uses edge detection to highlight the edges in the image
* - **emboss** - embosses the image
* - **gaussian_blur** - blurs the image using the Gaussian method
* - **grayscale** - converts the image into grayscale by changing the red,
* green and blue components to their weighted sum using
* the same coefficients as the REC.601 luma (Y') calculation;
* the alpha components are retained; for palette images
* the result may differ due to palette limitations
* - **mean_removal** - uses mean removal to achieve a *"sketchy"* effect
* - **negate** - reverses all the colors of the image
* - **pixelate** - applies pixelation effect to the image; use `arg1` to
* set the block size and `arg2` to set the pixelation
* effect mode; *this filter is available only for PHP
* 5.3.0+*
* - **selective_blur** - blurs the image
* - **scatter** - applies scatter effect to the image; use `arg1` and
* `arg2` to define the effect strength and additionally
* `arg3` to only apply the on select pixel colors
* - **smooth** - makes the image smoother; use `arg1` to set the level
* of smoothness; applies a 9-cell convolution matrix
* where center pixel has the weight of `arg1` and others
* weight of 1.0; the result is normalized by dividing
* the sum with `arg1` + 8.0 (sum of the matrix); any
* float is accepted, large value (in practice: 2048 or)
* more) = no change
*
* @param mixed $arg1 Used by the following filters:
* - **brightness** - sets the brightness level (`-255` to `255`)
* - **contrast** - sets the contrast level (`-100` to `100`)
* - **colorize** - sets the value of the red component (`-255` to `255`)
* - **pixelate** - sets the block size, in pixels
* - **scatter** - effect subtraction level; this must not be higher or
* equal to the addition level set with `arg3`
* - **smooth** - sets the smoothness level
*
* @param mixed $arg2 Used by the following filters:
* - **colorize** - sets the value of the green component (`-255` to `255`)
* - **pixelate** - whether to use advanced pixelation effect or not (defaults to `false`)
* - **scatter** - effect addition level
*
* @param mixed $arg3 Used by the following filters:
* - **colorize** - sets the value of the blue component (`-255` to `255`)
* - **scatter** - optional array indexed color values to apply effect at
*
* @param mixed $arg4 Used by the following filters:
* - **colorize** - alpha channel; a value between `0` and `127`. `0` indicates
* completely opaque while `127` indicates completely
* transparent
*
* @since 2.2.2
*
* @return boolean Returns `true` on success or false on error.
*
* If {@link https://www.php.net/manual/en/function.imagefilter.php imagefilter} is not
* available, the method will return `false` without setting an {@link error} code.
*
* If the requested filter doesn't exist, or invalid arguments are passed, the method
* will trigger a warning.
*
* If `false` is returned and you are sure that
* {@link https://www.php.net/manual/en/function.imagefilter.php imagefilter} exists and that
* the requested filter is valid, check the {@link error} property to see the error code.
*/
public function apply_filter($filter, $arg1 = '', $arg2 = '', $arg3 = '', $arg4 = '') {
// if "imagefilter" function exists and the requested filter exists
if (function_exists('imagefilter')) {
// if image resource was successfully created
if ($this->_create_from_source()) {
// prepare the target image
$target_identifier = $this->_prepare_image($this->source_width, $this->source_height, -1);
// copy the original image
imagecopyresampled(
$target_identifier,
$this->source_identifier,
0,
0,
0,
0,
$this->source_width,
$this->source_height,
$this->source_width,
$this->source_height
);
// if multiple filters are to be applied at once
if (is_array($filter)) {
// iterate through the filters
foreach ($filter as $arguments) {
// if filter exists
if (defined('IMG_FILTER_' . strtoupper($arguments[0]))) {
// try to apply the filter and trigger an error if the filter could not be applied
if (!@call_user_func_array('imagefilter', array_merge(array($target_identifier, constant('IMG_FILTER_' . strtoupper($arguments[0]))), array_slice($arguments, 1)))) {
trigger_error('Invalid arguments used for "' . strtoupper($arguments[0]) . '" filter', E_USER_WARNING);
}
// if filter doesn't exists, trigger an error
} else {
trigger_error('Filter "' . strtoupper($arguments[0]) . '" is not available', E_USER_WARNING);
}
}
// if a single filter is to be applied and it is available
} elseif (defined('IMG_FILTER_' . strtoupper($filter))) {
// get all the arguments passed to the method
$arguments = func_get_args();
// try to apply the filter and trigger an error if the filter could not be applied
if (!@call_user_func_array('imagefilter', array_merge(array($target_identifier, constant('IMG_FILTER_' . strtoupper($filter))), array_slice($arguments, 1)))) {
trigger_error('Invalid arguments used for "' . strtoupper($arguments[0]) . '" filter', E_USER_WARNING);
}
// if filter doesn't exists, trigger an error
} else {
trigger_error('Filter "' . strtoupper($filter) . '" is not available', E_USER_WARNING);
}
// write image
return $this->_write_image($target_identifier);
}
}
// if script gets this far, return false
// note that we do not set the error level as it has been already set
// by the _create_from_source() method earlier, if the case
return false;
}
/**
* Crops a portion of the image given as {@link source_path} and outputs it as the file specified as {@link target_path}.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // crop a rectangle of 100x100 pixels, starting from the top-left corner
* $img->crop(0, 0, 100, 100);
* </code>
*
* @param integer $start_x x coordinate to start cropping from
*
* @param integer $start_y y coordinate to start cropping from
*
* @param integer $end_x x coordinate where to end the cropping
*
* @param integer $end_y y coordinate where to end the cropping
*
* @param mixed $background_color (Optional) A hexadecimal color value (like `#FFFFFF` or `#FFF`) used when
* the cropping coordinates are off-scale (negative values and/or values
* greater than the image's size) to fill the remaining space.
*
* When set to `-1` the script will preserve transparency for transparent `GIF`
* and `PNG` images. For non-transparent images the background will be black
* (`#000000`) in this case.
*
* Default is `-1`
*
* @since 1.0.4
*
* @return boolean Returns `true` on success or `false` on error.
*
* If `false` is returned, check the {@link error} property to see the error code.
*/
public function crop($start_x, $start_y, $end_x, $end_y, $background_color = -1) {
// this method might be also called internally
// in this case, there's a sixth argument that points to an already existing image identifier
$args = func_get_args();
// if a sixth argument exists
// for PHP 8.0.0+ GD functions return and accept \GdImage objects instead of resources (https://php.watch/versions/8.0/gdimage)
if (isset($args[5]) && (is_resource($args[5]) || (version_compare(PHP_VERSION, '8.0.0', '>=') && $args[5] instanceof \GdImage))) {
// that it is the image identifier that we'll be using further on
$this->source_identifier = $args[5];
// set this to true so that the script will continue to execute at the next IF
$result = true;
// we need to make sure these are integers or PHP 8.1+ will show a warning
// https://php.watch/versions/8.1/deprecate-implicit-conversion-incompatible-float-string
$start_x = (int)$start_x;
$start_y = (int)$start_y;
$end_x = (int)$end_x;
$end_y = (int)$end_y;
// if method is called as usually
// try to create an image resource from source path
} else {
$result = $this->_create_from_source();
}
// if image resource was successfully created
if ($result !== false) {
// compute width and height
$width = $end_x - $start_x;
$height = $end_y - $start_y;
// prepare the target image
$target_identifier = $this->_prepare_image($width, $height, $background_color);
$dest_x = 0;
$dest_y = 0;
// if starting x is negative
if ($start_x < 0) {
// we are adjusting the position where we place the cropped image on the target image
$dest_x = abs($start_x);
// and crop starting from 0
$start_x = 0;
}
// if ending x is larger than the image's width, adjust the width we're showing
if ($end_x > ($image_width = imagesx($this->source_identifier))) {
$width = $image_width - $start_x;
}
// if starting y is negative
if ($start_y < 0) {
// we are adjusting the position where we place the cropped image on the target image
$dest_y = abs($start_y);
// and crop starting from 0
$start_y = 0;
}
// if ending y is larger than the image's height, adjust the height we're showing
if ($end_y > ($image_height = imagesy($this->source_identifier))) {
$height = $image_height - $start_y;
}
// crop the image
imagecopyresampled(
$target_identifier,
$this->source_identifier,
$dest_x,
$dest_y,
$start_x,
$start_y,
$width,
$height,
$width,
$height
);
// write image
return $this->_write_image($target_identifier);
}
// if script gets this far, return false
// note that we do not set the error level as it has been already set
// by the _create_from_source() method earlier
return false;
}
/**
* Flips both horizontally and vertically the image given as {@link source_path} and outputs the resulted image as
* {@link target_path}.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // flip the image both horizontally and vertically
* $img->flip_both();
* </code>
*
* @since 2.1
*
* @return boolean Returns `true` on success or `false` on error.
*
* If `false` is returned, check the {@link error} property to see the error code.
*/
public function flip_both() {
return $this->_flip('both');
}
/**
* Flips horizontally the image given as {@link source_path} and outputs the resulted image as {@link target_path}.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // flip the image horizontally
* $img->flip_horizontal();
* </code>
*
* @return boolean Returns `true` on success or `false` on error.
*
* If `false` is returned, check the {@link error} property to see the error code.
*/
public function flip_horizontal() {
return $this->_flip('horizontal');
}
/**
* Flips vertically the image given as {@link source_path} and outputs the resulted image as {@link target_path}.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // flip the image vertically
* $img->flip_vertical();
* </code>
*
* @return boolean Returns `true` on success or `false` on error.
*
* If `false` is returned, check the {@link error} property to see the error code.
*/
public function flip_vertical() {
return $this->_flip('vertical');
}
/**
* Resizes the image given as {@link source_path} and outputs the resulted image as {@link target_path}.
*
* <code>
* // include the Zebra_Image library
* // (you don't need this if you installed using composer)
* require 'path/to/Zebra_Image.php';
*
* // instantiate the class
* $img = new Zebra_Image();
*
* // a source image
* // (where "ext" is one of the supported file types extension)
* $img->source_path = 'path/to/source.ext';
*
* // path to where should the resulting image be saved
* // note that by simply setting a different extension to the file will
* // instruct the script to create an image of that particular type
* $img->target_path = 'path/to/target.ext';
*
* // apply a "sharpen" filter to the resulting images
* $img->sharpen_images = true;
*
* // resize the image to exactly 150x150 pixels, without altering
* // aspect ratio, by using the CROP_CENTER method
* $img->resize(150, 150, ZEBRA_IMAGE_CROP_CENTER);
* </code>
*
* @param integer $width The width to resize the image to.
*
* If set to `0`, the width will be automatically adjusted, depending on the
* value of the `height` argument so that the image preserves its aspect ratio.
*
* If {@link preserve_aspect_ratio} is set to `true` and both this and the
* `height` arguments are values greater than `0`, the image will be resized
* to the exact required width and height and the aspect ratio will be
* preserved (see also the description for the `method` argument below on
* how can this be done).
*
* If {@link preserve_aspect_ratio} is set to `false`, the image will be
* resized to the required width and the aspect ratio will be ignored.
*
* If both `width` and `height` are set to `0`, a copy of the source image
* will be created. {@link jpeg_quality} and {@link png_compression} will
* still apply!
*
* If either `width` or `height` are set to `0`, the script will consider
* the value of {@link preserve_aspect_ratio} to bet set to `true` regardless
* of its actual value!
*
* @param integer $height The height to resize the image to.
*
* If set to `0`, the height will be automatically adjusted, depending on
* the value of the `width` argument so that the image preserves its aspect
* ratio.
*
* If {@link preserve_aspect_ratio} is set to `true` and both this and the
* `width` arguments are values greater than `0`, the image will be resized
* to the exact required width and height and the aspect ratio will be
* preserved (see also the description for the `method` argument below on
* how can this be done).
*
* If {@link preserve_aspect_ratio} is set to `false`, the image will be
* resized to the required height and the aspect ratio will be ignored.
*
* If both `width` and `height` are set to `0`, a copy of the source image
* will be created. {@link jpeg_quality} and {@link png_compression} will
*
* If either `width` or `height` are set to `0`, the script will consider
* the value of {@link preserve_aspect_ratio} to bet set to `true` regardless
* of its actual value!
*
* @param int $method (Optional) Method to use when resizing images to exact width and height
* while preserving aspect ratio.
*
* If the {@link preserve_aspect_ratio} property is set to `true` and both
* the `width` and `height` arguments are values greater than `0`, the image
* will be resized to the exact given width and height and the aspect ratio
* will be preserved by using on of the following methods:
*
* - **ZEBRA_IMAGE_BOXED** - the image will be scaled so that it will fit
* in a box with the given width and height (both width/height will be
* smaller or equal to the required width/height) and then it will
* be centered both horizontally and vertically; the blank area will be
* filled with the color specified by the `bgcolor` argument. (the blank
* area will be filled only if the image is not transparent!)
*
* - **ZEBRA_IMAGE_NOT_BOXED** - the image will be scaled so that it
* *could* fit in a box with the given width and height but will not be
* enclosed in a box with given width and height. The new width/height
* will be both smaller or equal to the required width/height.
*
* - **ZEBRA_IMAGE_CROP_TOPLEFT**
* - **ZEBRA_IMAGE_CROP_TOPCENTER**
* - **ZEBRA_IMAGE_CROP_TOPRIGHT**
* - **ZEBRA_IMAGE_CROP_MIDDLELEFT**
* - **ZEBRA_IMAGE_CROP_CENTER**
* - **ZEBRA_IMAGE_CROP_MIDDLERIGHT**
* - **ZEBRA_IMAGE_CROP_BOTTOMLEFT**
* - **ZEBRA_IMAGE_CROP_BOTTOMCENTER**
* - **ZEBRA_IMAGE_CROP_BOTTOMRIGHT**
*
* For the methods involving crop, first the image is scaled so that both
* its sides are equal or greater than the respective sizes of the bounding
* box; next, a region of required width and height will be cropped from
* indicated region of the resulted image.
*
* Default is `ZEBRA_IMAGE_CROP_CENTER`
*
* @param mixed $background_color (Optional) The hexadecimal color (like `#FFFFFF` or `#FFF`) of the blank
* area. See the `method` argument.
*
* When set to `-1` the script will preserve transparency for transparent `GIF`
* and `PNG` images. For non-transparent images the background will be white
* (`#FFFFFF`) in this case.
*
* Default is `-1`
*
* @return boolean Returns `true` on success or `false` on error.
*
* If `false` is returned, check the {@link error} property to see what went
* wrong.
*/
public function resize($width = 0, $height = 0, $method = ZEBRA_IMAGE_CROP_CENTER, $background_color = -1) {
// we need to make sure these are integers or PHP 8.1+ will show a warning
// https://php.watch/versions/8.1/deprecate-implicit-conversion-incompatible-float-string
$width = (int)$width;
$height = (int)$height;
// if image resource was successfully created
if ($this->_create_from_source()) {
// if either width or height is to be adjusted automatically
// set a flag telling the script that, even if $preserve_aspect_ratio is set to false
// treat everything as if it was set to true
if ($width == 0 || $height == 0) {
$auto_preserve_aspect_ratio = true;
}
// if aspect ratio needs to be preserved
if ($this->preserve_aspect_ratio || isset($auto_preserve_aspect_ratio)) {
// if height is given and width is to be computed accordingly
if ($width == 0 && $height > 0) {
// get the original image's aspect ratio
$aspect_ratio = $this->source_width / $this->source_height;
// the target image's height is as given as argument to the method
$target_height = $height;
// compute the target image's width, preserving the aspect ratio
$target_width = round($height * $aspect_ratio);
// if width is given and height is to be computed accordingly
} elseif ($width > 0 && $height == 0) {
// get the original image's aspect ratio
$aspect_ratio = $this->source_height / $this->source_width;
// the target image's width is as given as argument to the method
$target_width = $width;
// compute the target image's height, preserving the aspect ratio
$target_height = round($width * $aspect_ratio);
// if both width and height are given and ZEBRA_IMAGE_BOXED or ZEBRA_IMAGE_NOT_BOXED methods are to be used
} elseif ($width > 0 && $height > 0 && ($method == 0 || $method == 1)) {
// compute the horizontal and vertical aspect ratios
$vertical_aspect_ratio = $height / $this->source_height;
$horizontal_aspect_ratio = $width / $this->source_width;
// if the image's newly computed height would be inside the bounding box
if (round($horizontal_aspect_ratio * $this->source_height) < $height) {
// the target image's width is as given as argument to the method
$target_width = $width;
// compute the target image's height so that the image will stay inside the bounding box
$target_height = round($horizontal_aspect_ratio * $this->source_height);
// otherwise
} else {
// the target image's height is as given as argument to the method
$target_height = $height;
// compute the target image's width so that the image will stay inside the bounding box
$target_width = round($vertical_aspect_ratio * $this->source_width);
}
// if both width and height are given and image is to be cropped in order to get to the required size
} elseif ($width > 0 && $height > 0 && $method > 1 && $method < 11) {
// compute the horizontal and vertical aspect ratios
$vertical_aspect_ratio = $this->source_height / $height;
$horizontal_aspect_ratio = $this->source_width / $width;
// we'll use one of the two
$aspect_ratio =
$vertical_aspect_ratio < $horizontal_aspect_ratio ?
$vertical_aspect_ratio :
$horizontal_aspect_ratio;
// compute the target image's width, preserving the aspect ratio
$target_width = round($this->source_width / $aspect_ratio);
// compute the target image's height, preserving the aspect ratio
$target_height = round($this->source_height / $aspect_ratio);
// for any other case
} else {
// we will create a copy of the source image