-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEasyImage.php
More file actions
4027 lines (3586 loc) · 122 KB
/
EasyImage.php
File metadata and controls
4027 lines (3586 loc) · 122 KB
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
/**
*
* EasyImage: An easy-to use image manipulation class.
*
* @package EasyImage
* @author Robert Parham
* @license wtfpl.net WTFPL
* @version 2.8
*/
set_time_limit(0);
class EasyImage{
/***************************************************************************
******************************* CONSTANTS *********************************
**************************************************************************/
const PDF = "application/pdf"; // File Format Constant
const PNG = "image/png"; // File Format Constant
const GIF = "image/gif"; // File Format Constant
const JPG = "image/jpg"; // File Format Constant
const VERT = "V"; // Flip type constant
const HORIZ = "H"; // Flip type constant
const BOTH = "B"; // Flip type constant
const LEFT = "left"; // Position constant
const RIGHT = "right"; // Position constant
const TOP = "top"; // Position constant
const BOTTOM = "bottom"; // Position constant
const CENTER = "center"; // Position constant
const FILL = "fill"; // Fill/cover constant
const COVER = "cover"; // Fill/cover constant
const TEXT_ALIGN_LEFT = 0; // Left align text
const TEXT_ALIGN_RIGHT = 1; // Right align text
const TEXT_ALIGN_CENTER = 2; // Center align text
const TEXT_ALIGN_JUSTIFY = 3; // Justify text
/***************************************************************************
**************************** IMAGE PROPERTIES *****************************
**************************************************************************/
private static $DPI = 72; // Dots per inch
private $filepath; // Path to the image
private $width; // Image width
private $height; // Image height
private $mime; // The mimetype of the image, ie self::PNG
private $landscape; // Is it landscape (vs. portrait)
private $imageFunct; // The naem of the function needed to render the image
private $compression; // The compression/quality parameter used to create the image
private $image; // Image resource identifier
private $isTemp; // Is it a temporary image
private $cropOffsets; // Array of offsets for an image cropped by autocrop funtion
private $outputAs; // Format to output the file as
private $fileExt; // File Extention
/***************************************************************************
***************************** PDF PROPERTIES ******************************
**************************************************************************/
private $pdf_page = 0; // current page number
private $pdf_n = 2; // current object number
private $pdf_buffer = ''; // buffer holding in-memory PDF
private $pdf_state = 0; // current document state
private $pdf_DrawColor = '0 G'; // commands for drawing color
private $pdf_FillColor = '0 g'; // commands for filling color
private $pdf_ColorFlag = false; // indicates whether fill and text colors are different
private $pdf_images = array(); // array of used images
private $pdf_WithAlpha = false; // has an alpha channel
private $pdf_InFooter = false; // in footer
/***************************************************************************
************************** ANIMATED GIF PROPERTIES ************************
**************************************************************************/
private $gif_sources; // Array of EasyImage instances for each frame
private $gif_delayTimes; // Array of delay times for each frame
private $gif_loops = 0; // How many times to loop animation
private $gif_disposal = 2; // Gif disposal
private $gif_transRed = 0; // Transparent red
private $gif_transGreen = 0; // Transparent green
private $gif_transBlue = 0; // Transparent blue
/***************************************************************************
****************************** PSD PROPERTIES *****************************
**************************************************************************/
private static $psd_infoArray; // PSD file data
private static $psd_fp; // PSD File pointer
private static $psd_fn; // PSD filename
private static $psd_tempname; // PSD temp filename
private static $psd_cbLength; // PSD color bytes length
/***************************************************************************
*************************** PUBLIC CONSTRUCTOR ****************************
**************************************************************************/
/**
* Convenience constructor - Determines the correct constructor based on arguments passed
* @return \EasyImage
*/
public static function Create(){
$args = func_get_args();
$num = func_num_args();
$return = null;
switch($num){
case(1):
if(is_array($args[0]))
$return = self::createAnimatedGIF($args[0]);
else if(filter_var($args[0], FILTER_VALIDATE_URL))
$return = self::createFromURL($args[0]);
else if(file_exists($args[0])){
if(strpos(strtoupper(mime_content_type($args[0])), "PHOTOSHOP") !== false){
$return = self::createFromPSD($args[0]);
}else if(self::isAnimatedGif($args[0])){
$return = self::createFromAnimatedGif($args[0]);
}else{
$return = self::createFromFile($args[0]);
}
}else $return = self::createFromBase64($args[0]);
break;
case(2):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1]);
else if(file_exists($args[0])) $return = createFromFile($args[0], $args[1]);
else if(is_numeric($args[0]) && is_numeric($args[1])) $return = self::createBlank($args[0], $args[1]);
else $return = self::createHTMLImage($args[0], $args[1]);
break;
case(3):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1], $args[2]);
else $return = self::createBlank($args[0], $args[1], $args[2]);
break;
case(4):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1], $args[2], $args[3]);
else $return = self::createTextImage($args[0], $args[1], $args[2], $args[3]);
break;
case(5):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1], $args[2], $args[3], $args[4]);
else $return = self::createTextImage($args[0], $args[1], $args[2], $args[3], $args[4]);
break;
case(6):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
else $return = self::createTextImage($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
break;
case(7):
if(is_array($args[0])) $return = self::createAnimatedGIF($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
else $return = self::createTextImage($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
break;
case(8):
$return = self::createTextImage($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7]);
break;
case(9):
$return = self::createTextImage($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6], $args[7], $args[8]);
break;
default:
self::Error("Incorrect parameter number for EasyImage::Create.");
}
return $return;
}
/***************************************************************************
***************************** EDITING METHODS *****************************
**************************************************************************/
/**
* Add perspective to an image
* @param float $gradient - gradient of perspective
* @param type $rightdown - angle
* @return \EasyImage
*/
public function perspective($gradient = 0.85, $rightdown="top"){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->perspective($gradient, $rightdown);
return $this;
}
switch($rightdown){
case "top": $rightdown = 0; break;
case "bottom": $rightdown = 1; break;
case "left": $rightdown = 2; break;
case "right": $rightdown = 3; break;
default: $rightdown = 0; break;
}
$w = imagesx($this->image);
$h = imagesy($this->image);
$col = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
$mult = 5;
$li = imagecreatetruecolor($w * $mult, $h * $mult);
imagealphablending($li, false);
imagefilledrectangle($li, 0, 0, $w * $mult, $h * $mult, $col);
imagesavealpha($li, true);
imagecopyresized($li, $this->image, 0, 0, 0, 0, $w * $mult, $h * $mult, $w, $h);
imagedestroy($this->image);
$w*=$mult;
$h*=$mult;
$image = imagecreatetruecolor($w, $h);
imagealphablending($image, false);
imagefilledrectangle($image, 0, 0, $w, $h, $col);
imagealphablending($image, true);
$test = $h * $gradient;
$naty = 0;
$natx = 0;
$rdmod = $rightdown % 2;
$min = 1;
if ($rightdown < 2){
for ($y = 0; $y < $h; $y++){
$ny = $rdmod ? $y : $h - $y;
$off = round((1 - $gradient) * $w * ($ny / $h));
$t = ((1 - pow(1 - pow(($ny / $h), 2), 0.5)) * (1 - $gradient) + ($ny / $h) * $gradient);
$nt = $rdmod ? $t : 1 - $t;
if (abs(0.5 - $nt) < $min){
$min = abs(0.5 - $nt);
$naty = $off;
}
imagecopyresampled($image, $li, round($off / 2), $y, 0, abs($nt * $h), $w - $off, 1, $w, 1);
}
}else{
for($x = 0; $x < $w; $x++){
$nx = $rdmod ? $x : $w - $x;
$off = round((1 - $gradient) * $h * ($nx / $w));
$t = ((1 - pow(1 - pow(($nx / $w), 2), 0.5)) * (1 - $gradient) + ($nx / $w) * $gradient);
$nt = $rdmod ? $t : 1 - $t;
if (abs(0.5 - $nt) < $min){
$min = abs(0.5 - $nt);
$natx = $off;
}
imagecopyresampled($image, $li, $x, round($off / 2), abs($nt * $w), 0, 1, $h - $off, 1, $h);
}
}
imagedestroy($li);
imagealphablending($image, false);
imagesavealpha($image, true);
$this->image = imagecreatetruecolor(($w + $naty) / $mult, ($h + $natx) / $mult);
imagealphablending($this->image, false);
imagefilledrectangle($this->image, 0, 0, ($w + $naty) / $mult, ($h + $natx) / $mult, $col);
imagealphablending($this->image, true);
imagecopyresampled($this->image, $image, 0, 0, 0, 0, ($w + $naty) / $mult, ($h + $natx) / $mult, $w, $h);
imagedestroy($image);
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
return $this;
}
/**
* Add a border radius
* @param int $radius - radius
* @param string $colour - hex color
* @return type
*/
public function borderRadius($radius=10, $colour="#FFFFFF"){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->borderRadius($radius=10, $colour);
return $this;
}
$source_width = $this->width;
$source_height = $this->height;
$corner_image = imagecreatetruecolor($radius, $radius);
$clear_colour = imagecolorallocatealpha($corner_image, 255, 255, 255, 127);
$solid_colour = self::getColorFromHex($corner_image, $colour);
imagealphablending($this->image, false);
imagealphablending($corner_image, false);
imagecolortransparent($corner_image, $clear_colour);
imagefill($corner_image, 0, 0, $solid_colour);
imagefilledellipse($corner_image, $radius, $radius, $radius * 2, $radius * 2, $clear_colour);
imagecopymerge($this->image, $corner_image, 0, 0, 0, 0, $radius, $radius, 100);
$corner_image = imagerotate($corner_image, 90, 0);
imagecopymerge($this->image, $corner_image, 0, $source_height - $radius, 0, 0, $radius, $radius, 100);
$corner_image = imagerotate($corner_image, 90, 0);
imagecopymerge($this->image, $corner_image, $source_width - $radius, $source_height - $radius, 0, 0, $radius, $radius, 100);
$corner_image = imagerotate($corner_image, 90, 0);
imagecopymerge($this->image, $corner_image, $source_width - $radius, 0, 0, 0, $radius, $radius, 100);
return $this;
}
/**
* Merge two images
* @param type $src
* @param type $dst_x
* @param type $dst_y
* @param type $src_x
* @param type $src_y
* @param type $src_w
* @param type $src_h
* @param type $pct
* @return \EasyImage
*/
public function mergeImages($src, $dst_x=0, $dst_y=0, $src_x=0, $src_y=0, $src_w=null, $src_h=null, $pct=50){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->mergeImages($src, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct);
return $this;
}
if(!is_object($src)) $src = EasyImage::Create($src);
if(empty($src_w)) $src_w = $src->getWidth();
if(empty($src_h)) $src_h = $src->getWidth();
imagecopymerge($this->image, $src->getImageResource(), $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct);
return $this;
}
/**
* Add a watermark to the image
* @param string $text - Text for the watermark
* @param int $font_size - Font size
* @param string $color - hex color
* @param string $font_file - path to .ttf font file
* @param string $horizontal_pos - "left", "right" or "center"
* @param string $vertical_pos - "top", "bottom" or "center"
* @param int $opacity - opacity level, 0-127
* @param int $padding - pixels to pad
* @return \EasyImage
*/
public function addWatermark($text, $font_size, $color, $font_file, $horizontal_pos="right", $vertical_pos="bottom", $opacity=65, $padding=3){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->addWatermark($text, $font_size, $color, $font_file, $horizontal_pos, $vertical_pos, $opacity, $padding);
return $this;
}
$watermark = self::createTextImage($text, $font_size, $color, $font_file, null, false, $opacity);
switch($horizontal_pos){
case(self::RIGHT): $x = $this->width - $padding - $watermark->getWidth(); break;
case(self::CENTER): $x = ($this->width / 2) - ($watermark->getWidth() / 2); break;
case(self::LEFT): default: $x = $padding;
}
switch($vertical_pos){
case(self::BOTTOM): $y = $this->height - $padding - $watermark->getHeight(); break;
case(self::CENTER): $y = ($this->height / 2) - ($watermark->getHeight() / 2); break;
case(self::TOP): default: $y = $padding;
}
$this->addOverlay($watermark, $x, $y, $watermark->getWidth(), $watermark->getHeight());
return $this;
}
/**
* Adjust transparencey of entire image
* @param int $alpha - a number 0 - 127
* @return \EasyImage
*/
public function makeOpaque($alpha){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->makeOpaque($alpha);
return $this;
}
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
for($x = $image_width; $x--;){
for($y = $image_height; $y--;){
$color = self::getPixelRGBA($x, $y);
if($color['alpha'] < $alpha){
$newColor = imagecolorallocatealpha($this->image, $color['red'], $color['green'], $color['blue'], $alpha);
imagesetpixel($this->image, $x, $y, $newColor);
}
}
}
return $this;
}
/**
* Add an Overlay
* @param mixed $EasyImage - the image; a URL, Filepath or EasyImage object
* @param int $dst_x - the destination x point
* @param int $dst_y - the destination y point
* @param int $src_w - source width
* @param int $src_h - source height
* @return \EasyImage
*/
public function addOverlay($EasyImage, $dst_x, $dst_y, $src_w=null, $src_h=null){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->addOverlay($EasyImage, $dst_x, $dst_y, $src_w=null, $src_h=null);
return $this;
}
if(!is_object($EasyImage)) $EasyImage = self::Create($EasyImage);
if(empty($src_w)) $src_w = $EasyImage->getWidth();
if(empty($src_h)) $src_h = $EasyImage->getHeight();
imagealphablending( $this->image, true );
imagesavealpha( $this->image, true );
$src = $EasyImage->getImageResource();
imagealphablending($src, true );
imagesavealpha($src, true );
imagecopy($this->image, $src, $dst_x, $dst_y, 0, 0, $src_w, $src_h);
return $this;
}
/**
* Convert image to black and white
* @return \EasyImage
*/
public function blackAndWhite(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->blackAndWhite();
return $this;
}
$black = self::getColorFromHex($this->image, "#000000");
$white = self::getColorFromHex($this->image, "#FFFFFF");
for ($x = $this->width; $x--;) {
for ($y = $this->height; $y--;) {
$a = $this->getPixelRGBA($x, $y);
//$a = self::hexToRGB("#888888");
$luma = (0.2126 * $a['red'] + 0.7152 * $a['green'] + 0.0722 * $a['blue']) / 255;
$newColor = $luma > 0.53333333333333 ? $white : $black;
imagesetpixel($this->image, $x, $y, $newColor);
}
}
return $this;
}
/**
* Crops the image to the given dimensions, at the given starting point
* Defaults to top left
* @param int $new_width - new width of image
* @param int $new_height - new height of the image
* @param int $x - x position to start cropping from
* @param int $y - y position to start cropping from
* @return \EasyImage
*/
public function crop($new_width, $new_height, $x = 0, $y = 0){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->crop($new_width, $new_height, $x, $y);
return $this;
}
// If the cropped area is larger than the original image
if($this->height < $new_height+$y || $this->width < $new_width+$x){
$img = EasyImage::Create($new_width+$x, $new_height+$y)->addOverlay($this, 0, 0);
$this->image = $img->getImageResource();
$this->width = $img->getWidth();
$this->height = $img->getHeight();
unset($img);
}
$clr = $this->getUniqueColor();
$img = imagecreatetruecolor($new_width, $new_height);
$color = imagecolorallocate($img, $clr['red'], $clr['green'], $clr['blue']);
imagecolortransparent($img, $color);
imagefilledrectangle($img, 0, 0, $new_width, $new_height, $color);
imagealphablending($img, false);
imagesavealpha($img, true);
imagecopy($img, $this->image, 0, 0, $x, $y, $new_width, $new_height);
$this->height = $new_height;
$this->width = $new_width;
if(false !== $img) $this->image = $img;
else self::Error("Could not crop image.");
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
return $this;
}
/**
* Automatically crops an image based on the surrounding color
* @param int $threshold - Sensitivity level
* @param int $padding - how many pixels of surrounding background to keep
* @return \EasyImage
*/
public function autoCrop($threshold = 50, $padding = 3) {
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->autoCrop($threshold, $padding);
return $this;
}
$bgColor = false;
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
$leftPixel = $image_width;
$topPixel = 0;
$bottomPixel = $image_height;
$rightPixel = 0;
for ($x = $image_width; $x--;) {
for ($y = $image_height; $y--;) {
$currentPixelColor = $this->getPixelRGBA($x, $y);
if(false === $bgColor) $bgColor = $currentPixelColor;
$dist = self::getColorDistance($currentPixelColor, $bgColor);
if($threshold < $dist){
if($y > $topPixel) $topPixel = $y;
if($y < $bottomPixel) $bottomPixel = $y;
if($x > $rightPixel) $rightPixel = $x;
if($x < $leftPixel) $leftPixel = $x;
}
}
}
// handle padding
if($rightPixel+$padding < $image_width) $rightPixel += $padding; else $rightPixel=$image_width;
if($topPixel+$padding < $image_height) $topPixel += $padding; else $topPixel = $image_height;
if($leftPixel-$padding > 0) $leftPixel -= $padding; else $leftPixel = 0;
if($bottomPixel-$padding > 0) $bottomPixel -= $padding; else $bottomPixel = 0;
$this->crop($rightPixel-$leftPixel, $topPixel-$bottomPixel, $leftPixel, $bottomPixel);
$this->cropOffsets = array("top"=>$topPixel, "bottom"=>$bottomPixel, "left"=>$leftPixel, "right"=>$rightPixel);
return $this;
}
/**
* Convert tranparency to a solid color
* @param string $hexColor - color in HEX format
* @return \EasyImage
*/
public function transparentToColor($hexColor){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->transparentToColor($hexColor);
return $this;
}
$newColor = self::getColorFromHex($this->image, $hexColor);
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
for($x = $image_width; $x--;){
for($y = $image_height; $y--;){
$color = self::getPixelRGBA($x, $y);
if($color['alpha'] == 127)
imagesetpixel($this->image, $x, $y, $newColor);
}
}
return $this;
}
/**
* Converts all pixels of a certain color to transparent
* @param string $hexColor - color in HEX format
* @return \EasyImage
*/
public function removeColor($hexColor){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->removeColor($hexColor);
return $this;
}
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
for($x = $image_width; $x--;){
for($y = $image_height; $y--;){
$color = self::getPixelRGBA($x, $y);
$color = self::RGBToHex($color);
if($color === $hexColor){
$newColor = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
imagesetpixel($this->image, $x, $y, $newColor);
}
}
}
return $this;
}
/**
* Remove all colors EXCEPT the provided one
* @param string $MaskHexColor - color in HEX format
* @return \EasyImage
*/
public function colorMask($MaskHexColor){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->colorMask($MaskHexColor);
return $this;
}
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
for($x = $image_width; $x--;){
for($y = $image_height; $y--;){
$color = self::getPixelRGBA($x, $y);
$color = self::RGBToHex($color);
if($color !== $MaskHexColor){
$newColor = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
imagesetpixel($this->image, $x, $y, $newColor);
}
}
}
return $this;
}
/**
* Replaces colors in an image
* @param string $oldColor - color in HEX format
* @param string $newColor - color in HEX format
* @param int $threshold - sensitivity level
* @return \EasyImage
*/
public function replaceColor($oldColor, $newColor, $threshold=50){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->replaceColor($oldColor, $newColor, $threshold);
return $this;
}
$rgb1 = array();
list($rgb1['red'], $rgb1['green'], $rgb1['blue']) = self::hexToRGB($oldColor);
list($new['red'], $new['green'], $new['blue']) = self::hexToRGB($newColor);
$image_width = imagesx($this->image);
$image_height = imagesy($this->image);
for($x = $image_width; $x--;){
for($y = $image_height; $y--;){
$rgb2 = self::getPixelRGBA($x, $y);
$dist = self::getColorDistance($rgb1, $rgb2);
if($threshold > $dist){
$color = imagecolorallocatealpha($this->image, $new['red'], $new['green'], $new['blue'], $rgb2['alpha']);
imagesetpixel($this->image, $x, $y, $color);
}
}
}
return $this;
}
/**
* Scales the image to cover the dimensions provided
* @param int $width - width of canvas
* @param int $height - height of canvas
* @param string $cover - "fill" or "cove"
* @return \EasyImage
*/
public function scale($width, $height=null, $cover='fill'){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->scale($width, $height, $cover);
return $this;
}
if(empty($height)) $height = $width;
// Get new dimensions
$imgRatio = $this->height/$this->width;
$canvasRatio = $height/$width;
if(
($canvasRatio > $imgRatio && $cover==self::FILL) ||
($canvasRatio <= $imgRatio && $cover!=self::FILL)
){
$finalHeight = $height;
$scale = $finalHeight / $this->height;
$finalWidth = $this->width * $scale;
}else{
$finalWidth = $width;
$scale = $finalWidth / $this->width;
$finalHeight = $this->height * $scale;
}
// Resize the image
$thumb = imagecreatetruecolor($finalWidth, $finalHeight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $finalWidth, $finalHeight, $this->width, $this->height);
$this->resize($finalWidth, $finalHeight);
$this->width = $finalWidth;
$this->height = $finalHeight;
return $this;
}
/**
* Resizes the image to the dimensions provided
* @param int $width - width of canvas
* @param int $height - height of canvas
* @return \EasyImage
*/
public function resize($width, $height){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->resize($width, $height);
return $this;
}
//echo $this; exit;
// Resize the image
$thumb = imagecreatetruecolor($width, $height);
imagealphablending($thumb, true);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefill($thumb, 0, 0, $transparent);
imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
$this->image = $thumb;
$this->width = $width;
$this->height = $height;
return $this;
}
/**
* Rotate the image
* @param int $degrees - degrees to rotate
* @return \EasyImage
*/
public function rotate($degrees){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->rotate($degrees);
return $this;
}
$degrees += 360;
$pngTransparency = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
$this->image = imagerotate($this->image, $degrees, $pngTransparency);
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
return $this;
}
/**
* Tile the image to the provided dimensions
* @param int $width - width of canvas
* @param int $height - height of canvas
* @return \EasyImage
*/
public function tile($width, $height){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->tile($width, $height);
return $this;
}
// Our output image to be created
$out = imagecreatetruecolor($width, $this->height);
imagealphablending($out, false);
imagesavealpha($out, true);
// Tile that shit horiz
$curr_x = 0;
while($curr_x < $width){
imagecopy($out, $this->image, $curr_x, 0, 0, 0, $this->width, $this->height);
$curr_x += $this->width;
}
// our final output image to be created
$thumb = imagecreatetruecolor($width, $height);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
// Tile that shit vert
$curr_y = 0;
while($curr_y < $height){
imagecopy($thumb, $out, 0, $curr_y, 0, 0, $width, $this->height);
$curr_y += $this->height;
}
imagedestroy($out);
$this->image = $thumb;
$this->width = $width;
$this->height = $height;
return $this;
}
/**
* Reverse all colors of the image
* @return \EasyImage
*/
public function reverseColors(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->reverseColors();
return $this;
}
imagefilter($this->image, IMG_FILTER_NEGATE);
return $this;
}
/**
* Convert image to greyscale
* @return \EasyImage
*/
public function greyScale(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->greyScale();
return $this;
}
imagefilter($this->image, IMG_FILTER_GRAYSCALE);
return $this;
}
/**
* Adjust brightness level.
* @param int $brightness - a number between 255 and -255
* @return \EasyImage
*/
public function adjustBrightness($brightness){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->adjustBrightness($brightness);
return $this;
}
if($brightness > 255) $brightness = 255;
if($brightness < -255) $brightness = -255;
imagefilter($this->image, IMG_FILTER_BRIGHTNESS, $brightness);
return $this;
}
/**
* Adjust the contrast level
* @param int $contrast
* @return \EasyImage
*/
public function adjustContrast($contrast){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->adjustContrast($contrast);
return $this;
}
imagefilter($this->image, IMG_FILTER_CONTRAST, $contrast);
return $this;
}
/**
* Turns on edgeDetect Filter
* @return \EasyImage
*/
public function edgeDetect(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->edgeDetect();
return $this;
}
imagefilter($this->image, IMG_FILTER_EDGEDETECT);
return $this;
}
/**
* Turns on emboss Filter
* @return \EasyImage
*/
public function emboss(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->emboss();
return $this;
}
imagefilter($this->image, IMG_FILTER_EMBOSS);
return $this;
}
/**
* Turns on gaussianBlur Filter
* @return \EasyImage
*/
public function gaussianBlur(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->gaussianBlur();
return $this;
}
imagefilter($this->image, IMG_FILTER_GAUSSIAN_BLUR);
return $this;
}
/**
* Turns on selectiveBlur Filter
* @return \EasyImage
*/
public function selectiveBlur(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->selectiveBlur();
return $this;
}
imagefilter($this->image, IMG_FILTER_SELECTIVE_BLUR);
return $this;
}
/**
* Turns on sketch Filter
* @return \EasyImage
*/
public function sketch(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->sketch();
return $this;
}
imagefilter($this->image, IMG_FILTER_MEAN_REMOVAL);
return $this;
}
/**
* Adds a vignette
* @return \EasyImage
*/
public function vignette(){
// If it's a gif, apply to each
if(is_array($this->gif_sources)){
foreach($this->gif_sources as $img)
$img->vignette();
return $this;
}
for($x = 0; $x < imagesx($this->image); ++$x){
for($y = 0; $y < imagesy($this->image); ++$y){
$rgb = $this->getPixelRGBA($x, $y);
$sharp = 0.4; // 0 - 10 small is sharpnes,
$level = 0.7; // 0 - 1 small is brighter
$l = sin(M_PI / $this->width * $x) * sin(M_PI / $this->height * $y);
$l = pow($l, $sharp);
$l = 1 - $level * (1 - $l);
$rgb['red'] *= $l;