Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//=======================================================================
3
// File:	GD_IMAGE.INC.PHP
4
// Description:	GD Instance of Image class
5
// Created: 	2006-05-06
6
// Ver:		$Id: gd_image.inc.php 835 2007-01-15 18:57:41Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
 
11
//===================================================
12
// CLASS RGB
13
// Description: Color definitions as RGB triples
14
//===================================================
15
class RGB {
16
    var $rgb_table;
17
    var $img;
18
    function RGB(&$aImg) {
19
	$this->img = &$aImg;
20
 
21
	// Conversion array between color names and RGB
22
	$this->rgb_table = array(
23
	    "aqua"=> array(0,255,255),
24
	    "lime"=> array(0,255,0),
25
	    "teal"=> array(0,128,128),
26
	    "whitesmoke"=>array(245,245,245),
27
	    "gainsboro"=>array(220,220,220),
28
	    "oldlace"=>array(253,245,230),
29
	    "linen"=>array(250,240,230),
30
	    "antiquewhite"=>array(250,235,215),
31
	    "papayawhip"=>array(255,239,213),
32
	    "blanchedalmond"=>array(255,235,205),
33
	    "bisque"=>array(255,228,196),
34
	    "peachpuff"=>array(255,218,185),
35
	    "navajowhite"=>array(255,222,173),
36
	    "moccasin"=>array(255,228,181),
37
	    "cornsilk"=>array(255,248,220),
38
	    "ivory"=>array(255,255,240),
39
	    "lemonchiffon"=>array(255,250,205),
40
	    "seashell"=>array(255,245,238),
41
	    "mintcream"=>array(245,255,250),
42
	    "azure"=>array(240,255,255),
43
	    "aliceblue"=>array(240,248,255),
44
	    "lavender"=>array(230,230,250),
45
	    "lavenderblush"=>array(255,240,245),
46
	    "mistyrose"=>array(255,228,225),
47
	    "white"=>array(255,255,255),
48
	    "black"=>array(0,0,0),
49
	    "darkslategray"=>array(47,79,79),
50
	    "dimgray"=>array(105,105,105),
51
	    "slategray"=>array(112,128,144),
52
	    "lightslategray"=>array(119,136,153),
53
	    "gray"=>array(190,190,190),
54
	    "lightgray"=>array(211,211,211),
55
	    "midnightblue"=>array(25,25,112),
56
	    "navy"=>array(0,0,128),
57
	    "cornflowerblue"=>array(100,149,237),
58
	    "darkslateblue"=>array(72,61,139),
59
	    "slateblue"=>array(106,90,205),
60
	    "mediumslateblue"=>array(123,104,238),
61
	    "lightslateblue"=>array(132,112,255),
62
	    "mediumblue"=>array(0,0,205),
63
	    "royalblue"=>array(65,105,225),
64
	    "blue"=>array(0,0,255),
65
	    "dodgerblue"=>array(30,144,255),
66
	    "deepskyblue"=>array(0,191,255),
67
	    "skyblue"=>array(135,206,235),
68
	    "lightskyblue"=>array(135,206,250),
69
	    "steelblue"=>array(70,130,180),
70
	    "lightred"=>array(211,167,168),
71
	    "lightsteelblue"=>array(176,196,222),
72
	    "lightblue"=>array(173,216,230),
73
	    "powderblue"=>array(176,224,230),
74
	    "paleturquoise"=>array(175,238,238),
75
	    "darkturquoise"=>array(0,206,209),
76
	    "mediumturquoise"=>array(72,209,204),
77
	    "turquoise"=>array(64,224,208),
78
	    "cyan"=>array(0,255,255),
79
	    "lightcyan"=>array(224,255,255),
80
	    "cadetblue"=>array(95,158,160),
81
	    "mediumaquamarine"=>array(102,205,170),
82
	    "aquamarine"=>array(127,255,212),
83
	    "darkgreen"=>array(0,100,0),
84
	    "darkolivegreen"=>array(85,107,47),
85
	    "darkseagreen"=>array(143,188,143),
86
	    "seagreen"=>array(46,139,87),
87
	    "mediumseagreen"=>array(60,179,113),
88
	    "lightseagreen"=>array(32,178,170),
89
	    "palegreen"=>array(152,251,152),
90
	    "springgreen"=>array(0,255,127),
91
	    "lawngreen"=>array(124,252,0),
92
	    "green"=>array(0,255,0),
93
	    "chartreuse"=>array(127,255,0),
94
	    "mediumspringgreen"=>array(0,250,154),
95
	    "greenyellow"=>array(173,255,47),
96
	    "limegreen"=>array(50,205,50),
97
	    "yellowgreen"=>array(154,205,50),
98
	    "forestgreen"=>array(34,139,34),
99
	    "olivedrab"=>array(107,142,35),
100
	    "darkkhaki"=>array(189,183,107),
101
	    "khaki"=>array(240,230,140),
102
	    "palegoldenrod"=>array(238,232,170),
103
	    "lightgoldenrodyellow"=>array(250,250,210),
104
	    "lightyellow"=>array(255,255,200),
105
	    "yellow"=>array(255,255,0),
106
	    "gold"=>array(255,215,0),
107
	    "lightgoldenrod"=>array(238,221,130),
108
	    "goldenrod"=>array(218,165,32),
109
	    "darkgoldenrod"=>array(184,134,11),
110
	    "rosybrown"=>array(188,143,143),
111
	    "indianred"=>array(205,92,92),
112
	    "saddlebrown"=>array(139,69,19),
113
	    "sienna"=>array(160,82,45),
114
	    "peru"=>array(205,133,63),
115
	    "burlywood"=>array(222,184,135),
116
	    "beige"=>array(245,245,220),
117
	    "wheat"=>array(245,222,179),
118
	    "sandybrown"=>array(244,164,96),
119
	    "tan"=>array(210,180,140),
120
	    "chocolate"=>array(210,105,30),
121
	    "firebrick"=>array(178,34,34),
122
	    "brown"=>array(165,42,42),
123
	    "darksalmon"=>array(233,150,122),
124
	    "salmon"=>array(250,128,114),
125
	    "lightsalmon"=>array(255,160,122),
126
	    "orange"=>array(255,165,0),
127
	    "darkorange"=>array(255,140,0),
128
	    "coral"=>array(255,127,80),
129
	    "lightcoral"=>array(240,128,128),
130
	    "tomato"=>array(255,99,71),
131
	    "orangered"=>array(255,69,0),
132
	    "red"=>array(255,0,0),
133
	    "hotpink"=>array(255,105,180),
134
	    "deeppink"=>array(255,20,147),
135
	    "pink"=>array(255,192,203),
136
	    "lightpink"=>array(255,182,193),
137
	    "palevioletred"=>array(219,112,147),
138
	    "maroon"=>array(176,48,96),
139
	    "mediumvioletred"=>array(199,21,133),
140
	    "violetred"=>array(208,32,144),
141
	    "magenta"=>array(255,0,255),
142
	    "violet"=>array(238,130,238),
143
	    "plum"=>array(221,160,221),
144
	    "orchid"=>array(218,112,214),
145
	    "mediumorchid"=>array(186,85,211),
146
	    "darkorchid"=>array(153,50,204),
147
	    "darkviolet"=>array(148,0,211),
148
	    "blueviolet"=>array(138,43,226),
149
	    "purple"=>array(160,32,240),
150
	    "mediumpurple"=>array(147,112,219),
151
	    "thistle"=>array(216,191,216),
152
	    "snow1"=>array(255,250,250),
153
	    "snow2"=>array(238,233,233),
154
	    "snow3"=>array(205,201,201),
155
	    "snow4"=>array(139,137,137),
156
	    "seashell1"=>array(255,245,238),
157
	    "seashell2"=>array(238,229,222),
158
	    "seashell3"=>array(205,197,191),
159
	    "seashell4"=>array(139,134,130),
160
	    "AntiqueWhite1"=>array(255,239,219),
161
	    "AntiqueWhite2"=>array(238,223,204),
162
	    "AntiqueWhite3"=>array(205,192,176),
163
	    "AntiqueWhite4"=>array(139,131,120),
164
	    "bisque1"=>array(255,228,196),
165
	    "bisque2"=>array(238,213,183),
166
	    "bisque3"=>array(205,183,158),
167
	    "bisque4"=>array(139,125,107),
168
	    "peachPuff1"=>array(255,218,185),
169
	    "peachpuff2"=>array(238,203,173),
170
	    "peachpuff3"=>array(205,175,149),
171
	    "peachpuff4"=>array(139,119,101),
172
	    "navajowhite1"=>array(255,222,173),
173
	    "navajowhite2"=>array(238,207,161),
174
	    "navajowhite3"=>array(205,179,139),
175
	    "navajowhite4"=>array(139,121,94),
176
	    "lemonchiffon1"=>array(255,250,205),
177
	    "lemonchiffon2"=>array(238,233,191),
178
	    "lemonchiffon3"=>array(205,201,165),
179
	    "lemonchiffon4"=>array(139,137,112),
180
	    "ivory1"=>array(255,255,240),
181
	    "ivory2"=>array(238,238,224),
182
	    "ivory3"=>array(205,205,193),
183
	    "ivory4"=>array(139,139,131),
184
	    "honeydew"=>array(193,205,193),
185
	    "lavenderblush1"=>array(255,240,245),
186
	    "lavenderblush2"=>array(238,224,229),
187
	    "lavenderblush3"=>array(205,193,197),
188
	    "lavenderblush4"=>array(139,131,134),
189
	    "mistyrose1"=>array(255,228,225),
190
	    "mistyrose2"=>array(238,213,210),
191
	    "mistyrose3"=>array(205,183,181),
192
	    "mistyrose4"=>array(139,125,123),
193
	    "azure1"=>array(240,255,255),
194
	    "azure2"=>array(224,238,238),
195
	    "azure3"=>array(193,205,205),
196
	    "azure4"=>array(131,139,139),
197
	    "slateblue1"=>array(131,111,255),
198
	    "slateblue2"=>array(122,103,238),
199
	    "slateblue3"=>array(105,89,205),
200
	    "slateblue4"=>array(71,60,139),
201
	    "royalblue1"=>array(72,118,255),
202
	    "royalblue2"=>array(67,110,238),
203
	    "royalblue3"=>array(58,95,205),
204
	    "royalblue4"=>array(39,64,139),
205
	    "dodgerblue1"=>array(30,144,255),
206
	    "dodgerblue2"=>array(28,134,238),
207
	    "dodgerblue3"=>array(24,116,205),
208
	    "dodgerblue4"=>array(16,78,139),
209
	    "steelblue1"=>array(99,184,255),
210
	    "steelblue2"=>array(92,172,238),
211
	    "steelblue3"=>array(79,148,205),
212
	    "steelblue4"=>array(54,100,139),
213
	    "deepskyblue1"=>array(0,191,255),
214
	    "deepskyblue2"=>array(0,178,238),
215
	    "deepskyblue3"=>array(0,154,205),
216
	    "deepskyblue4"=>array(0,104,139),
217
	    "skyblue1"=>array(135,206,255),
218
	    "skyblue2"=>array(126,192,238),
219
	    "skyblue3"=>array(108,166,205),
220
	    "skyblue4"=>array(74,112,139),
221
	    "lightskyblue1"=>array(176,226,255),
222
	    "lightskyblue2"=>array(164,211,238),
223
	    "lightskyblue3"=>array(141,182,205),
224
	    "lightskyblue4"=>array(96,123,139),
225
	    "slategray1"=>array(198,226,255),
226
	    "slategray2"=>array(185,211,238),
227
	    "slategray3"=>array(159,182,205),
228
	    "slategray4"=>array(108,123,139),
229
	    "lightsteelblue1"=>array(202,225,255),
230
	    "lightsteelblue2"=>array(188,210,238),
231
	    "lightsteelblue3"=>array(162,181,205),
232
	    "lightsteelblue4"=>array(110,123,139),
233
	    "lightblue1"=>array(191,239,255),
234
	    "lightblue2"=>array(178,223,238),
235
	    "lightblue3"=>array(154,192,205),
236
	    "lightblue4"=>array(104,131,139),
237
	    "lightcyan1"=>array(224,255,255),
238
	    "lightcyan2"=>array(209,238,238),
239
	    "lightcyan3"=>array(180,205,205),
240
	    "lightcyan4"=>array(122,139,139),
241
	    "paleturquoise1"=>array(187,255,255),
242
	    "paleturquoise2"=>array(174,238,238),
243
	    "paleturquoise3"=>array(150,205,205),
244
	    "paleturquoise4"=>array(102,139,139),
245
	    "cadetblue1"=>array(152,245,255),
246
	    "cadetblue2"=>array(142,229,238),
247
	    "cadetblue3"=>array(122,197,205),
248
	    "cadetblue4"=>array(83,134,139),
249
	    "turquoise1"=>array(0,245,255),
250
	    "turquoise2"=>array(0,229,238),
251
	    "turquoise3"=>array(0,197,205),
252
	    "turquoise4"=>array(0,134,139),
253
	    "cyan1"=>array(0,255,255),
254
	    "cyan2"=>array(0,238,238),
255
	    "cyan3"=>array(0,205,205),
256
	    "cyan4"=>array(0,139,139),
257
	    "darkslategray1"=>array(151,255,255),
258
	    "darkslategray2"=>array(141,238,238),
259
	    "darkslategray3"=>array(121,205,205),
260
	    "darkslategray4"=>array(82,139,139),
261
	    "aquamarine1"=>array(127,255,212),
262
	    "aquamarine2"=>array(118,238,198),
263
	    "aquamarine3"=>array(102,205,170),
264
	    "aquamarine4"=>array(69,139,116),
265
	    "darkseagreen1"=>array(193,255,193),
266
	    "darkseagreen2"=>array(180,238,180),
267
	    "darkseagreen3"=>array(155,205,155),
268
	    "darkseagreen4"=>array(105,139,105),
269
	    "seagreen1"=>array(84,255,159),
270
	    "seagreen2"=>array(78,238,148),
271
	    "seagreen3"=>array(67,205,128),
272
	    "seagreen4"=>array(46,139,87),
273
	    "palegreen1"=>array(154,255,154),
274
	    "palegreen2"=>array(144,238,144),
275
	    "palegreen3"=>array(124,205,124),
276
	    "palegreen4"=>array(84,139,84),
277
	    "springgreen1"=>array(0,255,127),
278
	    "springgreen2"=>array(0,238,118),
279
	    "springgreen3"=>array(0,205,102),
280
	    "springgreen4"=>array(0,139,69),
281
	    "chartreuse1"=>array(127,255,0),
282
	    "chartreuse2"=>array(118,238,0),
283
	    "chartreuse3"=>array(102,205,0),
284
	    "chartreuse4"=>array(69,139,0),
285
	    "olivedrab1"=>array(192,255,62),
286
	    "olivedrab2"=>array(179,238,58),
287
	    "olivedrab3"=>array(154,205,50),
288
	    "olivedrab4"=>array(105,139,34),
289
	    "darkolivegreen1"=>array(202,255,112),
290
	    "darkolivegreen2"=>array(188,238,104),
291
	    "darkolivegreen3"=>array(162,205,90),
292
	    "darkolivegreen4"=>array(110,139,61),
293
	    "khaki1"=>array(255,246,143),
294
	    "khaki2"=>array(238,230,133),
295
	    "khaki3"=>array(205,198,115),
296
	    "khaki4"=>array(139,134,78),
297
	    "lightgoldenrod1"=>array(255,236,139),
298
	    "lightgoldenrod2"=>array(238,220,130),
299
	    "lightgoldenrod3"=>array(205,190,112),
300
	    "lightgoldenrod4"=>array(139,129,76),
301
	    "yellow1"=>array(255,255,0),
302
	    "yellow2"=>array(238,238,0),
303
	    "yellow3"=>array(205,205,0),
304
	    "yellow4"=>array(139,139,0),
305
	    "gold1"=>array(255,215,0),
306
	    "gold2"=>array(238,201,0),
307
	    "gold3"=>array(205,173,0),
308
	    "gold4"=>array(139,117,0),
309
	    "goldenrod1"=>array(255,193,37),
310
	    "goldenrod2"=>array(238,180,34),
311
	    "goldenrod3"=>array(205,155,29),
312
	    "goldenrod4"=>array(139,105,20),
313
	    "darkgoldenrod1"=>array(255,185,15),
314
	    "darkgoldenrod2"=>array(238,173,14),
315
	    "darkgoldenrod3"=>array(205,149,12),
316
	    "darkgoldenrod4"=>array(139,101,8),
317
	    "rosybrown1"=>array(255,193,193),
318
	    "rosybrown2"=>array(238,180,180),
319
	    "rosybrown3"=>array(205,155,155),
320
	    "rosybrown4"=>array(139,105,105),
321
	    "indianred1"=>array(255,106,106),
322
	    "indianred2"=>array(238,99,99),
323
	    "indianred3"=>array(205,85,85),
324
	    "indianred4"=>array(139,58,58),
325
	    "sienna1"=>array(255,130,71),
326
	    "sienna2"=>array(238,121,66),
327
	    "sienna3"=>array(205,104,57),
328
	    "sienna4"=>array(139,71,38),
329
	    "burlywood1"=>array(255,211,155),
330
	    "burlywood2"=>array(238,197,145),
331
	    "burlywood3"=>array(205,170,125),
332
	    "burlywood4"=>array(139,115,85),
333
	    "wheat1"=>array(255,231,186),
334
	    "wheat2"=>array(238,216,174),
335
	    "wheat3"=>array(205,186,150),
336
	    "wheat4"=>array(139,126,102),
337
	    "tan1"=>array(255,165,79),
338
	    "tan2"=>array(238,154,73),
339
	    "tan3"=>array(205,133,63),
340
	    "tan4"=>array(139,90,43),
341
	    "chocolate1"=>array(255,127,36),
342
	    "chocolate2"=>array(238,118,33),
343
	    "chocolate3"=>array(205,102,29),
344
	    "chocolate4"=>array(139,69,19),
345
	    "firebrick1"=>array(255,48,48),
346
	    "firebrick2"=>array(238,44,44),
347
	    "firebrick3"=>array(205,38,38),
348
	    "firebrick4"=>array(139,26,26),
349
	    "brown1"=>array(255,64,64),
350
	    "brown2"=>array(238,59,59),
351
	    "brown3"=>array(205,51,51),
352
	    "brown4"=>array(139,35,35),
353
	    "salmon1"=>array(255,140,105),
354
	    "salmon2"=>array(238,130,98),
355
	    "salmon3"=>array(205,112,84),
356
	    "salmon4"=>array(139,76,57),
357
	    "lightsalmon1"=>array(255,160,122),
358
	    "lightsalmon2"=>array(238,149,114),
359
	    "lightsalmon3"=>array(205,129,98),
360
	    "lightsalmon4"=>array(139,87,66),
361
	    "orange1"=>array(255,165,0),
362
	    "orange2"=>array(238,154,0),
363
	    "orange3"=>array(205,133,0),
364
	    "orange4"=>array(139,90,0),
365
	    "darkorange1"=>array(255,127,0),
366
	    "darkorange2"=>array(238,118,0),
367
	    "darkorange3"=>array(205,102,0),
368
	    "darkorange4"=>array(139,69,0),
369
	    "coral1"=>array(255,114,86),
370
	    "coral2"=>array(238,106,80),
371
	    "coral3"=>array(205,91,69),
372
	    "coral4"=>array(139,62,47),
373
	    "tomato1"=>array(255,99,71),
374
	    "tomato2"=>array(238,92,66),
375
	    "tomato3"=>array(205,79,57),
376
	    "tomato4"=>array(139,54,38),
377
	    "orangered1"=>array(255,69,0),
378
	    "orangered2"=>array(238,64,0),
379
	    "orangered3"=>array(205,55,0),
380
	    "orangered4"=>array(139,37,0),
381
	    "deeppink1"=>array(255,20,147),
382
	    "deeppink2"=>array(238,18,137),
383
	    "deeppink3"=>array(205,16,118),
384
	    "deeppink4"=>array(139,10,80),
385
	    "hotpink1"=>array(255,110,180),
386
	    "hotpink2"=>array(238,106,167),
387
	    "hotpink3"=>array(205,96,144),
388
	    "hotpink4"=>array(139,58,98),
389
	    "pink1"=>array(255,181,197),
390
	    "pink2"=>array(238,169,184),
391
	    "pink3"=>array(205,145,158),
392
	    "pink4"=>array(139,99,108),
393
	    "lightpink1"=>array(255,174,185),
394
	    "lightpink2"=>array(238,162,173),
395
	    "lightpink3"=>array(205,140,149),
396
	    "lightpink4"=>array(139,95,101),
397
	    "palevioletred1"=>array(255,130,171),
398
	    "palevioletred2"=>array(238,121,159),
399
	    "palevioletred3"=>array(205,104,137),
400
	    "palevioletred4"=>array(139,71,93),
401
	    "maroon1"=>array(255,52,179),
402
	    "maroon2"=>array(238,48,167),
403
	    "maroon3"=>array(205,41,144),
404
	    "maroon4"=>array(139,28,98),
405
	    "violetred1"=>array(255,62,150),
406
	    "violetred2"=>array(238,58,140),
407
	    "violetred3"=>array(205,50,120),
408
	    "violetred4"=>array(139,34,82),
409
	    "magenta1"=>array(255,0,255),
410
	    "magenta2"=>array(238,0,238),
411
	    "magenta3"=>array(205,0,205),
412
	    "magenta4"=>array(139,0,139),
413
	    "mediumred"=>array(140,34,34),
414
	    "orchid1"=>array(255,131,250),
415
	    "orchid2"=>array(238,122,233),
416
	    "orchid3"=>array(205,105,201),
417
	    "orchid4"=>array(139,71,137),
418
	    "plum1"=>array(255,187,255),
419
	    "plum2"=>array(238,174,238),
420
	    "plum3"=>array(205,150,205),
421
	    "plum4"=>array(139,102,139),
422
	    "mediumorchid1"=>array(224,102,255),
423
	    "mediumorchid2"=>array(209,95,238),
424
	    "mediumorchid3"=>array(180,82,205),
425
	    "mediumorchid4"=>array(122,55,139),
426
	    "darkorchid1"=>array(191,62,255),
427
	    "darkorchid2"=>array(178,58,238),
428
	    "darkorchid3"=>array(154,50,205),
429
	    "darkorchid4"=>array(104,34,139),
430
	    "purple1"=>array(155,48,255),
431
	    "purple2"=>array(145,44,238),
432
	    "purple3"=>array(125,38,205),
433
	    "purple4"=>array(85,26,139),
434
	    "mediumpurple1"=>array(171,130,255),
435
	    "mediumpurple2"=>array(159,121,238),
436
	    "mediumpurple3"=>array(137,104,205),
437
	    "mediumpurple4"=>array(93,71,139),
438
	    "thistle1"=>array(255,225,255),
439
	    "thistle2"=>array(238,210,238),
440
	    "thistle3"=>array(205,181,205),
441
	    "thistle4"=>array(139,123,139),
442
	    "gray1"=>array(10,10,10),
443
	    "gray2"=>array(40,40,30),
444
	    "gray3"=>array(70,70,70),
445
	    "gray4"=>array(100,100,100),
446
	    "gray5"=>array(130,130,130),
447
	    "gray6"=>array(160,160,160),
448
	    "gray7"=>array(190,190,190),
449
	    "gray8"=>array(210,210,210),
450
	    "gray9"=>array(240,240,240),
451
	    "darkgray"=>array(100,100,100),
452
	    "darkblue"=>array(0,0,139),
453
	    "darkcyan"=>array(0,139,139),
454
	    "darkmagenta"=>array(139,0,139),
455
	    "darkred"=>array(139,0,0),
456
	    "silver"=>array(192, 192, 192),
457
	    "eggplant"=>array(144,176,168),
458
	    "lightgreen"=>array(144,238,144));
459
    }
460
//----------------
461
// PUBLIC METHODS
462
    // Colors can be specified as either
463
    // 1. #xxxxxx			HTML style
464
    // 2. "colorname" 	as a named color
465
    // 3. array(r,g,b)	RGB triple
466
    // This function translates this to a native RGB format and returns an
467
    // RGB triple.
468
    function Color($aColor) {
469
	if (is_string($aColor)) {
470
	    // Strip of any alpha factor
471
	    $pos = strpos($aColor,'@');
472
	    if( $pos === false ) {
473
		$alpha = 0;
474
	    }
475
	    else {
476
		$pos2 = strpos($aColor,':');
477
		if( $pos2===false )
478
		    $pos2 = $pos-1; // Sentinel
479
		if( $pos > $pos2 ) {
480
		    $alpha = substr($aColor,$pos+1);
481
		    $aColor = substr($aColor,0,$pos);
482
		}
483
		else {
484
		    $alpha = substr($aColor,$pos+1,$pos2-$pos-1);
485
		    $aColor = substr($aColor,0,$pos).substr($aColor,$pos2);
486
		}
487
	    }
488
 
489
	    // Extract potential adjustment figure at end of color
490
	    // specification
491
	    $pos = strpos($aColor,":");
492
	    if( $pos === false ) {
493
		$adj = 1.0;
494
	    }
495
	    else {
496
		$adj = 0.0 + substr($aColor,$pos+1);
497
		$aColor = substr($aColor,0,$pos);
498
	    }
499
	    if( $adj < 0 )
500
		JpGraphError::RaiseL(25077);//('Adjustment factor for color must be > 0');
501
 
502
	    if (substr($aColor, 0, 1) == "#") {
503
		$r = hexdec(substr($aColor, 1, 2));
504
		$g = hexdec(substr($aColor, 3, 2));
505
		$b = hexdec(substr($aColor, 5, 2));
506
	    } else {
507
      		if(!isset($this->rgb_table[$aColor]) )
508
		    JpGraphError::RaiseL(25078,$aColor);//(" Unknown color: $aColor");
509
		$tmp=$this->rgb_table[$aColor];
510
		$r = $tmp[0];
511
		$g = $tmp[1];
512
		$b = $tmp[2];
513
	    }
514
	    // Scale adj so that an adj=2 always
515
	    // makes the color 100% white (i.e. 255,255,255.
516
	    // and adj=1 neutral and adj=0 black.
517
	    if( $adj > 1 ) {
518
		$m = ($adj-1.0)*(255-min(255,min($r,min($g,$b))));
519
		return array(min(255,$r+$m), min(255,$g+$m), min(255,$b+$m),$alpha);
520
	    }
521
	    elseif( $adj < 1 ) {
522
		$m = ($adj-1.0)*max(255,max($r,max($g,$b)));
523
		return array(max(0,$r+$m), max(0,$g+$m), max(0,$b+$m),$alpha);
524
	    }
525
	    else {
526
		return array($r,$g,$b,$alpha);
527
	    }
528
 
529
	} elseif( is_array($aColor) ) {
530
	    if( count($aColor)==3 ) {
531
		$aColor[3]=0;
532
		return $aColor;
533
	    }
534
	    else
535
		return $aColor;
536
	}
537
	else
538
	    JpGraphError::RaiseL(25079,$aColor,count($aColor));//(" Unknown color specification: $aColor , size=".count($aColor));
539
    }
540
 
541
    // Compare two colors
542
    // return true if equal
543
    function Equal($aCol1,$aCol2) {
544
	$c1 = $this->Color($aCol1);
545
	$c2 = $this->Color($aCol2);
546
	if( $c1[0]==$c2[0] && $c1[1]==$c2[1] && $c1[2]==$c2[2] )
547
	    return true;
548
	else
549
	    return false;
550
    }
551
 
552
    // Allocate a new color in the current image
553
    // Return new color index, -1 if no more colors could be allocated
554
    function Allocate($aColor,$aAlpha=0.0) {
555
	list ($r, $g, $b, $a) = $this->color($aColor);
556
	// If alpha is specified in the color string then this
557
	// takes precedence over the second argument
558
	if( $a > 0 )
559
	    $aAlpha = $a;
560
	if( $aAlpha < 0 || $aAlpha > 1 ) {
561
	    JpGraphError::RaiseL(25080);//('Alpha parameter for color must be between 0.0 and 1.0');
562
	}
563
	return imagecolorresolvealpha($this->img, $r, $g, $b, round($aAlpha * 127));
564
    }
565
} // Class
566
 
