-
Notifications
You must be signed in to change notification settings - Fork 97
/
phpthumb.filters.php
1550 lines (1312 loc) · 67.9 KB
/
phpthumb.filters.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
//////////////////////////////////////////////////////////////
// phpThumb() by James Heinrich <info@silisoftware.com> //
// available at http://phpthumb.sourceforge.net //
// and/or https://github.com/JamesHeinrich/phpThumb //
//////////////////////////////////////////////////////////////
/// //
// phpthumb.filters.php - image processing filter functions //
// ///
//////////////////////////////////////////////////////////////
class phpthumb_filters {
/**
* @var phpthumb
*/
public $phpThumbObject = null;
public function DebugMessage($message, $file='', $line='') {
if (is_object($this->phpThumbObject)) {
return $this->phpThumbObject->DebugMessage($message, $file, $line);
}
return false;
}
public function ApplyMask(&$gdimg_mask, &$gdimg_image) {
if (phpthumb_functions::gd_version() < 2) {
$this->DebugMessage('Skipping ApplyMask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
return false;
}
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.2', '>=')) {
$this->DebugMessage('Using alpha ApplyMask() technique', __FILE__, __LINE__);
if ($gdimg_mask_resized = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_image), imagesy($gdimg_image))) {
imagecopyresampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, imagesx($gdimg_image), imagesy($gdimg_image), imagesx($gdimg_mask), imagesy($gdimg_mask));
if ($gdimg_mask_blendtemp = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_image), imagesy($gdimg_image))) {
$color_background = imagecolorallocate($gdimg_mask_blendtemp, 0, 0, 0);
imagefilledrectangle($gdimg_mask_blendtemp, 0, 0, imagesx($gdimg_mask_blendtemp), imagesy($gdimg_mask_blendtemp), $color_background);
imagealphablending($gdimg_mask_blendtemp, false);
imagesavealpha($gdimg_mask_blendtemp, true);
for ($x = 0, $xMax = imagesx($gdimg_image); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg_image); $y < $yMax; $y++) {
//$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y);
$RealPixel = phpthumb_functions::GetPixelColor($gdimg_image, $x, $y);
$MaskPixel = phpthumb_functions::GrayscalePixel(phpthumb_functions::GetPixelColor($gdimg_mask_resized, $x, $y));
$MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127)));
$newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha);
imagesetpixel($gdimg_mask_blendtemp, $x, $y, $newcolor);
}
}
imagealphablending($gdimg_image, false);
imagesavealpha($gdimg_image, true);
imagecopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, imagesx($gdimg_mask_blendtemp), imagesy($gdimg_mask_blendtemp));
imagedestroy($gdimg_mask_blendtemp);
} else {
$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
}
imagedestroy($gdimg_mask_resized);
} else {
$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
}
} else {
// alpha merging requires PHP v4.3.2+
$this->DebugMessage('Skipping ApplyMask() technique because PHP is v"'. PHP_VERSION .'"', __FILE__, __LINE__);
}
return true;
}
public function Bevel(&$gdimg, $width, $hexcolor1, $hexcolor2) {
$width = ($width ? $width : 5);
$hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
$hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
imagealphablending($gdimg, true);
for ($i = 0; $i < $width; $i++) {
$alpha = round(($i / $width) * 127);
$color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1, false, $alpha);
$color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2, false, $alpha);
imageline($gdimg, $i, $i + 1, $i, imagesy($gdimg) - $i - 1, $color1); // left
imageline($gdimg, $i, $i , imagesx($gdimg) - $i, $i , $color1); // top
imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i - 1, imagesx($gdimg) - $i, $i + 1, $color2); // right
imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i , $i, imagesy($gdimg) - $i , $color2); // bottom
}
return true;
}
public function Blur(&$gdimg, $radius=0.5) {
// Taken from Torstein Hønsi's phpUnsharpMask (see phpthumb.unsharp.php)
$radius = round(max(0, min($radius, 50)) * 2);
if (!$radius) {
return false;
}
$w = imagesx($gdimg);
$h = imagesy($gdimg);
if ($imgBlur = imagecreatetruecolor($w, $h)) {
// Gaussian blur matrix:
// 1 2 1
// 2 4 2
// 1 2 1
// 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 < $radius; $i++) {
imagecopy ($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1); // up left
imagecopymerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50); // down right
imagecopymerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33); // down left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25); // up right
imagecopymerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33); // left
imagecopymerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25); // right
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20); // up
imagecopymerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 17); // down
imagecopymerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50); // center
imagecopy ($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
}
return false;
}
public function BlurGaussian(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GAUSSIAN_BLUR)', __FILE__, __LINE__);
// fall through and try it the hard way
}
$this->DebugMessage('FAILED: phpthumb_filters::BlurGaussian($gdimg) [using phpthumb_filters::Blur() instead]', __FILE__, __LINE__);
return $this->Blur($gdimg, 0.5);
}
public function BlurSelective(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_SELECTIVE_BLUR)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::BlurSelective($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
public function Brightness(&$gdimg, $amount=0) {
if ($amount == 0) {
return true;
}
$amount = max(-255, min(255, $amount));
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_BRIGHTNESS, $amount)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_BRIGHTNESS, '.$amount.')', __FILE__, __LINE__);
// fall through and try it the hard way
}
$scaling = (255 - abs($amount)) / 255;
$baseamount = (($amount > 0) ? $amount : 0);
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
$NewPixel = array();
foreach ($OriginalPixel as $key => $value) {
$NewPixel[$key] = round($baseamount + ($OriginalPixel[$key] * $scaling));
}
$newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function Contrast(&$gdimg, $amount=0) {
if ($amount == 0) {
return true;
}
$amount = max(-255, min(255, $amount));
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
// imagefilter(IMG_FILTER_CONTRAST) has range +100 to -100 (positive numbers make it darker!)
$amount = ($amount / 255) * -100;
if (imagefilter($gdimg, IMG_FILTER_CONTRAST, $amount)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_CONTRAST, '.$amount.')', __FILE__, __LINE__);
// fall through and try it the hard way
}
if ($amount > 0) {
$scaling = 1 + ($amount / 255);
} else {
$scaling = (255 - abs($amount)) / 255;
}
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
$NewPixel = array();
foreach ($OriginalPixel as $key => $value) {
$NewPixel[$key] = min(255, max(0, round($OriginalPixel[$key] * $scaling)));
}
$newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function Colorize(&$gdimg, $amount, $targetColor) {
$amount = (is_numeric($amount) ? $amount : 25);
$amountPct = $amount / 100;
$targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'gray');
if ($amount == 0) {
return true;
}
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if ($targetColor == 'gray') {
$targetColor = '808080';
}
$r = round($amountPct * hexdec(substr($targetColor, 0, 2)));
$g = round($amountPct * hexdec(substr($targetColor, 2, 2)));
$b = round($amountPct * hexdec(substr($targetColor, 4, 2)));
if (imagefilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// overridden below for grayscale
$TargetPixel = array();
if ($targetColor != 'gray') {
$TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
$TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
$TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
}
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
if ($targetColor == 'gray') {
$TargetPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
}
$NewPixel = array();
foreach ($TargetPixel as $key => $value) {
$NewPixel[$key] = round(max(0, min(255, ($OriginalPixel[$key] * ((100 - $amount) / 100)) + ($TargetPixel[$key] * $amountPct))));
}
//$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
$newColor = imagecolorallocate($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function Crop(&$gdimg, $left=0, $right=0, $top=0, $bottom=0) {
if (!$left && !$right && !$top && !$bottom) {
return true;
}
$oldW = imagesx($gdimg);
$oldH = imagesy($gdimg);
if (($left > 0) && ($left < 1)) { $left = round($left * $oldW); }
if (($right > 0) && ($right < 1)) { $right = round($right * $oldW); }
if (($top > 0) && ($top < 1)) { $top = round($top * $oldH); }
if (($bottom > 0) && ($bottom < 1)) { $bottom = round($bottom * $oldH); }
$right = min($oldW - $left - 1, $right);
$bottom = min($oldH - $top - 1, $bottom);
$newW = (int)($oldW - $left - $right);
$newH = (int)($oldH - $top - $bottom);
$left = (int)$left;
$top = (int)$top;
if ($imgCropped = imagecreatetruecolor($newW, $newH)) {
imagecopy($imgCropped, $gdimg, 0, 0, $left, $top, $newW, $newH);
if ($gdimg = imagecreatetruecolor($newW, $newH)) {
imagecopy($gdimg, $imgCropped, 0, 0, 0, 0, $newW, $newH);
imagedestroy($imgCropped);
return true;
}
imagedestroy($imgCropped);
}
return false;
}
public function Desaturate(&$gdimg, $amount, $color='') {
if ($amount == 0) {
return true;
}
return $this->Colorize($gdimg, $amount, (phpthumb_functions::IsHexColor($color) ? $color : 'gray'));
}
public function DropShadow(&$gdimg, $distance, $width, $hexcolor, $angle, $alpha) {
if (phpthumb_functions::gd_version() < 2) {
return false;
}
$distance = ($distance ? $distance : 10);
$width = ($width ? $width : 10);
$hexcolor = ($hexcolor ? $hexcolor : '000000');
$angle = ($angle ? $angle : 225) % 360;
$alpha = max(0, min(100, ($alpha ? $alpha : 100)));
if ($alpha <= 0) {
// invisible shadow, nothing to do
return true;
}
if ($distance <= 0) {
// shadow completely obscured by source image, nothing to do
return true;
}
//$width_shadow = cos(deg2rad($angle)) * ($distance + $width);
//$height_shadow = sin(deg2rad($angle)) * ($distance + $width);
//$scaling = min(imagesx($gdimg) / (imagesx($gdimg) + abs($width_shadow)), imagesy($gdimg) / (imagesy($gdimg) + abs($height_shadow)));
$Offset = array();
for ($i = 0; $i < $width; $i++) {
$WidthAlpha[$i] = (abs(($width / 2) - $i) / $width);
$Offset['x'] = cos(deg2rad($angle)) * ($distance + $i);
$Offset['y'] = sin(deg2rad($angle)) * ($distance + $i);
}
$tempImageWidth = imagesx($gdimg) + abs($Offset['x']);
$tempImageHeight = imagesy($gdimg) + abs($Offset['y']);
if ($gdimg_dropshadow_temp = phpthumb_functions::ImageCreateFunction($tempImageWidth, $tempImageHeight)) {
imagealphablending($gdimg_dropshadow_temp, false);
imagesavealpha($gdimg_dropshadow_temp, true);
$transparent1 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, 0, 0, 0, 127);
imagefill($gdimg_dropshadow_temp, 0, 0, $transparent1);
$PixelMap = array();
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
$PixelMap[$x][$y] = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
}
}
for ($x = 0; $x < $tempImageWidth; $x++) {
for ($y = 0; $y < $tempImageHeight; $y++) {
//for ($i = 0; $i < $width; $i++) {
for ($i = 0; $i < 1; $i++) {
if (!isset($PixelMap[$x][$y]['alpha']) || ($PixelMap[$x][$y]['alpha'] > 0)) {
if (isset($PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha']) && ($PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha'] < 127)) {
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor, false, $PixelMap[$x + (int)$Offset['x']][$y + (int)$Offset['y']]['alpha']);
imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
}
}
}
}
}
imagealphablending($gdimg_dropshadow_temp, true);
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
if ($PixelMap[$x][$y]['alpha'] < 127) {
$thisColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
}
}
}
imagesavealpha($gdimg, true);
imagealphablending($gdimg, false);
//$this->is_alpha = true;
$transparent2 = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 0, 0, 0, 127);
imagefilledrectangle($gdimg, 0, 0, imagesx($gdimg), imagesy($gdimg), $transparent2);
imagecopyresampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg), imagesx($gdimg_dropshadow_temp), imagesy($gdimg_dropshadow_temp));
imagedestroy($gdimg_dropshadow_temp);
}
return true;
}
public function EdgeDetect(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_EDGEDETECT)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_EDGEDETECT)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::EdgeDetect($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
public function Ellipse($gdimg) {
if (phpthumb_functions::gd_version() < 2) {
return false;
}
// generate mask at twice desired resolution and downsample afterwards for easy antialiasing
if ($gdimg_ellipsemask_double = phpthumb_functions::ImageCreateFunction(imagesx($gdimg) * 2, imagesy($gdimg) * 2)) {
if ($gdimg_ellipsemask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) {
$color_transparent = imagecolorallocate($gdimg_ellipsemask_double, 255, 255, 255);
imagefilledellipse($gdimg_ellipsemask_double, imagesx($gdimg), imagesy($gdimg), (imagesx($gdimg) - 1) * 2, (imagesy($gdimg) - 1) * 2, $color_transparent);
imagecopyresampled($gdimg_ellipsemask, $gdimg_ellipsemask_double, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg), imagesx($gdimg) * 2, imagesy($gdimg) * 2);
$this->ApplyMask($gdimg_ellipsemask, $gdimg);
imagedestroy($gdimg_ellipsemask);
return true;
} else {
$this->DebugMessage('$gdimg_ellipsemask = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
}
imagedestroy($gdimg_ellipsemask_double);
} else {
$this->DebugMessage('$gdimg_ellipsemask_double = phpthumb_functions::ImageCreateFunction() failed', __FILE__, __LINE__);
}
return false;
}
public function Emboss(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_EMBOSS)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_EMBOSS)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::Emboss($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
public function Flip(&$gdimg, $x=false, $y=false) {
if (!$x && !$y) {
return false;
}
if ($tempImage = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) {
if ($x) {
imagecopy($tempImage, $gdimg, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg));
for ($x = 0, $xMax = imagesx($gdimg); $x < $xMax; $x++) {
imagecopy($gdimg, $tempImage, imagesx($gdimg) - 1 - $x, 0, $x, 0, 1, imagesy($gdimg));
}
}
if ($y) {
imagecopy($tempImage, $gdimg, 0, 0, 0, 0, imagesx($gdimg), imagesy($gdimg));
for ($y = 0, $yMax = imagesy($gdimg); $y < $yMax; $y++) {
imagecopy($gdimg, $tempImage, 0, imagesy($gdimg) - 1 - $y, 0, $y, imagesx($gdimg), 1);
}
}
imagedestroy($tempImage);
}
return true;
}
public function Frame(&$gdimg, $frame_width, $edge_width, $hexcolor_frame, $hexcolor1, $hexcolor2) {
$frame_width = ($frame_width ? $frame_width : 5);
$edge_width = ($edge_width ? $edge_width : 1);
$hexcolor_frame = ($hexcolor_frame ? $hexcolor_frame : 'CCCCCC');
$hexcolor1 = ($hexcolor1 ? $hexcolor1 : 'FFFFFF');
$hexcolor2 = ($hexcolor2 ? $hexcolor2 : '000000');
$color_frame = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor_frame);
$color1 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor1);
$color2 = phpthumb_functions::ImageHexColorAllocate($gdimg, $hexcolor2);
for ($i = 0; $i < $edge_width; $i++) {
// outer bevel
imageline($gdimg, $i, $i, $i, imagesy($gdimg) - $i, $color1); // left
imageline($gdimg, $i, $i, imagesx($gdimg) - $i, $i, $color1); // top
imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i, imagesx($gdimg) - $i, $i, $color2); // right
imageline($gdimg, imagesx($gdimg) - $i, imagesy($gdimg) - $i, $i, imagesy($gdimg) - $i, $color2); // bottom
}
for ($i = 0; $i < $frame_width; $i++) {
// actual frame
imagerectangle($gdimg, $edge_width + $i, $edge_width + $i, imagesx($gdimg) - $edge_width - $i, imagesy($gdimg) - $edge_width - $i, $color_frame);
}
for ($i = 0; $i < $edge_width; $i++) {
// inner bevel
imageline($gdimg, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, imagesy($gdimg) - $frame_width - $edge_width - $i, $color2); // left
imageline($gdimg, $frame_width + $edge_width + $i, $frame_width + $edge_width + $i, imagesx($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, $color2); // top
imageline($gdimg, imagesx($gdimg) - $frame_width - $edge_width - $i, imagesy($gdimg) - $frame_width - $edge_width - $i, imagesx($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, $color1); // right
imageline($gdimg, imagesx($gdimg) - $frame_width - $edge_width - $i, imagesy($gdimg) - $frame_width - $edge_width - $i, $frame_width + $edge_width + $i, imagesy($gdimg) - $frame_width - $edge_width - $i, $color1); // bottom
}
return true;
}
public function Gamma(&$gdimg, $amount) {
if (number_format($amount, 4) == '1.0000') {
return true;
}
return imagegammacorrect($gdimg, 1.0, $amount);
}
public function Grayscale(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_GRAYSCALE)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
return $this->Colorize($gdimg, 100, 'gray');
}
public function HistogramAnalysis(&$gdimg, $calculateGray=false) {
$ImageSX = imagesx($gdimg);
$ImageSY = imagesy($gdimg);
$Analysis = array();
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
@$Analysis['red'][$OriginalPixel['red']]++;
@$Analysis['green'][$OriginalPixel['green']]++;
@$Analysis['blue'][$OriginalPixel['blue']]++;
@$Analysis['alpha'][$OriginalPixel['alpha']]++;
if ($calculateGray) {
$GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
@$Analysis['gray'][$GrayPixel['red']]++;
}
}
}
$keys = array('red', 'green', 'blue', 'alpha');
if ($calculateGray) {
$keys[] = 'gray';
}
foreach ($keys as $dummy => $key) {
ksort($Analysis[$key]);
}
return $Analysis;
}
public function HistogramStretch(&$gdimg, $band='*', $method=0, $threshold=0.1) {
// equivalent of "Auto Contrast" in Adobe Photoshop
// method 0 stretches according to RGB colors. Gives a more conservative stretch.
// method 1 band stretches according to grayscale which is color-biased (59% green, 30% red, 11% blue). May give a punchier / more aggressive stretch, possibly appearing over-saturated
$Analysis = $this->HistogramAnalysis($gdimg, true);
$keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=> ($method == 0) ? 'all' : 'gray' );
$band = $band[ 0 ];
if (!isset($keys[$band])) {
return false;
}
$key = $keys[$band];
// If the absolute brightest and darkest pixels are used then one random
// pixel in the image could throw off the whole system. Instead, count up/down
// from the limit and allow <threshold> (default = 0.1%) of brightest/darkest
// pixels to be clipped to min/max
$threshold = (float) $threshold / 100;
$clip_threshold = imagesx($gdimg) * imagesx($gdimg) * $threshold;
$countsum = 0;
$range_min = 0;
for ($i = 0; $i <= 255; $i++) {
if ($method == 0) {
$countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
} else {
$countsum += @$Analysis[$key][$i];
}
if ($countsum >= $clip_threshold) {
$range_min = $i - 1;
break;
}
}
$range_min = max($range_min, 0);
$countsum = 0;
$range_max = 255;
for ($i = 255; $i >= 0; $i--) {
if ($method == 0) {
$countsum = max(@$Analysis['red'][$i], @$Analysis['green'][$i], @$Analysis['blue'][$i]);
} else {
$countsum += @$Analysis[$key][$i];
}
if ($countsum >= $clip_threshold) {
$range_max = $i + 1;
break;
}
}
$range_max = min($range_max, 255);
$range_scale = (($range_max == $range_min) ? 1 : (255 / ($range_max - $range_min)));
if (($range_min == 0) && ($range_max == 255)) {
// no adjustment necessary - don't waste CPU time!
return true;
}
$ImageSX = imagesx($gdimg);
$ImageSY = imagesy($gdimg);
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
if ($band == '*') {
$new['red'] = min(255, max(0, ($OriginalPixel['red'] - $range_min) * $range_scale));
$new['green'] = min(255, max(0, ($OriginalPixel['green'] - $range_min) * $range_scale));
$new['blue'] = min(255, max(0, ($OriginalPixel['blue'] - $range_min) * $range_scale));
$new['alpha'] = min(255, max(0, ($OriginalPixel['alpha'] - $range_min) * $range_scale));
} else {
$new = $OriginalPixel;
$new[$key] = min(255, max(0, ($OriginalPixel[$key] - $range_min) * $range_scale));
}
$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $new['red'], $new['green'], $new['blue'], $new['alpha']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function HistogramOverlay(&$gdimg, $bands='*', $colors='', $width=0.25, $height=0.25, $alignment='BR', $opacity=50, $margin_x=5, $margin_y=null) {
$margin_y = (null === $margin_y ? $margin_x : $margin_y);
$Analysis = $this->HistogramAnalysis($gdimg, true);
$histW = round(($width > 1) ? min($width, imagesx($gdimg)) : imagesx($gdimg) * $width);
$histH = round(($width > 1) ? min($width, imagesx($gdimg)) : imagesx($gdimg) * $width);
if ($gdHist = imagecreatetruecolor($histW, $histH)) {
$color_back = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHist, 0, 0, 0, 127);
imagefilledrectangle($gdHist, 0, 0, $histW, $histH, $color_back);
imagealphablending($gdHist, false);
imagesavealpha($gdHist, true);
$HistogramTempWidth = 256;
$HistogramTempHeight = 100;
if ($gdHistTemp = imagecreatetruecolor($HistogramTempWidth, $HistogramTempHeight)) {
$color_back_temp = phpthumb_functions::ImageColorAllocateAlphaSafe($gdHistTemp, 255, 0, 255, 127);
imagealphablending($gdHistTemp, false);
imagesavealpha($gdHistTemp, true);
imagefilledrectangle($gdHistTemp, 0, 0, imagesx($gdHistTemp), imagesy($gdHistTemp), $color_back_temp);
$DefaultColors = array('r'=>'FF0000', 'g'=>'00FF00', 'b'=>'0000FF', 'a'=>'999999', '*'=>'FFFFFF');
$Colors = explode(';', $colors);
$BandsToGraph = array_unique(preg_split('##', $bands));
$keys = array('r'=>'red', 'g'=>'green', 'b'=>'blue', 'a'=>'alpha', '*'=>'gray');
foreach ($BandsToGraph as $key => $band) {
if (!isset($keys[$band])) {
continue;
}
$PeakValue = max($Analysis[$keys[$band]]);
$thisColor = phpthumb_functions::ImageHexColorAllocate($gdHistTemp, phpthumb_functions::IsHexColor(@$Colors[$key]) ? $Colors[$key] : $DefaultColors[$band]);
for ($x = 0; $x < $HistogramTempWidth; $x++) {
imageline($gdHistTemp, $x, $HistogramTempHeight - 1, $x, $HistogramTempHeight - 1 - round(@$Analysis[$keys[$band]][$x] / $PeakValue * $HistogramTempHeight), $thisColor);
}
imageline($gdHistTemp, 0, $HistogramTempHeight - 1, $HistogramTempWidth - 1, $HistogramTempHeight - 1, $thisColor);
imageline($gdHistTemp, 0, $HistogramTempHeight - 2, $HistogramTempWidth - 1, $HistogramTempHeight - 2, $thisColor);
}
imagecopyresampled($gdHist, $gdHistTemp, 0, 0, 0, 0, imagesx($gdHist), imagesy($gdHist), imagesx($gdHistTemp), imagesy($gdHistTemp));
imagedestroy($gdHistTemp);
} else {
return false;
}
$this->WatermarkOverlay($gdimg, $gdHist, $alignment, $opacity, $margin_x, $margin_y);
imagedestroy($gdHist);
return true;
}
return false;
}
public function ImageBorder(&$gdimg, $border_width, $radius_x, $radius_y, $hexcolor_border) {
$border_width = ($border_width ? $border_width : 1);
$radius_x = ($radius_x ? $radius_x : 0);
$radius_y = ($radius_y ? $radius_y : 0);
$output_width = imagesx($gdimg);
$output_height = imagesy($gdimg);
list($new_width, $new_height) = phpthumb_functions::ProportionalResize($output_width, $output_height, $output_width - max($border_width * 2, $radius_x), $output_height - max($border_width * 2, $radius_y));
$offset_x = ($radius_x ? $output_width - $new_width - $radius_x : 0);
if ($gd_border_canvas = phpthumb_functions::ImageCreateFunction($output_width, $output_height)) {
imagesavealpha($gd_border_canvas, true);
imagealphablending($gd_border_canvas, false);
$color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gd_border_canvas, 255, 255, 255, 127);
imagefilledrectangle($gd_border_canvas, 0, 0, $output_width, $output_height, $color_background);
$color_border = phpthumb_functions::ImageHexColorAllocate($gd_border_canvas, (phpthumb_functions::IsHexColor($hexcolor_border) ? $hexcolor_border : '000000'));
for ($i = 0; $i < $border_width; $i++) {
imageline($gd_border_canvas, floor($offset_x / 2) + $radius_x, $i, $output_width - $radius_x - ceil($offset_x / 2), $i, $color_border); // top
imageline($gd_border_canvas, floor($offset_x / 2) + $radius_x, $output_height - 1 - $i, $output_width - $radius_x - ceil($offset_x / 2), $output_height - 1 - $i, $color_border); // bottom
imageline($gd_border_canvas, floor($offset_x / 2) + $i, $radius_y, floor($offset_x / 2) + $i, $output_height - $radius_y, $color_border); // left
imageline($gd_border_canvas, $output_width - 1 - $i - ceil($offset_x / 2), $radius_y, $output_width - 1 - $i - ceil($offset_x / 2), $output_height - $radius_y, $color_border); // right
}
if ($radius_x && $radius_y) {
// PHP bug: imagearc() with thicknesses > 1 give bad/undesirable/unpredicatable results
// Solution: Draw multiple 1px arcs side-by-side.
// Problem: parallel arcs give strange/ugly antialiasing problems
// Solution: draw non-parallel arcs, from one side of the line thickness at the start angle
// to the opposite edge of the line thickness at the terminating angle
for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
imagearc($gd_border_canvas, floor($offset_x / 2) + 1 + $radius_x, $thickness_offset - 1 + $radius_y, $radius_x * 2, $radius_y * 2, 180, 270, $color_border); // top-left
imagearc($gd_border_canvas, $output_width - $radius_x - 1 - ceil($offset_x / 2), $thickness_offset - 1 + $radius_y, $radius_x * 2, $radius_y * 2, 270, 360, $color_border); // top-right
imagearc($gd_border_canvas, $output_width - $radius_x - 1 - ceil($offset_x / 2), $output_height - $thickness_offset - $radius_y, $radius_x * 2, $radius_y * 2, 0, 90, $color_border); // bottom-right
imagearc($gd_border_canvas, floor($offset_x / 2) + 1 + $radius_x, $output_height - $thickness_offset - $radius_y, $radius_x * 2, $radius_y * 2, 90, 180, $color_border); // bottom-left
}
if ($border_width > 1) {
for ($thickness_offset = 0; $thickness_offset < $border_width; $thickness_offset++) {
imagearc($gd_border_canvas, floor($offset_x / 2) + $thickness_offset + $radius_x, $radius_y, $radius_x * 2, $radius_y * 2, 180, 270, $color_border); // top-left
imagearc($gd_border_canvas, $output_width - $thickness_offset - $radius_x - 1 - ceil($offset_x / 2), $radius_y, $radius_x * 2, $radius_y * 2, 270, 360, $color_border); // top-right
imagearc($gd_border_canvas, $output_width - $thickness_offset - $radius_x - 1 - ceil($offset_x / 2), $output_height - $radius_y, $radius_x * 2, $radius_y * 2, 0, 90, $color_border); // bottom-right
imagearc($gd_border_canvas, floor($offset_x / 2) + $thickness_offset + $radius_x, $output_height - $radius_y, $radius_x * 2, $radius_y * 2, 90, 180, $color_border); // bottom-left
}
}
}
$this->phpThumbObject->ImageResizeFunction($gd_border_canvas, $gdimg, floor(($output_width - $new_width) / 2), round(($output_height - $new_height) / 2), 0, 0, $new_width, $new_height, $output_width, $output_height);
imagedestroy($gdimg);
$gdimg = phpthumb_functions::ImageCreateFunction($output_width, $output_height);
imagesavealpha($gdimg, true);
imagealphablending($gdimg, false);
$gdimg_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, 255, 255, 255, 127);
imagefilledrectangle($gdimg, 0, 0, $output_width, $output_height, $gdimg_color_background);
imagecopy($gdimg, $gd_border_canvas, 0, 0, 0, 0, $output_width, $output_height);
imagedestroy($gd_border_canvas);
return true;
} else {
$this->DebugMessage('FAILED: $gd_border_canvas = phpthumb_functions::ImageCreateFunction('.$output_width.', '.$output_height.')', __FILE__, __LINE__);
}
return false;
}
public static function ImprovedImageRotate(&$gdimg_source, $rotate_angle, $config_background_hexcolor, $bg, &$phpThumbObject) {
while ($rotate_angle < 0) {
$rotate_angle += 360;
}
$rotate_angle %= 360;
if ($rotate_angle != 0) {
$background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_source, $config_background_hexcolor);
if ((phpthumb_functions::gd_version() >= 2) && !$bg && ($rotate_angle % 90)) {
//$this->DebugMessage('Using alpha rotate', __FILE__, __LINE__);
if ($gdimg_rotate_mask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_source), imagesy($gdimg_source))) {
$color_mask = array();
for ($i = 0; $i <= 255; $i++) {
$color_mask[$i] = imagecolorallocate($gdimg_rotate_mask, $i, $i, $i);
}
imagefilledrectangle($gdimg_rotate_mask, 0, 0, imagesx($gdimg_rotate_mask), imagesy($gdimg_rotate_mask), $color_mask[255]);
$imageX = imagesx($gdimg_source);
$imageY = imagesy($gdimg_source);
for ($x = 0; $x < $imageX; $x++) {
for ($y = 0; $y < $imageY; $y++) {
$pixelcolor = phpthumb_functions::GetPixelColor($gdimg_source, $x, $y);
imagesetpixel($gdimg_rotate_mask, $x, $y, $color_mask[255 - round($pixelcolor['alpha'] * 255 / 127)]);
}
}
$gdimg_rotate_mask = imagerotate($gdimg_rotate_mask, $rotate_angle, $color_mask[0]);
$gdimg_source = imagerotate($gdimg_source, $rotate_angle, $background_color);
imagealphablending($gdimg_source, false);
imagesavealpha($gdimg_source, true);
//$this->is_alpha = true;
$phpThumbFilters = new self();
//$phpThumbFilters->phpThumbObject = $this;
$phpThumbFilters->phpThumbObject = $phpThumbObject;
$phpThumbFilters->ApplyMask($gdimg_rotate_mask, $gdimg_source);
imagedestroy($gdimg_rotate_mask);
} else {
//$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
}
} else {
if (phpthumb_functions::gd_version() < 2) {
//$this->DebugMessage('Using non-alpha rotate because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
} elseif ($bg) {
//$this->DebugMessage('Using non-alpha rotate because $this->bg is "'.$bg.'"', __FILE__, __LINE__);
} elseif ($rotate_angle % 90) {
//$this->DebugMessage('Using non-alpha rotate because ($rotate_angle % 90) = "'.($rotate_angle % 90).'"', __FILE__, __LINE__);
} else {
//$this->DebugMessage('Using non-alpha rotate because $this->thumbnailFormat is "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
}
if (imagecolortransparent($gdimg_source) >= 0) {
// imagerotate() forgets all about an image's transparency and sets the transparent color to black
// To compensate, flood-fill the transparent color of the source image with the specified background color first
// then rotate and the colors should match
if (!function_exists('imageistruecolor') || !imageistruecolor($gdimg_source)) {
// convert paletted image to true-color before rotating to prevent nasty aliasing artifacts
//$this->source_width = imagesx($gdimg_source);
//$this->source_height = imagesy($gdimg_source);
$gdimg_newsrc = phpthumb_functions::ImageCreateFunction(imagesx($gdimg_source), imagesy($gdimg_source));
$background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor);
imagefilledrectangle($gdimg_newsrc, 0, 0, imagesx($gdimg_source), imagesy($gdimg_source), phpthumb_functions::ImageHexColorAllocate($gdimg_newsrc, $config_background_hexcolor));
imagecopy($gdimg_newsrc, $gdimg_source, 0, 0, 0, 0, imagesx($gdimg_source), imagesy($gdimg_source));
imagedestroy($gdimg_source);
unset($gdimg_source);
$gdimg_source = $gdimg_newsrc;
unset($gdimg_newsrc);
} else {
imagecolorset(
$gdimg_source,
imagecolortransparent($gdimg_source),
hexdec(substr($config_background_hexcolor, 0, 2)),
hexdec(substr($config_background_hexcolor, 2, 2)),
hexdec(substr($config_background_hexcolor, 4, 2)));
imagecolortransparent($gdimg_source, -1);
}
}
$gdimg_source = imagerotate($gdimg_source, $rotate_angle, $background_color);
}
}
return true;
}
public function MeanRemoval(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_MEAN_REMOVAL)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_MEAN_REMOVAL)', __FILE__, __LINE__);
// fall through and try it the hard way
}
// currently not implemented "the hard way"
$this->DebugMessage('FAILED: phpthumb_filters::MeanRemoval($gdimg) [function not implemented]', __FILE__, __LINE__);
return false;
}
public function Negative(&$gdimg) {
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_NEGATE)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_NEGATE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
$ImageSX = imagesx($gdimg);
$ImageSY = imagesy($gdimg);
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$currentPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, ~$currentPixel[ 'red'] & 0xFF, ~$currentPixel[ 'green'] & 0xFF, ~$currentPixel[ 'blue'] & 0xFF, $currentPixel[ 'alpha']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function RoundedImageCorners(&$gdimg, $radius_x, $radius_y) {
// generate mask at twice desired resolution and downsample afterwards for easy antialiasing
// mask is generated as a white double-size ellipse on a triple-size black background and copy-paste-resampled
// onto a correct-size mask image as 4 corners due to errors when the entire mask is resampled at once (gray edges)
if ($gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction($radius_x * 6, $radius_y * 6)) {
if ($gdimg_cornermask = phpthumb_functions::ImageCreateFunction(imagesx($gdimg), imagesy($gdimg))) {
$color_transparent = imagecolorallocate($gdimg_cornermask_triple, 255, 255, 255);
imagefilledellipse($gdimg_cornermask_triple, $radius_x * 3, $radius_y * 3, $radius_x * 4, $radius_y * 4, $color_transparent);
imagefilledrectangle($gdimg_cornermask, 0, 0, imagesx($gdimg), imagesy($gdimg), $color_transparent);
imagecopyresampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, 0, $radius_x, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
imagecopyresampled($gdimg_cornermask, $gdimg_cornermask_triple, 0, imagesy($gdimg) - $radius_y, $radius_x, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
imagecopyresampled($gdimg_cornermask, $gdimg_cornermask_triple, imagesx($gdimg) - $radius_x, imagesy($gdimg) - $radius_y, $radius_x * 3, $radius_y * 3, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
imagecopyresampled($gdimg_cornermask, $gdimg_cornermask_triple, imagesx($gdimg) - $radius_x, 0, $radius_x * 3, $radius_y, $radius_x, $radius_y, $radius_x * 2, $radius_y * 2);
$this->ApplyMask($gdimg_cornermask, $gdimg);
imagedestroy($gdimg_cornermask);
$this->DebugMessage('RoundedImageCorners('.$radius_x.', '.$radius_y.') succeeded', __FILE__, __LINE__);
return true;
} else {
$this->DebugMessage('FAILED: $gdimg_cornermask = phpthumb_functions::ImageCreateFunction('.imagesx($gdimg).', '.imagesy($gdimg).')', __FILE__, __LINE__);
}
imagedestroy($gdimg_cornermask_triple);
} else {
$this->DebugMessage('FAILED: $gdimg_cornermask_triple = phpthumb_functions::ImageCreateFunction('.($radius_x * 6).', '.($radius_y * 6).')', __FILE__, __LINE__);
}
return false;
}
public function Saturation(&$gdimg, $amount, $color='') {
if ($amount == 0) {
return true;
} elseif ($amount > 0) {
$amount = 0 - $amount;
} else {
$amount = abs($amount);
}
return $this->Desaturate($gdimg, $amount, $color);
}
public function Sepia(&$gdimg, $amount, $targetColor) {
$amount = (is_numeric($amount) ? max(0, min(100, $amount)) : 50);
$amountPct = $amount / 100;
$targetColor = (phpthumb_functions::IsHexColor($targetColor) ? $targetColor : 'A28065');
if ($amount == 0) {
return true;
}
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_GRAYSCALE)) {
$r = round($amountPct * hexdec(substr($targetColor, 0, 2)));
$g = round($amountPct * hexdec(substr($targetColor, 2, 2)));
$b = round($amountPct * hexdec(substr($targetColor, 4, 2)));
if (imagefilter($gdimg, IMG_FILTER_COLORIZE, $r, $g, $b)) {
return true;
}
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_COLORIZE)', __FILE__, __LINE__);
// fall through and try it the hard way
} else {
$this->DebugMessage('FAILED: imagefilter($gdimg, IMG_FILTER_GRAYSCALE)', __FILE__, __LINE__);
// fall through and try it the hard way
}
}
$TargetPixel['red'] = hexdec(substr($targetColor, 0, 2));
$TargetPixel['green'] = hexdec(substr($targetColor, 2, 2));
$TargetPixel['blue'] = hexdec(substr($targetColor, 4, 2));
$ImageSX = imagesx($gdimg);
$ImageSY = imagesy($gdimg);
for ($x = 0; $x < $ImageSX; $x++) {
for ($y = 0; $y < $ImageSY; $y++) {
$OriginalPixel = phpthumb_functions::GetPixelColor($gdimg, $x, $y);
$GrayPixel = phpthumb_functions::GrayscalePixel($OriginalPixel);
// http://www.gimpguru.org/Tutorials/SepiaToning/
// "In the traditional sepia toning process, the tinting occurs most in
// the mid-tones: the lighter and darker areas appear to be closer to B&W."
$SepiaAmount = ((128 - abs($GrayPixel['red'] - 128)) / 128) * $amountPct;
$NewPixel = array();
foreach ($TargetPixel as $key => $value) {
$NewPixel[$key] = round(max(0, min(255, $GrayPixel[$key] * (1 - $SepiaAmount) + ($TargetPixel[$key] * $SepiaAmount))));
}
$newColor = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg, $NewPixel['red'], $NewPixel['green'], $NewPixel['blue'], $OriginalPixel['alpha']);
imagesetpixel($gdimg, $x, $y, $newColor);
}
}
return true;
}
public function Smooth(&$gdimg, $amount=6) {
$amount = min(25, max(0, $amount));
if ($amount == 0) {
return true;
}
if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
if (imagefilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
return true;
}