[ Index ] |
|
Code source de Cr@wltr@ck 2.2.1 |
1 <?php 2 /* 3 * This work is hereby released into the Public Domain. 4 * To view a copy of the public domain dedication, 5 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to 6 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 7 * 8 */ 9 10 /* <php5> */ 11 if(is_file(dirname(__FILE__)."/Artichow.cfg.php")) { // For PHP 4+5 version 12 require_once dirname(__FILE__)."/Artichow.cfg.php"; 13 } 14 /* </php5> */ 15 16 17 /* <php4> */ 18 19 define("IMAGE_JPEG", 1); 20 define("IMAGE_PNG", 2); 21 define("IMAGE_GIF", 3); 22 23 /* </php4> */ 24 25 /* 26 * Register a class with the prefix in configuration file 27 */ 28 function registerClass($class, $abstract = FALSE) { 29 30 if(ARTICHOW_PREFIX === 'aw') { 31 return; 32 } 33 34 /* <php5> */ 35 if($abstract) { 36 $abstract = 'abstract'; 37 } else { 38 $abstract = ''; 39 } 40 /* </php5> */ 41 /* <php4> -- 42 $abstract = ''; 43 -- </php4> */ 44 45 eval($abstract." class ".ARTICHOW_PREFIX.$class." extends aw".$class." { }"); 46 47 } 48 49 /* 50 * Register an interface with the prefix in configuration file 51 */ 52 function registerInterface($interface) { 53 54 if(ARTICHOW_PREFIX === 'aw') { 55 return; 56 } 57 58 /* <php5> */ 59 eval("interface ".ARTICHOW_PREFIX.$interface." extends aw".$interface." { }"); 60 /* </php5> */ 61 62 } 63 64 // Some useful files 65 require_once ARTICHOW."/Component.class.php"; 66 67 require_once ARTICHOW."/inc/Grid.class.php"; 68 require_once ARTICHOW."/inc/Tools.class.php"; 69 require_once ARTICHOW."/inc/Drawer.class.php"; 70 require_once ARTICHOW."/inc/Math.class.php"; 71 require_once ARTICHOW."/inc/Tick.class.php"; 72 require_once ARTICHOW."/inc/Axis.class.php"; 73 require_once ARTICHOW."/inc/Legend.class.php"; 74 require_once ARTICHOW."/inc/Mark.class.php"; 75 require_once ARTICHOW."/inc/Label.class.php"; 76 require_once ARTICHOW."/inc/Text.class.php"; 77 require_once ARTICHOW."/inc/Color.class.php"; 78 require_once ARTICHOW."/inc/Font.class.php"; 79 require_once ARTICHOW."/inc/Gradient.class.php"; 80 require_once ARTICHOW."/inc/Shadow.class.php"; 81 require_once ARTICHOW."/inc/Border.class.php"; 82 83 require_once ARTICHOW."/common.php"; 84 85 /** 86 * An image for a graph 87 * 88 * @package Artichow 89 */ 90 class awImage { 91 92 /** 93 * Graph width 94 * 95 * @var int 96 */ 97 public $width; 98 99 /** 100 * Graph height 101 * 102 * @var int 103 */ 104 public $height; 105 106 /** 107 * Use anti-aliasing ? 108 * 109 * @var bool 110 */ 111 protected $antiAliasing = FALSE; 112 113 /** 114 * Image format 115 * 116 * @var int 117 */ 118 protected $format = awImage::PNG; 119 120 /** 121 * Image background color 122 * 123 * @var Color 124 */ 125 protected $background; 126 127 /** 128 * GD resource 129 * 130 * @var resource 131 */ 132 protected $resource; 133 134 /** 135 * Image drawer 136 * 137 * @var Drawer 138 */ 139 protected $drawer; 140 141 /** 142 * Shadow 143 * 144 * @var Shadow 145 */ 146 public $shadow; 147 148 /** 149 * Image border 150 * 151 * @var Border 152 */ 153 public $border; 154 155 /** 156 * Use JPEG for image 157 * 158 * @var int 159 */ 160 const JPEG = IMG_JPG; 161 162 /** 163 * Use PNG for image 164 * 165 * @var int 166 */ 167 const PNG = IMG_PNG; 168 169 /** 170 * Use GIF for image 171 * 172 * @var int 173 */ 174 const GIF = IMG_GIF; 175 176 /** 177 * Build the image 178 */ 179 public function __construct() { 180 181 $this->background = new awColor(255, 255, 255); 182 $this->shadow = new awShadow(awShadow::RIGHT_BOTTOM); 183 $this->border = new awBorder; 184 185 } 186 187 /** 188 * Get drawer of the image 189 * 190 * @param int $w Drawer width (from 0 to 1) (default to 1) 191 * @param int $h Drawer height (from 0 to 1) (default to 1) 192 * @param float $x Position on X axis of the center of the drawer (default to 0.5) 193 * @param float $y Position on Y axis of the center of the drawer (default to 0.5) 194 * @return Drawer 195 */ 196 public function getDrawer($w = 1, $h = 1, $x = 0.5, $y = 0.5) { 197 $this->create(); 198 $this->drawer->setSize($w, $h); 199 $this->drawer->setPosition($x, $y); 200 return $this->drawer; 201 } 202 203 /** 204 * Change the image size 205 * 206 * @var int $width Image width 207 * @var int $height Image height 208 */ 209 public function setSize($width, $height) { 210 211 if($width !== NULL) { 212 $this->width = (int)$width; 213 } 214 if($height !== NULL) { 215 $this->height = (int)$height; 216 } 217 218 } 219 220 /** 221 * Change image background color 222 * 223 * @param awColor $color 224 */ 225 public function setBackgroundColor(awColor $color) { 226 $this->background = $color; 227 } 228 229 /** 230 * Change image background gradient 231 * 232 * @param awGradient $gradient 233 */ 234 public function setBackgroundGradient(awGradient $gradient) { 235 $this->background = $gradient; 236 } 237 238 /** 239 * Can we use anti-aliasing ? 240 * 241 * @var bool $bool 242 */ 243 public function setAntiAliasing($bool) { 244 $this->antiAliasing = (bool)$bool; 245 } 246 247 /** 248 * Change image format 249 * 250 * @var int $format New image format 251 */ 252 public function setFormat($format) { 253 if($format === awImage::JPEG or $format === awImage::PNG or $format === awImage::GIF) { 254 $this->format = $format; 255 } 256 } 257 258 /** 259 * Create a new awimage 260 */ 261 public function create() { 262 263 if($this->resource === NULL) { 264 265 // Create image 266 267 $this->resource = imagecreatetruecolor($this->width, $this->height); 268 269 if(!$this->resource) { 270 awImage::drawError("Class Image: Unable to create a graph."); 271 } 272 273 imagealphablending($this->resource, TRUE); 274 275 if($this->antiAliasing) { 276 if(function_exists('imageantialias')) { 277 imageantialias($this->resource, TRUE); 278 } else { 279 awImage::drawErrorFile('missing-anti-aliasing'); 280 } 281 } 282 283 $this->drawer = new awDrawer($this->resource); 284 $this->drawer->setImageSize($this->width, $this->height); 285 286 // Original color 287 $this->drawer->filledRectangle( 288 new awWhite, 289 new awLine( 290 new awPoint(0, 0), 291 new awPoint($this->width, $this->height) 292 ) 293 ); 294 295 $shadow = $this->shadow->getSpace(); 296 297 $p1 = new awPoint($shadow->left, $shadow->top); 298 $p2 = new awPoint($this->width - $shadow->right - 1, $this->height - $shadow->bottom - 1); 299 300 // Draw image background 301 $this->drawer->filledRectangle($this->background, new awLine($p1, $p2)); 302 $this->background->free(); 303 304 // Draw image border 305 $this->border->rectangle($this->drawer, $p1, $p2); 306 307 } 308 309 } 310 311 /** 312 * Draw a component on the image 313 * 314 * @var awComponent $component A component 315 */ 316 public function drawComponent(awComponent $component) { 317 318 $shadow = $this->shadow->getSpace(); // Image shadow 319 $border = $this->border->visible() ? 1 : 0; // Image border size 320 321 $drawer = clone $this->drawer; 322 $drawer->setImageSize( 323 $this->width - $shadow->left - $shadow->right - $border * 2, 324 $this->height - $shadow->top - $shadow->bottom - $border * 2 325 ); 326 327 // No absolute size specified 328 if($component->w === NULL and $component->h === NULL) { 329 330 list($width, $height) = $drawer->setSize($component->width, $component->height); 331 332 // Set component size in pixels 333 $component->setAbsSize($width, $height); 334 335 } else { 336 337 $drawer->setAbsSize($component->w, $component->h); 338 339 } 340 341 if($component->top !== NULL and $component->left !== NULL) { 342 $drawer->setAbsPosition( 343 $border + $shadow->left + $component->left, 344 $border + $shadow->top + $component->top 345 ); 346 } else { 347 $drawer->setPosition($component->x, $component->y); 348 } 349 350 $drawer->movePosition($border + $shadow->left, $border + $shadow->top); 351 352 list($x1, $y1, $x2, $y2) = $component->getPosition(); 353 354 $component->init($drawer); 355 356 $component->drawComponent($drawer, $x1, $y1, $x2, $y2, $this->antiAliasing); 357 $component->drawEnvelope($drawer, $x1, $y1, $x2, $y2); 358 359 $component->finalize($drawer); 360 361 } 362 363 protected function drawShadow() { 364 365 $drawer = $this->getDrawer(); 366 367 $this->shadow->draw( 368 $drawer, 369 new awPoint(0, 0), 370 new awPoint($this->width, $this->height), 371 awShadow::IN 372 ); 373 374 } 375 376 /** 377 * Send the image into a file or to the user browser 378 * 379 * @var string $return If set to true, this method will return the image instead of outputing it 380 * @var string $header Enable/disable sending content-type header 381 */ 382 public function send($return = FALSE, $header = TRUE) { 383 384 // Test if format is available 385 if((imagetypes() & $this->format) === FALSE) { 386 awImage::drawError("Class Image: Format '".$this->format."' is not available on your system. Check that your PHP has been compiled with the good libraries."); 387 } 388 389 // Get some infos about this image 390 391 switch($this->format) { 392 case awImage::JPEG : 393 $function = 'imagejpeg'; 394 break; 395 case awImage::PNG : 396 $function = 'imagepng'; 397 break; 398 case awImage::GIF : 399 $function = 'imagegif'; 400 break; 401 } 402 403 // Create image 404 405 // Send headers to the browser 406 if($header === TRUE and headers_sent() === FALSE) { 407 header("Content-type: image/".$this->getFormat()); 408 } 409 410 if($return) { 411 ob_start(); 412 } 413 414 $function($this->resource); 415 416 if($return) { 417 return ob_get_clean(); 418 } 419 420 } 421 422 /* <php5> */ 423 private static $errorWriting = FALSE; 424 /* </php5> */ 425 426 /* 427 * Display an error image and exit 428 * 429 * @param string $message Error message 430 */ 431 public static function drawError($message) { 432 433 /* <php4> -- 434 static $errorWriting; 435 -- </php4> */ 436 437 if(self::$errorWriting) { 438 return; 439 } 440 441 self::$errorWriting = TRUE; 442 443 $message = wordwrap($message, 40, "\n", TRUE); 444 445 $width = 400; 446 $height = max(100, 40 + 22.5 * (substr_count($message, "\n") + 1)); 447 448 $image = new awImage(); 449 $image->setSize($width, $height); 450 451 $drawer = $image->getDrawer(); 452 453 // Display title 454 $drawer->filledRectangle( 455 new awWhite, 456 new awLine( 457 new awPoint(0, 0), 458 new awPoint($width, $height) 459 ) 460 ); 461 462 $drawer->filledRectangle( 463 new awRed, 464 new awLine( 465 new awPoint(0, 0), 466 new awPoint(110, 25) 467 ) 468 ); 469 470 $text = new awText( 471 "Artichow error", 472 new awFont3, 473 new awWhite, 474 0 475 ); 476 477 $drawer->string($text, new awPoint(5, 6)); 478 479 // Display red box 480 $drawer->rectangle( 481 new awRed, 482 new awLine( 483 new awPoint(0, 25), 484 new awPoint($width - 90, $height - 1) 485 ) 486 ); 487 488 // Display error image 489 $file = ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'error.png'; 490 491 $imageError = new awFileImage($file); 492 $drawer->copyImage( 493 $imageError, 494 new awPoint($width - 81, $height - 81), 495 new awPoint($width - 1, $height - 1) 496 ); 497 498 // Draw message 499 $text = new awText( 500 $message, 501 new awFont2, 502 new awBlack, 503 0 504 ); 505 506 $drawer->string($text, new awPoint(10, 40)); 507 508 $image->send(); 509 510 exit; 511 512 } 513 514 /* 515 * Display an error image located in a file and exit 516 * 517 * @param string $error Error name 518 */ 519 public static function drawErrorFile($error) { 520 521 $file = ARTICHOW_IMAGE.DIRECTORY_SEPARATOR.'errors'.DIRECTORY_SEPARATOR.$error.'.png'; 522 523 header("Content-Type: image/png"); 524 readfile($file); 525 exit; 526 527 } 528 529 protected function getFormat() { 530 531 switch($this->format) { 532 case awImage::JPEG : 533 return 'jpeg'; 534 case awImage::PNG : 535 return 'png'; 536 case awImage::GIF : 537 return 'gif'; 538 } 539 540 } 541 542 } 543 544 registerClass('Image'); 545 546 547 /** 548 * Load an image from a file 549 * 550 * @package Artichow 551 */ 552 class awFileImage extends awImage { 553 554 /** 555 * Build a new awimage 556 * 557 * @param string $file Image file name 558 */ 559 public function __construct($file) { 560 561 $image = @getimagesize($file); 562 563 if($image and in_array($image[2], array(2, 3))) { 564 565 $this->setSize($image[0], $image[1]); 566 567 switch($image[2]) { 568 569 case 2 : 570 $this->resource = imagecreatefromjpeg($file); 571 break; 572 573 case 3 : 574 $this->resource = imagecreatefrompng($file); 575 break; 576 577 } 578 579 $this->drawer = new awDrawer($this->resource); 580 $this->drawer->setImageSize($this->width, $this->height); 581 582 } else { 583 awImage::drawError("Class FileImage: Artichow does not support the format of this image (must be in PNG or JPEG)"); 584 } 585 586 } 587 588 } 589 590 registerClass('FileImage'); 591 592 /* 593 * Check for GD2 594 */ 595 if(function_exists('imagecreatetruecolor') === FALSE) { 596 awImage::drawErrorFile('missing-gd2'); 597 } 598 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Sep 6 14:14:11 2007 | par Balluche grâce à PHPXref 0.7 |