567
 
568
//===================================================
569
// CLASS Image
570
// Description: Wrapper class with some goodies to form the
571
// Interface to low level image drawing routines.
572
//===================================================
573
class Image {
574
    var $img_format;
575
    var $expired=true;
576
    var $img=null;
577
    var $left_margin=30,$right_margin=20,$top_margin=20,$bottom_margin=30;
578
    var $plotwidth=0,$plotheight=0;
579
    var $rgb=null;
580
    var $current_color,$current_color_name;
581
    var $lastx=0, $lasty=0;
582
    var $width=0, $height=0;
583
    var $line_weight=1;
584
    var $line_style=1;	// Default line style is solid
585
    var $obs_list=array();
586
    var $font_size=12,$font_family=FF_FONT1, $font_style=FS_NORMAL;
587
    var $font_file='';
588
    var $text_halign="left",$text_valign="bottom";
589
    var $ttf=null;
590
    var $use_anti_aliasing=false;
591
    var $quality=null;
592
    var $colorstack=array(),$colorstackidx=0;
593
    var $canvascolor = 'white' ;
594
    var $langconv = null ;
595
 
596
    //---------------
597
    // CONSTRUCTOR
598
    function Image($aWidth,$aHeight,$aFormat=DEFAULT_GFORMAT,$aSetAutoMargin=true) {
599
	$this->CreateImgCanvas($aWidth,$aHeight);
600
	if( $aSetAutoMargin )
601
	    $this->SetAutoMargin();
602
 
603
	if( !$this->SetImgFormat($aFormat) ) {
604
	    JpGraphError::RaiseL(25081,$aFormat);//("JpGraph: Selected graphic format is either not supported or unknown [$aFormat]");
605
	}
606
	$this->ttf = new TTF();
607
	$this->langconv = new LanguageConv();
608
    }
609
 
610
    // Should we use anti-aliasing. Note: This really slows down graphics!
611
    function SetAntiAliasing() {
612
	$this->use_anti_aliasing=true;
613
    }
614
 
615
    function CreateRawCanvas($aWidth=0,$aHeight=0) {
616
	if( $aWidth <= 1 || $aHeight <= 1 ) {
617
	    JpGraphError::RaiseL(25082,$aWidth,$aHeight);//("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)");
618
	}
619
	$this->img = @imagecreatetruecolor($aWidth, $aHeight);
620
	if( $this->img < 1 ) {
621
	    JpGraphError::RaiseL(25126);
622
	    //die("Can't create truecolor image. Check that you really have GD2 library installed.");
623
	}
624
	$this->SetAlphaBlending();
625
	if( $this->rgb != null )
626
	    $this->rgb->img = $this->img ;
627
	else
628
	    $this->rgb = new RGB($this->img);
629
    }
630
 
631
    function CloneCanvasH() {
632
	$oldimage = $this->img;
633
	$this->CreateRawCanvas($this->width,$this->height);
634
	imagecopy($this->img,$oldimage,0,0,0,0,$this->width,$this->height);
635
	return $oldimage;
636
    }
637
 
638
    function CreateImgCanvas($aWidth=0,$aHeight=0) {
639
 
640
	$old = array($this->img,$this->width,$this->height);
641
 
642
	$aWidth = round($aWidth);
643
	$aHeight = round($aHeight);
644
 
645
	$this->width=$aWidth;
646
	$this->height=$aHeight;
647
 
648
 
649
	if( $aWidth==0 || $aHeight==0 ) {
650
	    // We will set the final size later.
651
	    // Note: The size must be specified before any other
652
	    // img routines that stroke anything are called.
653
	    $this->img = null;
654
	    $this->rgb = null;
655
	    return $old;
656
	}
657
 
658
	$this->CreateRawCanvas($aWidth,$aHeight);
659
 
660
	// Set canvas color (will also be the background color for a
661
	// a pallett image
662
	$this->SetColor($this->canvascolor);
663
	$this->FilledRectangle(0,0,$aWidth,$aHeight);
664
 
665
	return $old ;
666
    }
667
 
668
    function CopyCanvasH($aToHdl,$aFromHdl,$aToX,$aToY,$aFromX,$aFromY,$aWidth,$aHeight,$aw=-1,$ah=-1) {
669
	if( $aw === -1 ) {
670
	    $aw = $aWidth;
671
	    $ah = $aHeight;
672
	    $f = 'imagecopyresized';
673
	}
674
	else {
675
	    $f = 'imagecopyresampled' ;
676
	}
677
	$f($aToHdl,$aFromHdl,
678
	   $aToX,$aToY,$aFromX,$aFromY, $aWidth,$aHeight,$aw,$ah);
679
    }
680
 
681
    function Copy($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1) {
682
	$this->CopyCanvasH($this->img,$fromImg,$toX,$toY,$fromX,$fromY,
683
			   $toWidth,$toHeight,$fromWidth,$fromHeight);
684
    }
685
 
686
    function CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1,$aMix=100) {
687
	if( $aMix == 100 ) {
688
	    $this->CopyCanvasH($this->img,$fromImg,
689
			       $toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth,$fromHeight);
690
	}
691
	else {
692
	    if( ($fromWidth  != -1 && ($fromWidth != $toWidth))  ||
693
		($fromHeight != -1 && ($fromHeight != $fromHeight)) ) {
694
		// Create a new canvas that will hold the re-scaled original from image
695
		if( $toWidth <= 1 || $toHeight <= 1 ) {
696
		    JpGraphError::RaiseL(25083);//('Illegal image size when copying image. Size for copied to image is 1 pixel or less.');
697
		}
698
		$tmpimg = @imagecreatetruecolor($toWidth, $toHeight);
699
		if( $tmpimg < 1 ) {
700
		    JpGraphError::RaiseL(25084);//('Failed to create temporary GD canvas. Out of memory ?');
701
		}
702
		$this->CopyCanvasH($tmpimg,$fromImg,0,0,0,0,
703
				   $toWidth,$toHeight,$fromWidth,$fromHeight);
704
		$fromImg = $tmpimg;
705
	    }
706
	    imagecopymerge($this->img,$fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$aMix);
707
	}
708
    }
