-
Notifications
You must be signed in to change notification settings - Fork 26
/
Image.class.php
executable file
·619 lines (494 loc) · 11.3 KB
/
Image.class.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
<?php
/*
* This work is hereby released into the Public Domain.
* To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
*
*/
/* <php5> */
if(is_file(dirname(__FILE__)."/Artichow.cfg.php")) { // For PHP 4+5 version
require_once dirname(__FILE__)."/Artichow.cfg.php";
}
/* </php5> */
/* <php4> */
define("IMAGE_JPEG", 1);
define("IMAGE_PNG", 2);
define("IMAGE_GIF", 3);
/* </php4> */
/*
* Register a class with the prefix in configuration file
*/
function registerClass($class, $abstract = FALSE) {
if(ARTICHOW_PREFIX === 'aw') {
return;
}
/* <php5> */
if($abstract) {
$abstract = 'abstract';
} else {
$abstract = '';
}
/* </php5> */
/* <php4> --
$abstract = '';
-- </php4> */
eval($abstract." class ".ARTICHOW_PREFIX.$class." extends aw".$class." { }");
}
/*
* Register an interface with the prefix in configuration file
*/
function registerInterface($interface) {
if(ARTICHOW_PREFIX === 'aw') {
return;
}
/* <php5> */
eval("interface ".ARTICHOW_PREFIX.$interface." extends aw".$interface." { }");
/* </php5> */
}
// Some useful files
require_once ARTICHOW."/Component.class.php";
require_once ARTICHOW."/inc/Grid.class.php";
require_once ARTICHOW."/inc/Tools.class.php";
require_once ARTICHOW."/inc/Driver.class.php";
require_once ARTICHOW."/inc/Math.class.php";
require_once ARTICHOW."/inc/Tick.class.php";
require_once ARTICHOW."/inc/Axis.class.php";
require_once ARTICHOW."/inc/Legend.class.php";
require_once ARTICHOW."/inc/Mark.class.php";
require_once ARTICHOW."/inc/Label.class.php";
require_once ARTICHOW."/inc/Text.class.php";
require_once ARTICHOW."/inc/Color.class.php";
require_once ARTICHOW."/inc/Font.class.php";
require_once ARTICHOW."/inc/Gradient.class.php";
require_once ARTICHOW."/inc/Shadow.class.php";
require_once ARTICHOW."/inc/Border.class.php";
require_once ARTICHOW."/common.php";
/**
* An image for a graph
*
* @package Artichow
*/
class awImage {
/**
* Graph width
*
* @var int
*/
public $width;
/**
* Graph height
*
* @var int
*/
public $height;
/**
* Use anti-aliasing ?
*
* @var bool
*/
protected $antiAliasing = FALSE;
/**
* Image format
*
* @var int
*/
protected $format = awImage::PNG;
/**
* Image background color
*
* @var Color
*/
protected $background;
/**
* GD resource
*
* @var resource
*/
protected $resource;
/**
* A Driver object
*
* @var Driver
*/
protected $driver;
/**
* Driver string
*
* @var string
*/
protected $driverString;
/**
* Shadow
*
* @var Shadow
*/
public $shadow;
/**
* Image border
*
* @var Border
*/
public $border;
/**
* Use JPEG for image
*
* @var int
*/
const JPEG = IMG_JPG;
/**
* Use PNG for image
*
* @var int
*/
const PNG = IMG_PNG;
/**
* Use GIF for image
*
* @var int
*/
const GIF = IMG_GIF;
/**
* Build the image
*/
public function __construct() {
$this->background = new awColor(255, 255, 255);
$this->shadow = new awShadow(awShadow::RIGHT_BOTTOM);
$this->border = new awBorder;
}
/**
* Get driver of the image
*
* @param int $w Driver width (from 0 to 1) (default to 1)
* @param int $h Driver height (from 0 to 1) (default to 1)
* @param float $x Position on X axis of the center of the driver (default to 0.5)
* @param float $y Position on Y axis of the center of the driver (default to 0.5)
* @return Driver
*/
public function getDriver($w = 1, $h = 1, $x = 0.5, $y = 0.5) {
$this->create();
$this->driver->setSize($w, $h);
$this->driver->setPosition($x, $y);
return $this->driver;
}
/**
* Sets the driver that will be used to draw the graph
*
* @param string $driverString
*/
public function setDriver($driverString) {
$this->driver = $this->selectDriver($driverString);
$this->driver->init($this);
}
/**
* Change the image size
*
* @var int $width Image width
* @var int $height Image height
*/
public function setSize($width, $height) {
if($width !== NULL) {
$this->width = (int)$width;
}
if($height !== NULL) {
$this->height = (int)$height;
}
}
/**
* Change image background
*
* @param mixed $background
*/
public function setBackground($background) {
if($background instanceof awColor) {
$this->setBackgroundColor($background);
} elseif($background instanceof awGradient) {
$this->setBackgroundGradient($background);
}
}
/**
* Change image background color
*
* @param awColor $color
*/
public function setBackgroundColor(awColor $color) {
$this->background = $color;
}
/**
* Change image background gradient
*
* @param awGradient $gradient
*/
public function setBackgroundGradient(awGradient $gradient) {
$this->background = $gradient;
}
/**
* Return image background, whether a Color or a Gradient
*
* @return mixed
*/
public function getBackground() {
return $this->background;
}
/**
* Turn antialiasing on or off
*
* @var bool $bool
*/
public function setAntiAliasing($bool) {
$this->antiAliasing = (bool)$bool;
}
/**
* Return the antialiasing setting
*
* @return bool
*/
public function getAntiAliasing() {
return $this->antiAliasing;
}
/**
* Change image format
*
* @var int $format New image format
*/
public function setFormat($format) {
if($format === awImage::JPEG or $format === awImage::PNG or $format === awImage::GIF) {
$this->format = $format;
}
}
/**
* Returns the image format as an integer
*
* @return unknown
*/
public function getFormat() {
return $this->format;
}
/**
* Returns the image format as a string
*
* @return string
*/
public function getFormatString() {
switch($this->format) {
case awImage::JPEG :
return 'jpeg';
case awImage::PNG :
return 'png';
case awImage::GIF :
return 'gif';
}
}
/**
* Create a new awimage
*/
public function create() {
if($this->driver === NULL) {
$driver = $this->selectDriver($this->driverString);
$driver->init($this);
$this->driver = $driver;
}
}
/**
* Select the correct driver
*
* @param string $driver The desired driver
* @return mixed
*/
protected function selectDriver($driver) {
$drivers = array('gd');
$driver = strtolower((string)$driver);
if(in_array($driver, $drivers, TRUE)) {
$string = $driver;
} else {
$string = ARTICHOW_DRIVER;
}
switch ($string) {
case 'gd':
require_once ARTICHOW.'/inc/drivers/gd.class.php';
$this->driverString = $string;
return new awGDDriver();
default:
// We should never get here, unless the wrong string is used AND the ARTICHOW_DRIVER
// global has been messed with.
awImage::drawError('Class Image: Unknown driver type (\''.$string.'\')');
break;
}
}
/**
* Draw a component on the image
*
* @var awComponent $component A component
*/
public function drawComponent(awComponent $component) {
$shadow = $this->shadow->getSpace(); // Image shadow
$border = $this->border->visible() ? 1 : 0; // Image border size
$driver = clone $this->driver;
$driver->setImageSize(
$this->width - $shadow->left - $shadow->right - $border * 2,
$this->height - $shadow->top - $shadow->bottom - $border * 2
);
// No absolute size specified
if($component->w === NULL and $component->h === NULL) {
list($width, $height) = $driver->setSize($component->width, $component->height);
// Set component size in pixels
$component->setAbsSize($width, $height);
} else {
$driver->setAbsSize($component->w, $component->h);
}
if($component->top !== NULL and $component->left !== NULL) {
$driver->setAbsPosition(
$border + $shadow->left + $component->left,
$border + $shadow->top + $component->top
);
} else {
$driver->setPosition($component->x, $component->y);
}
$driver->movePosition($border + $shadow->left, $border + $shadow->top);
list($x1, $y1, $x2, $y2) = $component->getPosition();
$component->init($driver);
$component->drawComponent($driver, $x1, $y1, $x2, $y2, $this->antiAliasing);
$component->drawEnvelope($driver, $x1, $y1, $x2, $y2);
$component->finalize($driver);
}
protected function drawShadow() {
$driver = $this->getDriver();
$this->shadow->draw(
$driver,
new awPoint(0, 0),
new awPoint($this->width, $this->height),
awShadow::IN
);
}
/**
* Send the image into a file or to the user browser
*
*/
public function send() {
$this->driver->send($this);
}
/**
* Return the image content as binary data
*
*/
public function get() {
return $this->driver->get($this);
}
/**
* Send the correct HTTP header according to the image type
*
*/
public function sendHeaders() {
if(headers_sent() === FALSE) {
switch ($this->driverString) {
case 'gd' :
header('Content-type: image/'.$this->getFormatString());
break;
}
}
}
/* <php5> */
private static $errorWriting = FALSE;
/* </php5> */
/*
* Display an error image and exit
*
* @param string $message Error message
*/
public static function drawError($message) {
/* <php4> --
static $errorWriting;
-- </php4> */
if(self::$errorWriting) {
return;
}
self::$errorWriting = TRUE;
$message = wordwrap($message, 40, "\n", TRUE);
$width = 400;
$height = max(100, 40 + 22.5 * (substr_count($message, "\n") + 1));
$image = new awImage();
$image->setSize($width, $height);
$image->setDriver('gd');
$driver = $image->getDriver();
$driver->init($image);
// Display title
$driver->filledRectangle(
new awWhite,
new awLine(
new awPoint(0, 0),
new awPoint($width, $height)
)
);
$driver->filledRectangle(
new awRed,
new awLine(
new awPoint(0, 0),
new awPoint(110, 25)
)
);
$text = new awText(
"Artichow error",
new awFont3,
new awWhite,
0
);
$driver->string($text, new awPoint(5, 6));
// Display red box
$driver->rectangle(
new awRed,
new awLine(
new awPoint(0, 25),
new awPoint($width - 90, $height - 1)
)
);
// Display error image
$file = ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'error.png';
$imageError = new awFileImage($file);
$driver->copyImage(
$imageError,
new awPoint($width - 81, $height - 81),
new awPoint($width - 1, $height - 1)
);
// Draw message
$text = new awText(
strip_tags($message),
new awFont2,
new awBlack,
0
);
$driver->string($text, new awPoint(10, 40));
$image->send();
exit;
}
/*
* Display an error image located in a file and exit
*
* @param string $error Error name
*/
public static function drawErrorFile($error) {
$file = ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error.'.png';
header("Content-Type: image/png");
readfile($file);
exit;
}
}
registerClass('Image');
/**
* Load an image from a file
*
* @package Artichow
*/
class awFileImage extends awImage {
/**
* Build a new awimage
*
* @param string $file Image file name
*/
public function __construct($file) {
$driver = $this->selectDriver($this->driverString);
$driver->initFromFile($this, $file);
$this->driver = $driver;
}
}
registerClass('FileImage');
?>