-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgd2imaging.php
executable file
·2761 lines (2463 loc) · 74.9 KB
/
gd2imaging.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
namespace Tigra;
/**
* Tigra Image Library
*
* GD2 Imaging (part of Lotos Framework)
*
* Copyright (c) 2005-2011 Artur Graniszewski ([email protected])
* All rights reserved.
*
* @category Library
* @package Lotos
* @subpackage Imaging
* @copyright Copyright (c) 2005-2011 Artur Graniszewski ([email protected])
* @license GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* @version 1.7.4
*/
use Exception;
/**
* Pixel shader class.
*/
class Shader
{
/**
* Shader drawing mode: use integer values of imagecolorat() only (fastest mode)
*/
const USE_INT = 1;
/**
* Shader drawing mode: calculate RGB values from integer returned by imagecolorat() (fast mode)
*/
const USE_RGB = 2;
/**
* Shader drawing mode: calculate HSV values from integer returned by imagecolorat() (slowest mode)
* This mode enables USE_RGB automatically.
*/
const USE_HSV = 4;
/**
* Shader drawing mode: Operate on alpha channel.
*/
const USE_ALPHA = 8;
/**
* Shader instructions (PHP code).
*
* @var string
*/
protected $shaderContent = '';
/**
* Shader drawing mode.
*
* @var int
*/
protected $shaderMode = self::USE_HSV;
/**
* Shaders' active area.
*
* @var Point[]
*/
protected $area = null;
/**
* Shader constructor
*
* @param mixed $shaderContent Shader instructions (PHP code).
* @return Shader
*/
public function __construct($shaderContent = '') {
$this->shaderContent = $shaderContent;
}
/**
* Returns the drawing mode (bitmask) of this shader.
*
* @param int $shaderMode Shader drawing mode: Shader::USE_INT, Shader::USE_RGB, Shader::USE_HSV (default), etc.
* @return Shader
*/
public function setMode($shaderMode) {
$this->shaderMode = $shaderMode;
return $this;
}
/**
* Returns the drawing mode (bitmask) of this shader.
*
* @return int
*/
public function getMode() {
return $this->shaderMode;
}
/**
* Sets the position of an active area (shader effects will be applied only to this area).
*
* @param Point $topLeftPosition Top left position of the area.
* @param Point $$bottomRightPosition Bottom right position of the area.
* @return Shader
*/
public function useArea(Point $topLeftPosition, Point $bottomRightPosition) {
if($topLeftPosition->x > $bottomRightPosition->x) {
$tmp = $topLeftPosition->x;
$topLeftPosition->x = $bottomRightPosition->x;
$bottomRightPosition->x = $tmp;
}
if($topLeftPosition->y > $bottomRightPosition->y) {
$tmp = $topLeftPosition->y;
$topLeftPosition->y = $bottomRightPosition->y;
$bottomRightPosition->y = $tmp;
}
$this->area = array($topLeftPosition, $bottomRightPosition);
return $this;
}
/**
* Returns the position of an active area (an array of two points: top left, and bottom right)
*
* @return Point[]
*/
public function getArea() {
return $this->area;
}
/**
* Returns shader instructions (PHP code).
*
* @return string
*/
public function getInstructions() {
return $this->shaderContent;
}
}
/**
* Stores point position.
*/
class Point
{
/**
* Point X coordinate.
*
* @var int
*/
public $x;
/**
* Point Y coordinate.
*
* @var int
*/
public $y;
/**
* Creates Point instance.
*
* @param int $x Point X coordinate.
* @param int $y Point Y coordinate.
* @return Point
*/
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
/**
* Returns position of the pixel.
*
* @return int[] X and Y coordinates of the pixel.
*/
public function getPosition() {
return array($this->x, $this->y);
}
}
/**
* Describes image color.
*/
class Color
{
const RED = 1;
const GREEN = 2;
const BLUE = 4;
/**
* HSL color model.
*/
const HSL = 8;
/**
* HSV color model.
*/
const HSV = 16;
/**
* HSI color model.
*/
const HSI = 32;
/**
* RGB value of the color.
*
* @var int
*/
public $rgb;
/**
* Creates color instance.
*
* @param int $rgbOrRedValue Full RGB value or Red value (as int)
* @param mixed $greenValue Green value (if $rgbOrRedValue does not contain entire RGB value)
* @param mixed $blueValue Blue value (if $rgbOrRedValue does not contain entire RGB value)
* @return Color
*/
public function __construct($rgbOrRedValue, $greenValue = null, $blueValue = null) {
if($greenValue === null && $blueValue === null) {
$this->rgb = $rgbOrRedValue;
return;
}
$this->rgb = ($rgbOrRedValue << 16) + ($greenValue << 8) + $blueValue;
}
/**
* Calculates a value of a red component of the RGB value.
*
* Note: should not be used for performance reasons (reason PHP < 5.4 functions overhead).
* @return int
*/
public function getRed() {
return ($this->rgb >> 16) & 0xff;
}
/**
* Calculates a value of a green component of the RGB value.
*
* Note: should not be used for performance reasons (reason PHP < 5.4 functions overhead).
* @return int
*/
public function getGreen() {
return ($this->rgb >> 8) & 0xff;
}
/**
* Calculates a value of a blue component of the RGB value.
*
* Note: should not be used for performance reasons (reason PHP < 5.4 functions overhead).
* @return int
*/
public function getBlue() {
return $this->rgb & 0xff;
}
/**
* Returns color chroma.
*
* @return float
*/
public function getChroma() {
$r = ($this->rgb >> 16) & 0xff;
$g = ($this->rgb >> 8) & 0xff;
$b = $this->rgb & 0xff;
return (max($r, $g, $b) - min($r, $g, $b)) / 255;
}
/**
* Returns color hue.
*
* @return int Value in degrees (0 => 360).
*/
public function getHue() {
$rgb = $this->rgb;
$r = (($rgb >> 16) & 0xff) / 255;
$g = (($rgb >> 8) & 0xff) / 255;
$b = ($rgb & 0xff) / 255;
$hue = rad2deg(atan2(1.7320508075688 /* = sqrt(3) */ * ($g - $b), 2 * $r - $g - $b));
return $hue >= 0 ? $hue : 360 + $hue;
}
/**
* Returns color saturation.
*
* @param int $colorMode Color mode for saturation (use Color::HSV, Color::HSI or Color::HSL as the value), default is Color::HSL
* @return float
*/
public function getSaturation($colorMode = self::HSL) {
$r = (($this->rgb >> 16) & 0xff) / 255;
$g = (($this->rgb >> 8) & 0xff) / 255;
$b = ($this->rgb & 0xff) / 255;
$max = max($r, $g, $b);
$min = min($r, $g, $b);
if($max === 0) {
return 0;
}
if($colorMode === self::HSL) {
$diff = $max - $min;
//$luminance = ($max + $min) / 2;
if($diff < 0.5) {
return $diff / ($max + $min);
} else {
return $diff / (2 - $max - $min);
}
} else if($colorMode === self::HSV) {
return ($max - $min) / $max;
} else if($colorMode === self::HSI) {
if($max - $min === 0) {
return 0;
} else {
return 1 - $min / (($r + $g + $b) / 3);
}
}
throw new Exception('Unknown color mode');
}
/**
* Returns hexadecimal representation of the current color.
*
* @return string
*/
public function getHexValue() {
return str_pad(dechex($this->rgb), 6, '0', STR_PAD_LEFT);
}
/**
* Returns color luminance.
*
* @param int $mode Luminance mode: 0 = fastest, 1 = Digital CCIR601, 2 = Digital ITU-R, 3 = HSP (best quality), Color::HSL = HSL (default), Color::HSV = HSV
* @return float
*/
public function getLuminance($mode = self::HSL) {
$r = ($this->rgb >> 16) & 0xff;
$g = ($this->rgb >> 8) & 0xff;
$b = $this->rgb & 0xff;
switch ($mode) {
case 0:
// fastest, but less accurate.
return (($r + $r + $r + $b + $g + $g + $g + $g) >> 3) / 255;
break;
case 1:
// Digital CCIR601
return (int)(0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
break;
case 2:
// Ditigal ITU-R
return (int)(0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
break;
case 3:
// HSP algorithm
return round(sqrt(0.299 * $r * $r + 0.587 * $g * $g + 0.114 * $b * $b)) / 255;
break;
case self::HSL:
// HSL algorithm
return (max($r, $g, $b) + min($r, $g, $b)) / (2 * 255);
break;
case self::HSV:
// HSV algorithm
return max($r, $g, $b) / 255;
break;
case self::HSI:
// HSI algorithm
return ($r + $g + $b) / (3 * 255);
break;
default:
throw new Exception('Unknown color mode');
break;
}
}
}
/**
* Describes area dimensions.
*/
class Dimensions
{
/**
* Width dimension.
*
* @var int
*/
public $width;
/**
* Height dimension.
*
* @var int
*/
public $height;
/**
* Stores area dimensions.
*
* @param int $width Width of the area.
* @param int $height Height of the area.
* @return Dimensions
*/
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
/**
* Returns an array containing width and height of this area.
*
* @return int[]
*/
public function getSize() {
return array($this->width, $this->height);
}
/**
* Multiplies width and height by a given values.
*
* @param int $x Value to multiply a width dimension.
* @param int $y Value to multiply a height dimension.
* @return Dimensions
*/
public function multiply($x, $y) {
$this->width *= $x;
$this->height *= $y;
return $this;
}
/**
* Adds values to width and height respectively.
*
* @param int $x Value to add to a width dimension.
* @param int $y Value to add to a height dimension.
* @return Dimensions
*/
public function add($x = 0, $y = 0) {
$this->width += $x;
$this->height += $y;
return $this;
}
/**
* Returns width of the diagonal line.
*
* @return double
*/
public function getDiagonalWidth() {
return sqrt($this->width * $this->width + $this->height * $this->height);
}
/**
* Returns the size of area.
*
* @return int
*/
public function getAreaSize() {
return $this->width * $this->height;
}
}
/**
* Describes color RGB channels.
*/
class Channel
{
const RED = 1;
const GREEN = 2;
const BLUE = 4;
const RGB = 7;
}
/**
* Describes vector.
*/
class Vector
{
/**
* An array of components of this vector.
*
* @var double[]
*/
protected $components = array();
/**
* A dimension of this vector.
*
* @var int
*/
protected $dimension;
/**
* An identifier of this vector.
*
* @var mixed
*/
protected $identifier = -1;
/**
* A length of this vector.
*
* @var int
*/
protected $length = null;
/**
* Creates new vector.
*
* @param int $dimension Dimension of this vector.
* @param mixed $identifier Identifier of this vector.
* @param double[] $components Components of this vector.
* @return Vector
*/
public function __construct($dimension, $identifier = -1, $components = null) {
$this->dimension = $dimension;
$this->identifier = $identifier;
$this->components = is_array($components) ? $components : array_fill(0, $dimension, 0);
}
/**
* Adds noise to this vector.
*
* @param int $level Noise level.
* @return Vector
*/
public function addNoise($level) {
$components = array();
foreach($this->components as $component) {
$components[] = $component + rand() * 2 * $level - $level;
}
$this->components = $components;
return $this;
}
/**
* Returns the dimension of this vector.
*
* @return int
*/
public function getDimensions() {
return $this->dimension;
}
/**
* Returns the identifier of this vector.
*
* @return mixed
*/
public function getIdentifier() {
return $this->identifier;
}
/**
* Sets the identifier of this vector.
*
* @param string $name
* @return void
*/
public function setIdentifier($name) {
$this->identifier = $name;
}
/**
* Returns components of this vector.
*
* @return double[]
*/
public function toArray() {
return $this->components;
}
/**
* Returns a component of this vector.
*
* @param int $i Index of the component.
* @return double Value of the selected component.
*/
public function getValue($i) {
if($i > $this->dimension) {
throw new Exception();
} else {
return $this->components[$i];
}
}
/**
* Adds two vectors.
*
* @param Vector $v A vector to add.
* @return Vector
*/
public function add(Vector $v) {
if($this->dimension != $v->getDimensions()) {
throw new Exception("Both vectors must have the same size");
}
$components = array();
$otherComponents = $v->toArray();
foreach($this->components as $i => $component) {
$components[] = $component + $otherComponents[$i];
}
$this->components = $components;
return $this;
}
/**
* Multiplies two vectors or by a given scalar value.
*
* @param mixed $v A vector or scalar value to multiply.
* @return Vector
*/
public function multiply($v) {
$components = array();
if(is_object($v)) {
if(!($v instanceof Vector)) {
throw new Exception('Unspupported data structure');
}
if($this->dimension != $v->getDimensions()) {
throw new Exception("Both vectors must have the same size");
}
$otherComponents = $v->toArray();
foreach($this->components as $i => $component) {
$components[] = $component * $otherComponents[$i];
}
} else {
foreach($this->components as $i => $component) {
$components[] = $component * $v;
}
}
$this->components = $components;
return $this;
}
/**
* Negates this vector.
*
* @return Vector
*/
public function negate() {
$components = array();
foreach($this->components as $index => $component) {
$components[$index] = -$component;
}
$this->components = $components;
return $this;
}
/**
* Adds two vectors.
*
* @param Vector $v A vector to add.
* @return Vector
*/
public function substract(Vector $v) {
if($this->dimension != $v->getDimensions()) {
throw new Exception("Both vectors must have the same size");
}
$components = array();
$otherComponents = $v->toArray();
foreach($this->components as $i => $component) {
$components[] = $component - $otherComponents[$i];
}
$this->components = $components;
return $this;
}
/**
* Returns substracted length
*
* @param Vector $v A vector to substract
* @return float
*/
public function getSubstractedLength(Vector $v) {
if($this->dimension != $v->getDimensions()) {
throw new Exception("Both vectors must have the same size");
}
$j = 0;
$otherComponents = $v->toArray();
foreach($this->components as $i => $component) {
$value = $component - $otherComponents[$i];
$j += $value * $value;
}
return sqrt($j);
}
/**
* Compares two vectors.
*
* @param Vector $v
* @return bool True if vectors are equal, false otherwise.
*/
public function equals(Vector $v) {
if($this->dimension != $v->getDimensions()) {
throw new Exception("Both vectors must have the same size");
}
$otherComponents = $v->toArray();
$ret = true;
for($i = 0; $ret && $i < $this->dimension; ++$i) {
$ret = ($this->components[$i] == $otherComponents[$i]);
}
return $ret;
}
/**
* Performs Vector normalization.
*
* @return Vector
*/
public function normalize() {
$j = $this->getLength();
if($j === 0) {
throw new Exception('Cannot normalize zero length vector');
}
$j *= $j;
$components = array();
foreach($this->components as $component) {
$components[] = sqrt(($component * $component) / $j);
}
$this->components = $components;
return $this;
}
/*
* Calculates pythagorean length of a Vector.
*
* @return int
*/
public function getLength() {
if($this->length !== null) {
return $this->length;
}
$j = 0;
foreach($this->components as $component) {
$j += $component * $component;
}
return ($this->length = sqrt($j));
}
/*
* Calculate sum of Vector components.
*
* @return int
*/
public function sum() {
return array_sum($this->components);
}
/**
* Returns copy of this vector.
*
* @return Vector
*/
public function getCopy() {
return new Vector($this->dimension, $this->identifier, $this->components);
}
}
/**
* Performs image quantization.
*/
class Quantizator
{
/**
* An array of glyphs/vectors to compare with.
*
* @var Vector[]
*/
protected $glyphs = array();
/**
* Adds vector to the vectors database.
*
* @param Vector $v Vector to add.
* @param mixed $identifier Vector identifier.
* @return Quantizator
*/
public function addGlyph(Vector $v, $identifier = null) {
$v = $v->getCopy();
$v->setIdentifier($identifier);
$this->glyphs[] = $v;
return $this;
}
/**
* Finds nearest euklid.
*
* @param Vector $v Vector to compare.
* @param int $noise Noise to add (default: 0 for no noise)
* @return mixed[] array containing ID of the similar vector, and it's distance to the compared vector.
*/
public function findNearestEuklid(Vector $v, $noise = 0) {
$minDimension = 1000000;
foreach($this->glyphs as $index => $w) {
$w = $w->getCopy();
if($noise) {
$w = $w->addNoise($noise);
}
$dimension = $w->getSubstractedLength($v);
if($dimension < $minDimension) {
$ret = $w;
$minDimension = $dimension;
}
}
if(!$ret) {
$ret = $w;
}
return array($ret->getIdentifier(), $minDimension);
}
}
/**
* Blender class.
*/
class Blender
{
/**
* Blending mode: addition.
*/
const USE_ADDITION = 1;
/**
* Blending mode: divide.
*/
const USE_DIVIDE = 2;
/**
* Blending mode: subtract.
*/
const USE_SUBTRACT = 4;
/**
* Blending mode: darken.
*/
const USE_DARKEN = 8;
/**
* Blending mode: lighten.
*/
const USE_LIGHTEN = 16;
/**
* Blending mode: dissolve.
*/
const USE_DISSOLVE = 32;
/**
* Blending mode: difference.
*/
const USE_DIFFERENCE = 64;
/**
* Blending mode: multiply.
*/
const USE_MULTIPLY = 128;
/**
* Blending mode: opacity.
*/
const USE_OPACITY = 256;
}
/**
* Allows advanced image processing.
*/
class Image
{
/**
* A GD2 image resource.
*
* @var resource
*/
protected $image;
/**
* A width of this image.
*
* @var int
*/
protected $width;
/**
* A height of this image.
*
* @var int
*/
protected $height;
/**
* Viewport's position in the parent image.
*
* @var mixed
*/
protected $position;
/**
* Viewport's parent image [not implemented]
*
* @var Image
*/
protected $parentImage;
/**
* Not implemented
*
* @var mixed
*/
protected $useBooster;
/**
* Creates new Image.
*
* @param string $fileName Image file name.
* @param resource $image Image data.
* @return Image
*/
public function __construct($fileName, $image = null, Point $position = null, Dimensions $size = null) {
if(!$image && $fileName) {
$image = imagecreatefromstring(file_get_contents($fileName));
}
if($image && !$position) {
$this->image = imagecreatetruecolor(imagesx($image), imagesy($image));
imagecopy($this->image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
} else {
$this->image = imagecreatetruecolor($size->width, $size->height);
imagecopy($this->image, $image, 0, 0, $position->x, $position->y, $size->width, $size->height);
$this->position = $position;
}
$this->getSize();
}
/**
* Not implemented.
*
* @param mixed $yes
*/
public function useBooster($yes = false) {
$this->useBooster = (bool) $yes;
}
/**
* Returns next power of two representation of the given number.
*
* @param int $number Number to convert.
* @return int Power of two representation.
*/
protected function getNextPowerOfTwo($number) {
$ret = 1;
while($ret < $number) {
$ret <<= 1;
}
return $ret;
}
/**
* Gets image width.
*
* @return int
*/
public function getWidth() {
return $this->width;
}
/**
* Gets image height.
*
* @return int
*/
public function getHeight() {
return $this->height;
}
/**
* Sets image gamma.
*
* @return Image
*/
public function setGamma($level) {
imagegammacorrect($this->image, 1, $level);
return $this;
}
/**
* Resizes this image.
*
* @param mixed $widthOrDimensions New width (or Dimensions) of the this image.
* @param int $height New height of this image (used only if $widthOrDimensions is not an instance of Dimensions class)
* @param bool Use resampling? Default: true (slower)