709
 
710
    function GetWidth($aImg=null) {
711
	if( $aImg === null )
712
	    $aImg = $this->img;
713
	return imagesx($aImg);
714
    }
715
 
716
    function GetHeight($aImg=null) {
717
	if( $aImg === null )
718
	    $aImg = $this->img;
719
	return imagesy($aImg);
720
    }
721
 
722
    function CreateFromString($aStr) {
723
	$img = @imagecreatefromstring($aStr);
724
	if( $img === false ) {
725
	    JpGraphError::RaiseL(25085);//('An image can not be created from the supplied string. It is either in a format not supported or the string is representing an corrupt image.');
726
	}
727
	return $img;
728
    }
729
 
730
    function SetCanvasH($aHdl) {
731
	$this->img = $aHdl;
732
	$this->rgb->img = $aHdl;
733
    }
734
 
735
    function SetCanvasColor($aColor) {
736
	$this->canvascolor = $aColor ;
737
    }
738
 
739
    function SetAlphaBlending($aFlg=true) {
740
	ImageAlphaBlending($this->img,$aFlg);
741
    }
742
 
743
 
744
    function SetAutoMargin() {
745
	GLOBAL $gJpgBrandTiming;
746
	$min_bm=10;
747
	/*
748
	if( $gJpgBrandTiming )
749
	    $min_bm=15;
750
	*/
751
	$lm = min(40,$this->width/7);
752
	$rm = min(20,$this->width/10);
753
	$tm = max(20,$this->height/7);
754
	$bm = max($min_bm,$this->height/7);
755
	$this->SetMargin($lm,$rm,$tm,$bm);
756
    }
757
 
758
 
759
    //---------------
760
    // PUBLIC METHODS
761
 
