[ Index ]
 

Code source de Cr@wltr@ck 2.2.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/graphs/artichow/php4/ -> Image.class.php (source)

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


Généré le : Thu Sep 6 14:14:11 2007 par Balluche grâce à PHPXref 0.7