762
    function SetFont($family,$style=FS_NORMAL,$size=10) {
763
	$this->font_family=$family;
764
	$this->font_style=$style;
765
	$this->font_size=$size;
766
	$this->font_file='';
767
	if( ($this->font_family==FF_FONT1 || $this->font_family==FF_FONT2) && $this->font_style==FS_BOLD ){
768
	    ++$this->font_family;
769
	}
770
	if( $this->font_family > FF_FONT2+1 ) { // A TTF font so get the font file
771
 
772
	    // Check that this PHP has support for TTF fonts
773
	    if( !function_exists('imagettfbbox') ) {
774
		JpGraphError::RaiseL(25087);//('This PHP build has not been configured with TTF support. You need to recompile your PHP installation with FreeType support.');
775
	    }
776
	    $this->font_file = $this->ttf->File($this->font_family,$this->font_style);
777
	}
778
    }
779
 
780
    // Get the specific height for a text string
781
    function GetTextHeight($txt="",$angle=0) {
782
	$tmp = split("\n",$txt);
783
	$n = count($tmp);
784
	$m=0;
785
	for($i=0; $i< $n; ++$i)
786
	    $m = max($m,strlen($tmp[$i]));
787
 
788
	if( $this->font_family <= FF_FONT2+1 ) {
789
	    if( $angle==0 ) {
790
		$h = imagefontheight($this->font_family);
791
		if( $h === false ) {
792
		    JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
793
		}
794
 
795
		return $n*$h;
796
	    }
797
	    else {
798
		$w = @imagefontwidth($this->font_family);
799
		if( $w === false ) {
800
		    JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
801
		}
802
 
803
		return $m*$w;
804
	    }
805
	}
806
	else {
807
	    $bbox = $this->GetTTFBBox($txt,$angle);
808
	    return $bbox[1]-$bbox[5];
809
	}
810
    }
811
 
812
    // Estimate font height
813
    function GetFontHeight($angle=0) {
814
	$txt = "XOMg";
815
	return $this->GetTextHeight($txt,$angle);
816
    }
817
 
818
    // Approximate font width with width of letter "O"
819
    function GetFontWidth($angle=0) {
820
	$txt = 'O';
821
	return $this->GetTextWidth($txt,$angle);
822
    }
823
 
824
    // Get actual width of text in absolute pixels
825
    function GetTextWidth($txt,$angle=0) {
826
 
827
	$tmp = split("\n",$txt);
828
	$n = count($tmp);
829
	if( $this->font_family <= FF_FONT2+1 ) {
830
 
831
	    $m=0;
832
	    for($i=0; $i < $n; ++$i) {
833
		$l=strlen($tmp[$i]);
834
		if( $l > $m ) {
835
		    $m = $l;
836
		}
837
	    }
838
 
839
	    if( $angle==0 ) {
840
		$w = @imagefontwidth($this->font_family);
841
		if( $w === false ) {
842
		    JpGraphError::RaiseL(25088);//('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
843
		}
844
		return $m*$w;
845
	    }
846
	    else {
847
		// 90 degrees internal so height becomes width
848
		$h = @imagefontheight($this->font_family);
849
		if( $h === false ) {
850
		    JpGraphError::RaiseL(25089);//('You have a misconfigured GD font support. The call to imagefontheight() fails.');
851
		}
852
		return $n*$h;
853
	    }
854
	}
855
	else {
856
	    // For TTF fonts we must walk through a lines and find the
857
	    // widest one which we use as the width of the multi-line
858
	    // paragraph
859
	    $m=0;
860
	    for( $i=0; $i < $n; ++$i ) {
861
		$bbox = $this->GetTTFBBox($tmp[$i],$angle);
862
		$mm =  $bbox[2] - $bbox[0];
863
		if( $mm > $m )
864
		    $m = $mm;
865
	    }
866
	    return $m;
867
	}
868
    }
869
 
870
    // Draw text with a box around it
871
    function StrokeBoxedText($x,$y,$txt,$dir=0,$fcolor="white",$bcolor="black",
872
			     $shadowcolor=false,$paragraph_align="left",
873
			     $xmarg=6,$ymarg=4,$cornerradius=0,$dropwidth=3) {
874
 
875
	if( !is_numeric($dir) ) {
876
	    if( $dir=="h" ) $dir=0;
877
	    elseif( $dir=="v" ) $dir=90;
878
	    else JpGraphError::RaiseL(25090,$dir);//(" Unknown direction specified in call to StrokeBoxedText() [$dir]");
879
	}
880
 
881
	if( $this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2+1) {
882
	    $width=$this->GetTextWidth($txt,$dir) ;
883
	    $height=$this->GetTextHeight($txt,$dir) ;
884
	}
885
	else {
886
	    $width=$this->GetBBoxWidth($txt,$dir) ;
887
	    $height=$this->GetBBoxHeight($txt,$dir) ;
888
	}
889
 
890
	$height += 2*$ymarg;
891
	$width  += 2*$xmarg;
892
 
893
	if( $this->text_halign=="right" ) $x -= $width;
894
	elseif( $this->text_halign=="center" ) $x -= $width/2;
895
	if( $this->text_valign=="bottom" ) $y -= $height;
896
	elseif( $this->text_valign=="center" ) $y -= $height/2;
897
 
898
	if( $shadowcolor ) {
899
	    $this->PushColor($shadowcolor);
900
	    $this->FilledRoundedRectangle($x-$xmarg+$dropwidth,$y-$ymarg+$dropwidth,
901
					  $x+$width+$dropwidth,$y+$height-$ymarg+$dropwidth,
902
					  $cornerradius);
903
	    $this->PopColor();
904
	    $this->PushColor($fcolor);
905
	    $this->FilledRoundedRectangle($x-$xmarg,$y-$ymarg,
906
					  $x+$width,$y+$height-$ymarg,
907
					  $cornerradius);
908
	    $this->PopColor();
909
	    $this->PushColor($bcolor);
910
	    $this->RoundedRectangle($x-$xmarg,$y-$ymarg,
911
				    $x+$width,$y+$height-$ymarg,$cornerradius);
912
	    $this->PopColor();
913
	}
914
	else {
915
	    if( $fcolor ) {
916
		$oc=$this->current_color;
917
		$this->SetColor($fcolor);
918
		$this->FilledRoundedRectangle($x-$xmarg,$y-$ymarg,$x+$width,$y+$height-$ymarg,$cornerradius);
919
		$this->current_color=$oc;
920
	    }
921
	    if( $bcolor ) {
922
		$oc=$this->current_color;
923
		$this->SetColor($bcolor);
924
		$this->RoundedRectangle($x-$xmarg,$y-$ymarg,$x+$width,$y+$height-$ymarg,$cornerradius);
925
		$this->current_color=$oc;
926
	    }
927
	}
928
 
929
	$h=$this->text_halign;
930
	$v=$this->text_valign;
931
	$this->SetTextAlign("left","top");
932
	$this->StrokeText($x, $y, $txt, $dir, $paragraph_align);
933
	$bb = array($x-$xmarg,$y+$height-$ymarg,$x+$width,$y+$height-$ymarg,
934
		    $x+$width,$y-$ymarg,$x-$xmarg,$y-$ymarg);
935
	$this->SetTextAlign($h,$v);
936
	return $bb;
937
    }
938
 
939
    // Set text alignment
940
    function SetTextAlign($halign,$valign="bottom") {
941
	$this->text_halign=$halign;
942
	$this->text_valign=$valign;
943
    }
944
 
945
 
946
    function _StrokeBuiltinFont($x,$y,$txt,$dir=0,$paragraph_align="left",&$aBoundingBox,$aDebug=false) {
947
 
948
	if( is_numeric($dir) && $dir!=90 && $dir!=0)
949
	    JpGraphError::RaiseL(25091);//(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead.");
950
 
951
	$h=$this->GetTextHeight($txt);
952
	$fh=$this->GetFontHeight();
953
	$w=$this->GetTextWidth($txt);
954
 
955
	if( $this->text_halign=="right")
956
	    $x -= $dir==0 ? $w : $h;
957
	elseif( $this->text_halign=="center" ) {
958
	    // For center we subtract 1 pixel since this makes the middle
959
	    // be prefectly in the middle
960
	    $x -= $dir==0 ? $w/2-1 : $h/2;
961
	}
962
	if( $this->text_valign=="top" )
963
	    $y += $dir==0 ? $h : $w;
964
	elseif( $this->text_valign=="center" )
965
	    $y += $dir==0 ? $h/2 : $w/2;
966
 
967
	if( $dir==90 ) {
968
	    imagestringup($this->img,$this->font_family,$x,$y,$txt,$this->current_color);
969
	    $aBoundingBox = array(round($x),round($y),round($x),round($y-$w),round($x+$h),round($y-$w),round($x+$h),round($y));
970
            if( $aDebug ) {
971
		// Draw bounding box
972
		$this->PushColor('green');
973
		$this->Polygon($aBoundingBox,true);
974
		$this->PopColor();
975
	    }
976
	}
977
	else {
978
	    if( ereg("\n",$txt) ) {
979
		$tmp = split("\n",$txt);
980
		for($i=0; $i < count($tmp); ++$i) {
981
		    $w1 = $this->GetTextWidth($tmp[$i]);
982
		    if( $paragraph_align=="left" ) {
983
			imagestring($this->img,$this->font_family,$x,$y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
984
		    }
985
		    elseif( $paragraph_align=="right" ) {
986
			imagestring($this->img,$this->font_family,$x+($w-$w1),
987
				    $y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
988
		    }
989
		    else {
990
			imagestring($this->img,$this->font_family,$x+$w/2-$w1/2,
991
				    $y-$h+1+$i*$fh,$tmp[$i],$this->current_color);
992
		    }
993
		}
994
	    }
995
	    else {
996
		//Put the text
997
		imagestring($this->img,$this->font_family,$x,$y-$h+1,$txt,$this->current_color);
998
	    }
999
            if( $aDebug ) {
1000
		// Draw the bounding rectangle and the bounding box
1001
		$p1 = array(round($x),round($y),round($x),round($y-$h),round($x+$w),round($y-$h),round($x+$w),round($y));
1002
 
1003
		// Draw bounding box
1004
		$this->PushColor('green');
1005
		$this->Polygon($p1,true);
1006
		$this->PopColor();
1007
 
1008
            }
1009
	    $aBoundingBox=array(round($x),round($y),round($x),round($y-$h),round($x+$w),round($y-$h),round($x+$w),round($y));
1010
	}
1011
    }
1012
 
1013
    function AddTxtCR($aTxt) {
1014
	// If the user has just specified a '\n'
1015
	// instead of '\n\t' we have to add '\r' since
1016
	// the width will be too muchy otherwise since when
1017
	// we print we stroke the individually lines by hand.
1018
	$e = explode("\n",$aTxt);
1019
	$n = count($e);
1020
	for($i=0; $i<$n; ++$i) {
1021
	    $e[$i]=str_replace("\r","",$e[$i]);
1022
	}
1023
	return implode("\n\r",$e);
1024
    }
1025
 
1026
    function GetTTFBBox($aTxt,$aAngle=0) {
1027
	$bbox = @ImageTTFBBox($this->font_size,$aAngle,$this->font_file,$aTxt);
1028
	if( $bbox === false ) {
1029
	    JpGraphError::RaiseL(25092,$this->font_file);
1030
//("There is either a configuration problem with TrueType or a problem reading font file (".$this->font_file."). Make sure file exists and is in a readable place for the HTTP process. (If 'basedir' restriction is enabled in PHP then the font file must be located in the document root.). It might also be a wrongly installed FreeType library. Try uppgrading to at least FreeType 2.1.13 and recompile GD with the correct setup so it can find the new FT library.");
1031
	}
1032
	return $bbox;
1033
    }
1034
 
1035
    function GetBBoxTTF($aTxt,$aAngle=0) {
1036
	// Normalize the bounding box to become a minimum
1037
	// enscribing rectangle
1038
 
1039
	$aTxt = $this->AddTxtCR($aTxt);
1040
 
1041
	if( !is_readable($this->font_file) ) {
1042
	    JpGraphError::RaiseL(25093,$this->font_file);
1043
//('Can not read font file ('.$this->font_file.') in call to Image::GetBBoxTTF. Please make sure that you have set a font before calling this method and that the font is installed in the TTF directory.');
1044
	}
1045
	$bbox = $this->GetTTFBBox($aTxt,$aAngle);
1046
 
1047
	if( $aAngle==0 )
1048
	    return $bbox;
1049
	if( $aAngle >= 0 ) {
1050
	    if(  $aAngle <= 90 ) { //<=0
1051
		$bbox = array($bbox[6],$bbox[1],$bbox[2],$bbox[1],
1052
			      $bbox[2],$bbox[5],$bbox[6],$bbox[5]);
1053
	    }
1054
	    elseif(  $aAngle <= 180 ) { //<= 2
1055
		$bbox = array($bbox[4],$bbox[7],$bbox[0],$bbox[7],
1056
			      $bbox[0],$bbox[3],$bbox[4],$bbox[3]);
1057
	    }
1058
	    elseif(  $aAngle <= 270 )  { //<= 3
1059
		$bbox = array($bbox[2],$bbox[5],$bbox[6],$bbox[5],
1060
			      $bbox[6],$bbox[1],$bbox[2],$bbox[1]);
1061
	    }
1062
	    else {
1063
		$bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
1064
			      $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
1065
	    }
1066
	}
1067
	elseif(  $aAngle < 0 ) {
1068
	    if( $aAngle <= -270 ) { // <= -3
1069
		$bbox = array($bbox[6],$bbox[1],$bbox[2],$bbox[1],
1070
			      $bbox[2],$bbox[5],$bbox[6],$bbox[5]);
1071
	    }
1072
	    elseif( $aAngle <= -180 ) { // <= -2
1073
		$bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
1074
			      $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
1075
	    }
1076
	    elseif( $aAngle <= -90 ) { // <= -1
1077
		$bbox = array($bbox[2],$bbox[5],$bbox[6],$bbox[5],
1078
			      $bbox[6],$bbox[1],$bbox[2],$bbox[1]);
1079
	    }
1080
	    else {
1081
		$bbox = array($bbox[0],$bbox[3],$bbox[4],$bbox[3],
1082
			      $bbox[4],$bbox[7],$bbox[0],$bbox[7]);
1083
	    }
1084
	}
1085
	return $bbox;
1086
    }
1087
 
1088
    function GetBBoxHeight($aTxt,$aAngle=0) {
1089
	$box = $this->GetBBoxTTF($aTxt,$aAngle);
1090
	return $box[1]-$box[7]+1;
1091
    }
1092
 
1093
    function GetBBoxWidth($aTxt,$aAngle=0) {
1094
	$box = $this->GetBBoxTTF($aTxt,$aAngle);
1095
	return $box[2]-$box[0]+1;
1096
    }
1097
 
1098
    function _StrokeTTF($x,$y,$txt,$dir=0,$paragraph_align="left",&$aBoundingBox,$debug=false) {
1099
 
1100
	// Setupo default inter line margin for paragraphs to
1101
	// 25% of the font height.
1102
	$ConstLineSpacing = 0.25 ;
1103
 
1104
	// Remember the anchor point before adjustment
1105
	if( $debug ) {
1106
	    $ox=$x;
1107
	    $oy=$y;
1108
	}
1109
 
1110
	if( !ereg("\n",$txt) || ($dir>0 && ereg("\n",$txt)) ) {
1111
	    // Format a single line
1112
 
1113
	    $txt = $this->AddTxtCR($txt);
1114
 
1115
	    $bbox=$this->GetBBoxTTF($txt,$dir);
1116
 
1117
	    // Align x,y ot lower left corner of bbox
1118
	    $x -= $bbox[0];
1119
	    $y -= $bbox[1];
1120
 
1121
	    // Note to self: "topanchor" is deprecated after we changed the
1122
	    // bopunding box stuff.
1123
	    if( $this->text_halign=="right" || $this->text_halign=="topanchor" )
1124
		$x -= $bbox[2]-$bbox[0];
1125
	    elseif( $this->text_halign=="center" ) $x -= ($bbox[2]-$bbox[0])/2;
1126
 
1127
	    if( $this->text_valign=="top" ) $y += abs($bbox[5])+$bbox[1];
1128
	    elseif( $this->text_valign=="center" ) $y -= ($bbox[5]-$bbox[1])/2;
1129
 
1130
	    ImageTTFText ($this->img, $this->font_size, $dir, $x, $y,
1131
			  $this->current_color,$this->font_file,$txt);
1132
 
1133
	    // Calculate and return the co-ordinates for the bounding box
1134
	    $box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$txt);
1135
	    $p1 = array();
1136
 
1137
 
1138
	    for($i=0; $i < 4; ++$i) {
1139
		$p1[] = round($box[$i*2]+$x);
1140
		$p1[] = round($box[$i*2+1]+$y);
1141
	    }
1142
	    $aBoundingBox = $p1;
1143
 
1144
	    // Debugging code to highlight the bonding box and bounding rectangle
1145
	    // For text at 0 degrees the bounding box and bounding rectangle are the
1146
	    // same
1147
            if( $debug ) {
1148
		// Draw the bounding rectangle and the bounding box
1149
		$box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$txt);
1150
		$p = array();
1151
		$p1 = array();
1152
		for($i=0; $i < 4; ++$i) {
1153
		    $p[] = $bbox[$i*2]+$x;
1154
		    $p[] = $bbox[$i*2+1]+$y;
1155
		    $p1[] = $box[$i*2]+$x;
1156
		    $p1[] = $box[$i*2+1]+$y;
1157
		}
1158
 
1159
		// Draw bounding box
1160
		$this->PushColor('green');
1161
		$this->Polygon($p1,true);
1162
		$this->PopColor();
1163
 
1164
		// Draw bounding rectangle
1165
		$this->PushColor('darkgreen');
1166
		$this->Polygon($p,true);
1167
		$this->PopColor();
1168
 
1169
		// Draw a cross at the anchor point
1170
		$this->PushColor('red');
1171
		$this->Line($ox-15,$oy,$ox+15,$oy);
1172
		$this->Line($ox,$oy-15,$ox,$oy+15);
1173
		$this->PopColor();
1174
            }
1175
	}
1176
	else {
1177
	    // Format a text paragraph
1178
	    $fh=$this->GetFontHeight();
1179
 
1180
	    // Line margin is 25% of font height
1181
	    $linemargin=round($fh*$ConstLineSpacing);
1182
	    $fh += $linemargin;
1183
	    $w=$this->GetTextWidth($txt);
1184
 
1185
	    $y -= $linemargin/2;
1186
	    $tmp = split("\n",$txt);
1187
	    $nl = count($tmp);
1188
	    $h = $nl * $fh;
1189
 
1190
	    if( $this->text_halign=="right")
1191
		$x -= $dir==0 ? $w : $h;
1192
	    elseif( $this->text_halign=="center" ) {
1193
		$x -= $dir==0 ? $w/2 : $h/2;
1194
	    }
1195
 
1196
	    if( $this->text_valign=="top" )
1197
		$y +=	$dir==0 ? $h : $w;
1198
	    elseif( $this->text_valign=="center" )
1199
		$y +=	$dir==0 ? $h/2 : $w/2;
1200
 
1201
	    // Here comes a tricky bit.
1202
	    // Since we have to give the position for the string at the
1203
	    // baseline this means thaht text will move slightly up
1204
	    // and down depending on any of it's character descend below
1205
	    // the baseline, for example a 'g'. To adjust the Y-position
1206
	    // we therefore adjust the text with the baseline Y-offset
1207
	    // as used for the current font and size. This will keep the
1208
	    // baseline at a fixed positoned disregarding the actual
1209
	    // characters in the string.
1210
	    $standardbox = $this->GetTTFBBox('Gg',$dir);
1211
	    $yadj = $standardbox[1];
1212
	    $xadj = $standardbox[0];
1213
	    $aBoundingBox = array();
1214
	    for($i=0; $i < $nl; ++$i) {
1215
		$wl = $this->GetTextWidth($tmp[$i]);
1216
		$bbox = $this->GetTTFBBox($tmp[$i],$dir);
1217
		if( $paragraph_align=="left" ) {
1218
		    $xl = $x;
1219
		}
1220
		elseif( $paragraph_align=="right" ) {
1221
		    $xl = $x + ($w-$wl);
1222
		}
1223
		else {
1224
		    // Center
1225
		    $xl = $x + $w/2 - $wl/2 ;
1226
		}
1227
 
1228
		$xl -= $bbox[0];
1229
		$yl = $y - $yadj;
1230
		$xl = $xl - $xadj;
1231
		ImageTTFText ($this->img, $this->font_size, $dir,
1232
			      $xl, $yl-($h-$fh)+$fh*$i,
1233
			      $this->current_color,$this->font_file,$tmp[$i]);
1234
 
1235
		if( $debug  ) {
1236
		    // Draw the bounding rectangle around each line
1237
		    $box=@ImageTTFBBox($this->font_size,$dir,$this->font_file,$tmp[$i]);
1238
		    $p = array();
1239
		    for($j=0; $j < 4; ++$j) {
1240
			$p[] = $bbox[$j*2]+$xl;
1241
			$p[] = $bbox[$j*2+1]+$yl-($h-$fh)+$fh*$i;
1242
		    }
1243
 
1244
		    // Draw bounding rectangle
1245
		    $this->PushColor('darkgreen');
1246
		    $this->Polygon($p,true);
1247
		    $this->PopColor();
1248
		}
1249
	    }
1250
 
1251
	    // Get the bounding box
1252
	    $bbox = $this->GetBBoxTTF($txt,$dir);
1253
	    for($j=0; $j < 4; ++$j) {
1254
		$bbox[$j*2]+= round($x);
1255
		$bbox[$j*2+1]+= round($y - ($h-$fh) - $yadj);
1256
	    }
1257
	    $aBoundingBox = $bbox;
1258
 
1259
	    if( $debug ) {
1260
		// Draw a cross at the anchor point
1261
		$this->PushColor('red');
1262
		$this->Line($ox-25,$oy,$ox+25,$oy);
1263
		$this->Line($ox,$oy-25,$ox,$oy+25);
1264
		$this->PopColor();
1265
	    }
1266
 
1267
	}
1268
    }
1269
 
1270
    function StrokeText($x,$y,$txt,$dir=0,$paragraph_align="left",$debug=false) {
1271
 
1272
	$x = round($x);
1273
	$y = round($y);
1274
 
1275
	// Do special language encoding
1276
	$txt = $this->langconv->Convert($txt,$this->font_family);
1277
 
1278
	if( !is_numeric($dir) )
1279
	    JpGraphError::RaiseL(25094);//(" Direction for text most be given as an angle between 0 and 90.");
1280
 
1281
	if( $this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2+1) {
1282
	    $this->_StrokeBuiltinFont($x,$y,$txt,$dir,$paragraph_align,$boundingbox,$debug);
1283
	}
1284
	elseif($this->font_family >= _FF_FIRST && $this->font_family <= _FF_LAST)  {
1285
	    $this->_StrokeTTF($x,$y,$txt,$dir,$paragraph_align,$boundingbox,$debug);
1286
	}
1287
	else
1288
	    JpGraphError::RaiseL(25095);//(" Unknown font font family specification. ");
1289
	return $boundingbox;
1290
    }
1291
 
1292
    function SetMargin($lm,$rm,$tm,$bm) {
1293
	$this->left_margin=$lm;
1294
	$this->right_margin=$rm;
1295
	$this->top_margin=$tm;
1296
	$this->bottom_margin=$bm;
1297
	$this->plotwidth=$this->width - $this->left_margin-$this->right_margin ;
1298
	$this->plotheight=$this->height - $this->top_margin-$this->bottom_margin ;
1299
	if( $this->width  > 0 && $this->height > 0 ) {
1300
	    if( $this->plotwidth < 0  || $this->plotheight < 0 )
1301
		JpGraphError::raise("Too small plot area. ($lm,$rm,$tm,$bm : $this->plotwidth x $this->plotheight). With the given image size and margins there is to little space left for the plot. Increase the plot size or reduce the margins.");
1302
	}
1303
    }
1304
 
1305
    function SetTransparent($color) {
1306
	imagecolortransparent ($this->img,$this->rgb->allocate($color));
1307
    }
1308
 
1309
    function SetColor($color,$aAlpha=0) {
1310
	$this->current_color_name = $color;
1311
	$this->current_color=$this->rgb->allocate($color,$aAlpha);
1312
	if( $this->current_color == -1 ) {
1313
	    JpGraphError::RaiseL(25096);
1314
//("Can't allocate any more colors.");
1315
	}
1316
	return $this->current_color;
1317
    }
1318
 
1319
    function PushColor($color) {
1320
	if( $color != "" ) {
1321
	    $this->colorstack[$this->colorstackidx]=$this->current_color_name;
1322
	    $this->colorstack[$this->colorstackidx+1]=$this->current_color;
1323
	    $this->colorstackidx+=2;
1324
	    $this->SetColor($color);
1325
	}
1326
	else {
1327
	    JpGraphError::RaiseL(25097);//("Color specified as empty string in PushColor().");
1328
	}
1329
    }
1330
 
1331
    function PopColor() {
1332
	if($this->colorstackidx<1)
1333
	    JpGraphError::RaiseL(25098);//(" Negative Color stack index. Unmatched call to PopColor()");
1334
	$this->current_color=$this->colorstack[--$this->colorstackidx];
1335
	$this->current_color_name=$this->colorstack[--$this->colorstackidx];
1336
    }
1337
 
1338
 
1339
    function SetLineWeight($weight) {
1340
	$this->line_weight = $weight;
1341
    }
1342
 
1343
    function SetStartPoint($x,$y) {
1344
	$this->lastx=round($x);
1345
	$this->lasty=round($y);
1346
    }
1347
 
1348
    function Arc($cx,$cy,$w,$h,$s,$e) {
1349
	// GD Arc doesn't like negative angles
1350
	while( $s < 0) $s += 360;
1351
	while( $e < 0) $e += 360;
1352
 
1353
	imagearc($this->img,round($cx),round($cy),round($w),round($h),
1354
		 $s,$e,$this->current_color);
1355
    }
1356
 
1357
    function FilledArc($xc,$yc,$w,$h,$s,$e,$style="") {
1358
 
1359
	while( $s < 0 ) $s += 360;
1360
	while( $e < 0 ) $e += 360;
1361
	if( $style=="" )
1362
	    $style=IMG_ARC_PIE;
1363
	imagefilledarc($this->img,round($xc),round($yc),round($w),round($h),
1364
		       round($s),round($e),$this->current_color,$style);
1365
    }
1366
 
1367
    function FilledCakeSlice($cx,$cy,$w,$h,$s,$e) {
1368
	$this->CakeSlice($cx,$cy,$w,$h,$s,$e,$this->current_color_name);
1369
    }
1370
 
1371
    function CakeSlice($xc,$yc,$w,$h,$s,$e,$fillcolor="",$arccolor="") {
1372
	$s = round($s); $e = round($e);
1373
	$w = round($w); $h = round($h);
1374
	$xc = round($xc); $yc = round($yc);
1375
	$this->PushColor($fillcolor);
1376
	$this->FilledArc($xc,$yc,2*$w,2*$h,$s,$e);
1377
	$this->PopColor();
1378
	if( $arccolor != "" ) {
1379
	    $this->PushColor($arccolor);
1380
	    // We add 2 pixels to make the Arc() better aligned with the filled arc.
1381
	    imagefilledarc($this->img,$xc,$yc,2*$w,2*$h,$s,$e,$this->current_color,IMG_ARC_NOFILL | IMG_ARC_EDGED ) ;
1382
	    $this->PopColor();
1383
	}
1384
    }
1385
 
1386
    function Ellipse($xc,$yc,$w,$h) {
1387
	$this->Arc($xc,$yc,$w,$h,0,360);
1388
    }
1389
 
1390
    // Breseham circle gives visually better result then using GD
1391
    // built in arc(). It takes some more time but gives better
1392
    // accuracy.
1393
    function BresenhamCircle($xc,$yc,$r) {
1394
	$d = 3-2*$r;
1395
	$x = 0;
1396
	$y = $r;
1397
	while($x<=$y) {
1398
	    $this->Point($xc+$x,$yc+$y);
1399
	    $this->Point($xc+$x,$yc-$y);
1400
	    $this->Point($xc-$x,$yc+$y);
1401
	    $this->Point($xc-$x,$yc-$y);
1402
 
1403
	    $this->Point($xc+$y,$yc+$x);
1404
	    $this->Point($xc+$y,$yc-$x);
1405
	    $this->Point($xc-$y,$yc+$x);
1406
	    $this->Point($xc-$y,$yc-$x);
1407
 
1408
	    if( $d<0 ) $d += 4*$x+6;
1409
	    else {
1410
		$d += 4*($x-$y)+10;
1411
		--$y;
1412
	    }
1413
	    ++$x;
1414
	}
1415
    }
1416
 
1417
    function Circle($xc,$yc,$r) {
1418
	if( USE_BRESENHAM )
1419
	    $this->BresenhamCircle($xc,$yc,$r);
1420
	else {
1421
 
1422
	    /*
1423
            // Some experimental code snippet to see if we can get a decent
1424
	    // result doing a trig-circle
1425
	    // Create an approximated circle with 0.05 rad resolution
1426
	    $end = 2*M_PI;
1427
	    $l = $r/10;
1428
	    if( $l < 3 ) $l=3;
1429
	    $step_size = 2*M_PI/(2*$r*M_PI/$l);
1430
	    $pts = array();
1431
	    $pts[] = $r + $xc;
1432
	    $pts[] = $yc;
1433
	    for( $a=$step_size; $a <= $end; $a += $step_size ) {
1434
		$pts[] = round($xc + $r*cos($a));
1435
		$pts[] = round($yc - $r*sin($a));
1436
	    }
1437
	    imagepolygon($this->img,$pts,count($pts)/2,$this->current_color);
1438
	    */
1439
 
1440
	    $this->Arc($xc,$yc,$r*2,$r*2,0,360);
1441
 
1442
	    // For some reason imageellipse() isn't in GD 2.0.1, PHP 4.1.1
1443
	    //imageellipse($this->img,$xc,$yc,$r,$r,$this->current_color);
1444
	}
1445
    }
1446
 
1447
    function FilledCircle($xc,$yc,$r) {
1448
	imagefilledellipse($this->img,round($xc),round($yc),2*$r,2*$r,$this->current_color);
1449
    }
1450
 
1451
    // Linear Color InterPolation
1452
    function lip($f,$t,$p) {
1453
	$p = round($p,1);
1454
	$r = $f[0] + ($t[0]-$f[0])*$p;
1455
	$g = $f[1] + ($t[1]-$f[1])*$p;
1456
	$b = $f[2] + ($t[2]-$f[2])*$p;
1457
	return array($r,$g,$b);
1458
    }
1459
 
1460
    // Anti-aliased line.
1461
    // Note that this is roughly 8 times slower then a normal line!
1462
    function WuLine($x1,$y1,$x2,$y2) {
1463
	// Get foreground line color
1464
	$lc = imagecolorsforindex($this->img,$this->current_color);
1465
	$lc = array($lc["red"],$lc["green"],$lc["blue"]);
1466
 
1467
	$dx = $x2-$x1;
1468
	$dy = $y2-$y1;
1469
 
1470
	if( abs($dx) > abs($dy) ) {
1471
	    if( $dx<0 ) {
1472
		$dx = -$dx;$dy = -$dy;
1473
		$tmp=$x2;$x2=$x1;$x1=$tmp;
1474
		$tmp=$y2;$y2=$y1;$y1=$tmp;
1475
	    }
1476
	    $x=$x1<<16; $y=$y1<<16;
1477
	    $yinc = ($dy*65535)/$dx;
1478
	    while( ($x >> 16) < $x2 ) {
1479
 
1480
		$bc = @imagecolorsforindex($this->img,imagecolorat($this->img,$x>>16,$y>>16));
1481
		if( $bc <= 0 ) {
1482
		    JpGraphError::RaiseL(25100);//('Problem with color palette and your GD setup. Please disable anti-aliasing or use GD2 with true-color. If you have GD2 library installed please make sure that you have set the USE_GD2 constant to true and that truecolor is enabled.');
1483
		}
1484
		$bc=array($bc["red"],$bc["green"],$bc["blue"]);
1485
 
1486
		$this->SetColor($this->lip($lc,$bc,($y & 0xFFFF)/65535));
1487
		imagesetpixel($this->img,$x>>16,$y>>16,$this->current_color);
1488
		$this->SetColor($this->lip($lc,$bc,(~$y & 0xFFFF)/65535));
1489
		imagesetpixel($this->img,$x>>16,($y>>16)+1,$this->current_color);
1490
		$x += 65536; $y += $yinc;
1491
	    }
1492
	}
1493
	else {
1494
	    if( $dy<0 ) {
1495
		$dx = -$dx;$dy = -$dy;
1496
		$tmp=$x2;$x2=$x1;$x1=$tmp;
1497
		$tmp=$y2;$y2=$y1;$y1=$tmp;
1498
	    }
1499
	    $x=$x1<<16; $y=$y1<<16;
1500
	    $xinc = ($dx*65535)/$dy;
1501
	    while( ($y >> 16) < $y2 ) {
1502
 
1503
		$bc = @imagecolorsforindex($this->img,imagecolorat($this->img,$x>>16,$y>>16));
1504
		if( $bc <= 0 ) {
1505
		    JpGraphError::RaiseL(25100);//('Problem with color palette and your GD setup. Please disable anti-aliasing or use GD2 with true-color. If you have GD2 library installed please make sure that you have set the USE_GD2 constant to true and truecolor is enabled.');
1506
 
1507
		}
1508
 
1509
		$bc=array($bc["red"],$bc["green"],$bc["blue"]);
1510
 
1511
		$this->SetColor($this->lip($lc,$bc,($x & 0xFFFF)/65535));
1512
		imagesetpixel($this->img,$x>>16,$y>>16,$this->current_color);
1513
		$this->SetColor($this->lip($lc,$bc,(~$x & 0xFFFF)/65535));
1514
		imagesetpixel($this->img,($x>>16)+1,$y>>16,$this->current_color);
1515
		$y += 65536; $x += $xinc;
1516
	    }
1517
	}
1518
	$this->SetColor($lc);
1519
	imagesetpixel($this->img,$x2,$y2,$this->current_color);
1520
	imagesetpixel($this->img,$x1,$y1,$this->current_color);
1521
    }
1522
 
1523
    // Set line style dashed, dotted etc
1524
    function SetLineStyle($s) {
1525
	if( is_numeric($s) ) {
1526
	    if( $s<1 || $s>4 )
1527
		JpGraphError::RaiseL(25101,$s);//(" Illegal numeric argument to SetLineStyle(): ($s)");
1528
	}
1529
	elseif( is_string($s) ) {
1530
	    if( $s == "solid" ) $s=1;
1531
	    elseif( $s == "dotted" ) $s=2;
1532
	    elseif( $s == "dashed" ) $s=3;
1533
	    elseif( $s == "longdashed" ) $s=4;
1534
	    else JpGraphError::RaiseL(25102,$s);//(" Illegal string argument to SetLineStyle(): $s");
1535
	}
1536
	else JpGraphError::RaiseL(25103,$s);//(" Illegal argument to SetLineStyle $s");
1537
	$this->line_style=$s;
1538
    }
1539
 
1540
    // Same as Line but take the line_style into account
1541
    function StyleLine($x1,$y1,$x2,$y2) {
1542
	switch( $this->line_style ) {
1543
	    case 1:// Solid
1544
		$this->Line($x1,$y1,$x2,$y2);
1545
		break;
1546
	    case 2: // Dotted
1547
		$this->DashedLine($x1,$y1,$x2,$y2,1,6);
1548
		break;
1549
	    case 3: // Dashed
1550
		$this->DashedLine($x1,$y1,$x2,$y2,2,4);
1551
		break;
1552
	    case 4: // Longdashes
1553
		$this->DashedLine($x1,$y1,$x2,$y2,8,6);
1554
		break;
1555
	    default:
1556
		JpGraphError::RaiseL(25104,$this->line_style);//(" Unknown line style: $this->line_style ");
1557
		break;
1558
	}
1559
    }
1560
 
1561
    function Line($x1,$y1,$x2,$y2) {
1562
 
1563
	$x1 = round($x1);
1564
	$x2 = round($x2);
1565
	$y1 = round($y1);
1566
	$y2 = round($y2);
1567
 
1568
	if( $this->line_weight==0 ) return;
1569
	if( $this->use_anti_aliasing ) {
1570
	    $dx = $x2-$x1;
1571
	    $dy = $y2-$y1;
1572
	    // Vertical, Horizontal or 45 lines don't need anti-aliasing
1573
	    if( $dx!=0 && $dy!=0 && $dx!=$dy ) {
1574
		$this->WuLine($x1,$y1,$x2,$y2);
1575
		return;
1576
	    }
1577
	}
1578
	if( $this->line_weight==1 ) {
1579
	    imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
1580
	}
1581
	elseif( $x1==$x2 ) {		// Special case for vertical lines
1582
	    imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
1583
	    $w1=floor($this->line_weight/2);
1584
	    $w2=floor(($this->line_weight-1)/2);
1585
	    for($i=1; $i<=$w1; ++$i)
1586
		imageline($this->img,$x1+$i,$y1,$x2+$i,$y2,$this->current_color);
1587
	    for($i=1; $i<=$w2; ++$i)
1588
		imageline($this->img,$x1-$i,$y1,$x2-$i,$y2,$this->current_color);
1589
	}
1590
	elseif( $y1==$y2 ) {		// Special case for horizontal lines
1591
	    imageline($this->img,$x1,$y1,$x2,$y2,$this->current_color);
1592
	    $w1=floor($this->line_weight/2);
1593
	    $w2=floor(($this->line_weight-1)/2);
1594
	    for($i=1; $i<=$w1; ++$i)
1595
		imageline($this->img,$x1,$y1+$i,$x2,$y2+$i,$this->current_color);
1596
	    for($i=1; $i<=$w2; ++$i)
1597
		imageline($this->img,$x1,$y1-$i,$x2,$y2-$i,$this->current_color);
1598
	}
1599
	else {	// General case with a line at an angle
1600
	    $a = atan2($y1-$y2,$x2-$x1);
1601
	    // Now establish some offsets from the center. This gets a little
1602
	    // bit involved since we are dealing with integer functions and we
1603
	    // want the apperance to be as smooth as possible and never be thicker
1604
	    // then the specified width.
1605
 
1606
	    // We do the trig stuff to make sure that the endpoints of the line
1607
	    // are perpendicular to the line itself.
1608
	    $dx=(sin($a)*$this->line_weight/2);
1609
	    $dy=(cos($a)*$this->line_weight/2);
1610
 
1611
	    $pnts = array(round($x2+$dx),round($y2+$dy),round($x2-$dx),round($y2-$dy),
1612
			  round($x1-$dx),round($y1-$dy),round($x1+$dx),round($y1+$dy));
1613
	    imagefilledpolygon($this->img,$pnts,count($pnts)/2,$this->current_color);
1614
	}
1615
	$this->lastx=$x2; $this->lasty=$y2;
1616
    }
1617
 
1618
    function Polygon($p,$closed=FALSE,$fast=FALSE) {
1619
	if( $this->line_weight==0 ) return;
1620
	$n=count($p);
1621
	$oldx = $p[0];
1622
	$oldy = $p[1];
1623
	if( $fast ) {
1624
	    for( $i=2; $i < $n; $i+=2 ) {
1625
		imageline($this->img,$oldx,$oldy,$p[$i],$p[$i+1],$this->current_color);
1626
		$oldx = $p[$i];
1627
		$oldy = $p[$i+1];
1628
	    }
1629
	    if( $closed ) {
1630
		imageline($this->img,$p[$n*2-2],$p[$n*2-1],$p[0],$p[1],$this->current_color);
1631
	    }
1632
	}
1633
	else {
1634
	    for( $i=2; $i < $n; $i+=2 ) {
1635
		$this->StyleLine($oldx,$oldy,$p[$i],$p[$i+1]);
1636
		$oldx = $p[$i];
1637
		$oldy = $p[$i+1];
1638
	    }
1639
	    if( $closed )
1640
		$this->Line($oldx,$oldy,$p[0],$p[1]);
1641
	}
1642
    }
1643
 
1644
    function FilledPolygon($pts) {
1645
	$n=count($pts);
1646
	if( $n == 0 ) {
1647
	    JpGraphError::RaiseL(25105);//('NULL data specified for a filled polygon. Check that your data is not NULL.');
1648
	}
1649
	for($i=0; $i < $n; ++$i)
1650
	    $pts[$i] = round($pts[$i]);
1651
	imagefilledpolygon($this->img,$pts,count($pts)/2,$this->current_color);
1652
    }
1653
 
1654
    function Rectangle($xl,$yu,$xr,$yl) {
1655
	$this->Polygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl,$xl,$yu));
1656
    }
1657
 
1658
    function FilledRectangle($xl,$yu,$xr,$yl) {
1659
	$this->FilledPolygon(array($xl,$yu,$xr,$yu,$xr,$yl,$xl,$yl));
1660
    }
1661
 
1662
    function FilledRectangle2($xl,$yu,$xr,$yl,$color1,$color2,$style=1) {
1663
	// Fill a rectangle with lines of two colors
1664
	if( $style===1 ) {
1665
	    // Horizontal stripe
1666
	    if( $yl < $yu ) {
1667
		$t = $yl; $yl=$yu; $yu=$t;
1668
	    }
1669
	    for( $y=$yu; $y <= $yl; ++$y) {
1670
		$this->SetColor($color1);
1671
		$this->Line($xl,$y,$xr,$y);
1672
		++$y;
1673
		$this->SetColor($color2);
1674
		$this->Line($xl,$y,$xr,$y);
1675
	    }
1676
	}
1677
	else {
1678
	    if( $xl < $xl ) {
1679
		$t = $xl; $xl=$xr; $xr=$t;
1680
	    }
1681
	    for( $x=$xl; $x <= $xr; ++$x) {
1682
		$this->SetColor($color1);
1683
		$this->Line($x,$yu,$x,$yl);
1684
		++$x;
1685
		$this->SetColor($color2);
1686
		$this->Line($x,$yu,$x,$yl);
1687
	    }
1688
	}
1689
    }
1690
 
1691
    function ShadowRectangle($xl,$yu,$xr,$yl,$fcolor=false,$shadow_width=3,$shadow_color=array(102,102,102)) {
1692
	// This is complicated by the fact that we must also handle the case where
1693
        // the reactangle has no fill color
1694
	$this->PushColor($shadow_color);
1695
	$this->FilledRectangle($xr-$shadow_width,$yu+$shadow_width,$xr,$yl-$shadow_width-1);
1696
	$this->FilledRectangle($xl+$shadow_width,$yl-$shadow_width,$xr,$yl);
1697
	//$this->FilledRectangle($xl+$shadow_width,$yu+$shadow_width,$xr,$yl);
1698
	$this->PopColor();
1699
	if( $fcolor==false )
1700
	    $this->Rectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
1701
	else {
1702
	    $this->PushColor($fcolor);
1703
	    $this->FilledRectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
1704
	    $this->PopColor();
1705
	    $this->Rectangle($xl,$yu,$xr-$shadow_width-1,$yl-$shadow_width-1);
1706
	}
1707
    }
1708
 
1709
    function FilledRoundedRectangle($xt,$yt,$xr,$yl,$r=5) {
1710
	if( $r==0 ) {
1711
	    $this->FilledRectangle($xt,$yt,$xr,$yl);
1712
	    return;
1713
	}
1714
 
1715
	// To avoid overlapping fillings (which will look strange
1716
	// when alphablending is enabled) we have no choice but
1717
	// to fill the five distinct areas one by one.
1718
 
1719
	// Center square
1720
	$this->FilledRectangle($xt+$r,$yt+$r,$xr-$r,$yl-$r);
1721
	// Top band
1722
	$this->FilledRectangle($xt+$r,$yt,$xr-$r,$yt+$r-1);
1723
	// Bottom band
1724
	$this->FilledRectangle($xt+$r,$yl-$r+1,$xr-$r,$yl);
1725
	// Left band
1726
	$this->FilledRectangle($xt,$yt+$r+1,$xt+$r-1,$yl-$r);
1727
	// Right band
1728
	$this->FilledRectangle($xr-$r+1,$yt+$r,$xr,$yl-$r);
1729
 
1730
	// Topleft & Topright arc
1731
	$this->FilledArc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
1732
	$this->FilledArc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
1733
 
1734
	// Bottomleft & Bottom right arc
1735
	$this->FilledArc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
1736
	$this->FilledArc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
1737
 
1738
    }
1739
 
1740
    function RoundedRectangle($xt,$yt,$xr,$yl,$r=5) {
1741
 
1742
	if( $r==0 ) {
1743
	    $this->Rectangle($xt,$yt,$xr,$yl);
1744
	    return;
1745
	}
1746
 
1747
	// Top & Bottom line
1748
	$this->Line($xt+$r,$yt,$xr-$r,$yt);
1749
	$this->Line($xt+$r,$yl,$xr-$r,$yl);
1750
 
1751
	// Left & Right line
1752
	$this->Line($xt,$yt+$r,$xt,$yl-$r);
1753
	$this->Line($xr,$yt+$r,$xr,$yl-$r);
1754
 
1755
	// Topleft & Topright arc
1756
	$this->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
1757
	$this->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
1758
 
1759
	// Bottomleft & Bottomright arc
1760
	$this->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
1761
	$this->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
1762
    }
1763
 
1764
    function FilledBevel($x1,$y1,$x2,$y2,$depth=2,$color1='white@0.4',$color2='darkgray@0.4') {
1765
	$this->FilledRectangle($x1,$y1,$x2,$y2);
1766
	$this->Bevel($x1,$y1,$x2,$y2,$depth,$color1,$color2);
1767
    }
1768
 
1769
    function Bevel($x1,$y1,$x2,$y2,$depth=2,$color1='white@0.4',$color2='black@0.5') {
1770
	$this->PushColor($color1);
1771
	for( $i=0; $i < $depth; ++$i ) {
1772
	    $this->Line($x1+$i,$y1+$i,$x1+$i,$y2-$i);
1773
	    $this->Line($x1+$i,$y1+$i,$x2-$i,$y1+$i);
1774
	}
1775
	$this->PopColor();
1776
 
1777
	$this->PushColor($color2);
1778
	for( $i=0; $i < $depth; ++$i ) {
1779
	    $this->Line($x1+$i,$y2-$i,$x2-$i,$y2-$i);
1780
	    $this->Line($x2-$i,$y1+$i,$x2-$i,$y2-$i-1);
1781
	}
1782
	$this->PopColor();
1783
    }
1784
 
1785
    function StyleLineTo($x,$y) {
1786
	$this->StyleLine($this->lastx,$this->lasty,$x,$y);
1787
	$this->lastx=$x;
1788
	$this->lasty=$y;
1789
    }
1790
 
1791
    function LineTo($x,$y) {
1792
	$this->Line($this->lastx,$this->lasty,$x,$y);
1793
	$this->lastx=$x;
1794
	$this->lasty=$y;
1795
    }
1796
 
1797
    function Point($x,$y) {
1798
	imagesetpixel($this->img,round($x),round($y),$this->current_color);
1799
    }
1800
 
1801
    function Fill($x,$y) {
1802
	imagefill($this->img,round($x),round($y),$this->current_color);
1803
    }
1804
 
1805
    function FillToBorder($x,$y,$aBordColor) {
1806
	$bc = $this->rgb->allocate($aBordColor);
1807
	if( $bc == -1 ) {
1808
	    JpGraphError::RaiseL(25106);//('Image::FillToBorder : Can not allocate more colors');
1809
	}
1810
	imagefilltoborder($this->img,round($x),round($y),$bc,$this->current_color);
1811
    }
1812
 
1813
    function DashedLine($x1,$y1,$x2,$y2,$dash_length=1,$dash_space=4) {
1814
 
1815
	$x1 = round($x1);
1816
	$x2 = round($x2);
1817
	$y1 = round($y1);
1818
	$y2 = round($y2);
1819
 
1820
	// Code based on, but not identical to, work by Ariel Garza and James Pine
1821
	$line_length = ceil (sqrt(pow(($x2 - $x1),2) + pow(($y2 - $y1),2)) );
1822
	$dx = ($line_length) ? ($x2 - $x1) / $line_length : 0;
1823
	$dy = ($line_length) ? ($y2 - $y1) / $line_length : 0;
1824
	$lastx = $x1; $lasty = $y1;
1825
	$xmax = max($x1,$x2);
1826
	$xmin = min($x1,$x2);
1827
	$ymax = max($y1,$y2);
1828
	$ymin = min($y1,$y2);
1829
	for ($i = 0; $i < $line_length; $i += ($dash_length + $dash_space)) {
1830
	    $x = ($dash_length * $dx) + $lastx;
1831
	    $y = ($dash_length * $dy) + $lasty;
1832
 
1833
	    // The last section might overshoot so we must take a computational hit
1834
	    // and check this.
1835
	    if( $x>$xmax ) $x=$xmax;
1836
	    if( $y>$ymax ) $y=$ymax;
1837
 
1838
	    if( $x<$xmin ) $x=$xmin;
1839
	    if( $y<$ymin ) $y=$ymin;
1840
 
1841
	    $this->Line($lastx,$lasty,$x,$y);
1842
	    $lastx = $x + ($dash_space * $dx);
1843
	    $lasty = $y + ($dash_space * $dy);
1844
	}
1845
    }
1846
 
1847
    function SetExpired($aFlg=true) {
1848
	$this->expired = $aFlg;
1849
    }
1850
 
1851
    // Generate image header
1852
    function Headers() {
1853
 
1854
	// In case we are running from the command line with the client version of
1855
	// PHP we can't send any headers.
1856
	$sapi = php_sapi_name();
1857
	if( $sapi == 'cli' )
1858
	    return;
1859
 
1860
	if( headers_sent($file,$lineno) ) {
1861
	    $file=basename($file);
1862
	    $t = new ErrMsgText();
1863
	    $msg = $t->Get(10,$file,$lineno);
1864
	    die($msg);
1865
	}
1866
 
1867
	if ($this->expired) {
1868
	    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
1869
	    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
1870
	    header("Cache-Control: no-cache, must-revalidate");
1871
	    header("Pragma: no-cache");
1872
	}
1873
	header("Content-type: image/$this->img_format");
1874
    }
1875
 
1876
    // Adjust image quality for formats that allow this
1877
    function SetQuality($q) {
1878
	$this->quality = $q;
1879
    }
1880
 
1881
    // Stream image to browser or to file
1882
    function Stream($aFile="") {
1883
	$func="image".$this->img_format;
1884
	if( $this->img_format=="jpeg" && $this->quality != null ) {
1885
	    $res = @$func($this->img,$aFile,$this->quality);
1886
	}
1887
	else {
1888
	    if( $aFile != "" ) {
1889
		$res = @$func($this->img,$aFile);
1890
		if( !$res )
1891
		    JpGraphError::RaiseL(25107,$aFile);//("Can't write to file '$aFile'. Check that the process running PHP has enough permission.");
1892
	    }
1893
	    else {
1894
		$res = @$func($this->img);
1895
		if( !$res )
1896
		    JpGraphError::RaiseL(25108);//("Can't stream image. This is most likely due to a faulty PHP/GD setup. Try to recompile PHP and use the built-in GD library that comes with PHP.");
1897
 
1898
	    }
1899
	}
1900
    }
1901
 
1902
    // Clear resource tide up by image
1903
    function Destroy() {
1904
	imagedestroy($this->img);
1905
    }
1906
 
1907
    // Specify image format. Note depending on your installation
1908
    // of PHP not all formats may be supported.
1909
    function SetImgFormat($aFormat,$aQuality=75) {
1910
	$this->quality = $aQuality;
1911
	$aFormat = strtolower($aFormat);
1912
	$tst = true;
1913
	$supported = imagetypes();
1914
	if( $aFormat=="auto" ) {
1915
	    if( $supported & IMG_PNG )
1916
		$this->img_format="png";
1917
	    elseif( $supported & IMG_JPG )
1918
		$this->img_format="jpeg";
1919
	    elseif( $supported & IMG_GIF )
1920
		$this->img_format="gif";
1921
	    else
1922
		JpGraphError::RaiseL(25109);//("Your PHP (and GD-lib) installation does not appear to support any known graphic formats. You need to first make sure GD is compiled as a module to PHP. If you also want to use JPEG images you must get the JPEG library. Please see the PHP docs for details.");
1923
 
1924
	    return true;
1925
	}
1926
	else {
1927
	    if( $aFormat=="jpeg" || $aFormat=="png" || $aFormat=="gif" ) {
1928
		if( $aFormat=="jpeg" && !($supported & IMG_JPG) )
1929
		    $tst=false;
1930
		elseif( $aFormat=="png" && !($supported & IMG_PNG) )
1931
		    $tst=false;
1932
		elseif( $aFormat=="gif" && !($supported & IMG_GIF) )
1933
		    $tst=false;
1934
		else {
1935
		    $this->img_format=$aFormat;
1936
		    return true;
1937
		}
1938
	    }
1939
	    else
1940
		$tst=false;
1941
	    if( !$tst )
1942
		JpGraphError::RaiseL(25110,$aFormat);//(" Your PHP installation does not support the chosen graphic format: $aFormat");
1943
	}
1944
    }
1945
} // CLASS
1946
 
1947
//===================================================
1948
// CLASS RotImage
1949
// Description: Exactly as Image but draws the image at
1950
// a specified angle around a specified rotation point.
1951
//===================================================
1952
class RotImage extends Image {
1953
    var $m=array();
1954
    var $a=0;
1955
    var $dx=0,$dy=0,$transx=0,$transy=0;
1956
 
1957
    function RotImage($aWidth,$aHeight,$a=0,$aFormat=DEFAULT_GFORMAT,$aSetAutoMargin=true) {
1958
	$this->Image($aWidth,$aHeight,$aFormat,$aSetAutoMargin);
1959
	$this->dx=$this->left_margin+$this->plotwidth/2;
1960
	$this->dy=$this->top_margin+$this->plotheight/2;
1961
	$this->SetAngle($a);
1962
    }
1963
 
1964
    function SetCenter($dx,$dy) {
1965
	$old_dx = $this->dx;
1966
	$old_dy = $this->dy;
1967
	$this->dx=$dx;
1968
	$this->dy=$dy;
1969
	$this->SetAngle($this->a);
1970
	return array($old_dx,$old_dy);
1971
    }
1972
 
1973
    function SetTranslation($dx,$dy) {
1974
	$old = array($this->transx,$this->transy);
1975
	$this->transx = $dx;
1976
	$this->transy = $dy;
1977
	return $old;
1978
    }
1979
 
1980
    function UpdateRotMatrice()  {
1981
	$a = $this->a;
1982
	$a *= M_PI/180;
1983
	$sa=sin($a); $ca=cos($a);
1984
	// Create the rotation matrix
1985
	$this->m[0][0] = $ca;
1986
	$this->m[0][1] = -$sa;
1987
	$this->m[0][2] = $this->dx*(1-$ca) + $sa*$this->dy ;
1988
	$this->m[1][0] = $sa;
1989
	$this->m[1][1] = $ca;
1990
	$this->m[1][2] = $this->dy*(1-$ca) - $sa*$this->dx ;
1991
    }
1992
 
1993
    function SetAngle($a) {
1994
	$tmp = $this->a;
1995
	$this->a = $a;
1996
	$this->UpdateRotMatrice();
1997
	return $tmp;
1998
    }
1999
 
2000
    function Circle($xc,$yc,$r) {
2001
	// Circle get's rotated through the Arc() call
2002
	// made in the parent class
2003
	parent::Circle($xc,$yc,$r);
2004
    }
2005
 
2006
    function FilledCircle($xc,$yc,$r) {
2007
	list($xc,$yc) = $this->Rotate($xc,$yc);
2008
	parent::FilledCircle($xc,$yc,$r);
2009
    }
2010
 
2011
    function Arc($xc,$yc,$w,$h,$s,$e) {
2012
	list($xc,$yc) = $this->Rotate($xc,$yc);
2013
	$s += $this->a;
2014
	$e += $this->a;
2015
	parent::Arc($xc,$yc,$w,$h,$s,$e);
2016
    }
2017
 
2018
    function FilledArc($xc,$yc,$w,$h,$s,$e) {
2019
	list($xc,$yc) = $this->Rotate($xc,$yc);
2020
	$s += $this->a;
2021
	$e += $this->a;
2022
	parent::FilledArc($xc,$yc,$w,$h,$s,$e);
2023
    }
2024
 
2025
    function SetMargin($lm,$rm,$tm,$bm) {
2026
	parent::SetMargin($lm,$rm,$tm,$bm);
2027
	$this->dx=$this->left_margin+$this->plotwidth/2;
2028
	$this->dy=$this->top_margin+$this->plotheight/2;
2029
	$this->UpdateRotMatrice();
2030
    }
2031
 
2032
    function Rotate($x,$y) {
2033
	// Optimization. Ignore rotation if Angle==0 || ANgle==360
2034
	if( $this->a == 0 || $this->a == 360 ) {
2035
	    return array($x + $this->transx, $y + $this->transy );
2036
	}
2037
	else {
2038
	    $x1=round($this->m[0][0]*$x + $this->m[0][1]*$y,1) + $this->m[0][2] + $this->transx;
2039
	    $y1=round($this->m[1][0]*$x + $this->m[1][1]*$y,1) + $this->m[1][2] + $this->transy;
2040
	    return array($x1,$y1);
2041
	}
2042
    }
2043
 
2044
    function CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth=-1,$fromHeight=-1,$aMix=100) {
2045
	list($toX,$toY) = $this->Rotate($toX,$toY);
2046
	parent::CopyMerge($fromImg,$toX,$toY,$fromX,$fromY,$toWidth,$toHeight,$fromWidth,$fromHeight,$aMix);
2047
 
2048
    }
2049
 
2050
    function ArrRotate($pnts) {
2051
	$n = count($pnts)-1;
2052
	for($i=0; $i < $n; $i+=2) {
2053
	    list ($x,$y) = $this->Rotate($pnts[$i],$pnts[$i+1]);
2054
	    $pnts[$i] = $x; $pnts[$i+1] = $y;
2055
	}
2056
	return $pnts;
2057
    }
2058
 
2059
    function Line($x1,$y1,$x2,$y2) {
2060
	list($x1,$y1) = $this->Rotate($x1,$y1);
2061
	list($x2,$y2) = $this->Rotate($x2,$y2);
2062
	parent::Line($x1,$y1,$x2,$y2);
2063
    }
2064
 
2065
    function Rectangle($x1,$y1,$x2,$y2) {
2066
	// Rectangle uses Line() so it will be rotated through that call
2067
	parent::Rectangle($x1,$y1,$x2,$y2);
2068
    }
2069
 
2070
    function FilledRectangle($x1,$y1,$x2,$y2) {
2071
	if( $y1==$y2 || $x1==$x2 )
2072
	    $this->Line($x1,$y1,$x2,$y2);
2073
	else
2074
	    $this->FilledPolygon(array($x1,$y1,$x2,$y1,$x2,$y2,$x1,$y2));
2075
    }
2076
 
2077
    function Polygon($pnts,$closed=FALSE,$fast=false) {
2078
	// Polygon uses Line() so it will be rotated through that call unless
2079
	// fast drawing routines are used in which case a rotate is needed
2080
	if( $fast ) {
2081
	    parent::Polygon($this->ArrRotate($pnts));
2082
	}
2083
	else
2084
	    parent::Polygon($pnts,$closed,$fast);
2085
    }
2086
 
2087
    function FilledPolygon($pnts) {
2088
	parent::FilledPolygon($this->ArrRotate($pnts));
2089
    }
2090
 
2091
    function Point($x,$y) {
2092
	list($xp,$yp) = $this->Rotate($x,$y);
2093
	parent::Point($xp,$yp);
2094
    }
2095
 
2096
    function StrokeText($x,$y,$txt,$dir=0,$paragraph_align="left",$debug=false) {
2097
	list($xp,$yp) = $this->Rotate($x,$y);
2098
	return parent::StrokeText($xp,$yp,$txt,$dir,$paragraph_align,$debug);
2099
    }
2100
}
2101
 
2102
 
2103
?>