[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/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   // 20/08/2006 : CMIL : update function send($file = NULL) to not use exit()
  10   
  11  /* <php4> */
  12  
  13  define("IMAGE_JPEG", 1);
  14  define("IMAGE_PNG", 2);
  15  define("IMAGE_GIF", 3);
  16  
  17  /* </php4> */
  18  
  19  /*
  20   * Check for GD2
  21   */
  22  if(function_exists('imagecreatetruecolor') === FALSE) {
  23      trigger_error("You must compile PHP with GD2 support to use Artichow", E_USER_ERROR);
  24  }
  25  
  26  require_once  ARTICHOW."/inc/Shadow.class.php";
  27  require_once  ARTICHOW."/inc/Border.class.php";
  28   
  29  /**
  30   * An image for a graph
  31   *
  32   * @package Artichow
  33   */
  34  class awImage {
  35  
  36      /**
  37       * Graph width
  38       *
  39       * @var int
  40       */
  41      var $width;
  42  
  43      /**
  44       * Graph height
  45       *
  46       * @var int
  47       */
  48      var $height;
  49      
  50      /**
  51       * Use anti-aliasing ?
  52       *
  53       * @var bool
  54       */
  55      var $antiAliasing = FALSE;
  56      
  57      /**
  58       * Image format
  59       *
  60       * @var int
  61       */
  62      var $format = IMAGE_PNG;
  63      
  64      /**
  65       * Image background color
  66       *
  67       * @var Color
  68       */
  69      var $background;
  70      
  71      /**
  72       * GD resource
  73       *
  74       * @var resource
  75       */
  76      var $resource;
  77      
  78      /**
  79       * Image drawer
  80       *
  81       * @var Drawer
  82       */
  83      var $drawer;
  84      
  85      /**
  86       * Shadow
  87       *
  88       * @var Shadow
  89       */
  90      var $shadow;
  91      
  92      /**
  93       * Image border
  94       *
  95       * @var Border
  96       */
  97      var $border;
  98      
  99      /**
 100       * Use JPEG for image
 101       *
 102       * @var int
 103       */
 104      
 105      
 106      /**
 107       * Use PNG for image
 108       *
 109       * @var int
 110       */
 111      
 112      
 113      /**
 114       * Use GIF for image
 115       *
 116       * @var int
 117       */
 118      
 119      
 120      /**
 121       * Build the image
 122       */
 123  	 function awImage() {
 124          
 125          $this->background = new awColor(255, 255, 255);
 126          $this->shadow = new awShadow(SHADOW_RIGHT_BOTTOM);
 127          $this->border = new awBorder;
 128          
 129      }
 130      
 131      /**
 132       * Get drawer of the image
 133       *
 134       * @param int $w Drawer width (from 0 to 1) (default to 1)
 135       * @param int $h Drawer height (from 0 to 1) (default to 1)
 136       * @param float $x Position on X axis of the center of the drawer (default to 0.5)
 137       * @param float $y Position on Y axis of the center of the drawer (default to 0.5)
 138       * @return Drawer
 139       */
 140  	 function getDrawer($w = 1, $h = 1, $x = 0.5, $y = 0.5) {
 141          $this->create();
 142          $this->drawer->setSize($w, $h);
 143          $this->drawer->setPosition($x, $y);
 144          return $this->drawer;
 145      }
 146      
 147      /**
 148       * Change the image size
 149       *
 150       * @var int $width Image width
 151       * @var int $height Image height
 152       */
 153  	 function setSize($width, $height) {
 154      
 155          if($width !== NULL) {
 156              $this->width = (int)$width;
 157          }
 158          if($height !== NULL) {
 159              $this->height = (int)$height;
 160          }
 161      
 162      }
 163      
 164      /**
 165       * Change image background color
 166       *
 167       * @param $color
 168       */
 169  	 function setBackgroundColor($color) {
 170          $this->background = $color;
 171      }
 172      
 173      /**
 174       * Change image background gradient
 175       *
 176       * @param $gradient
 177       */
 178  	 function setBackgroundGradient($gradient) {
 179          $this->background = $gradient;
 180      }
 181      
 182      /**
 183       * Can we use anti-aliasing ?
 184       *
 185       * @var bool $bool
 186       */
 187  	 function setAntiAliasing($bool) {
 188          $this->antiAliasing = (bool)$bool;
 189      }
 190      
 191      /**
 192       * Change image format
 193       *
 194       * @var int $format New image format
 195       */
 196  	 function setFormat($format) {
 197          if($format === IMAGE_JPEG or $format === IMAGE_PNG or $format === IMAGE_GIF) {
 198              $this->format = $format;
 199          }
 200      }
 201      
 202      /**
 203       * Create a new awimage
 204       */
 205  	 function create() {
 206      
 207          if($this->resource === NULL) {
 208      
 209              // Create image
 210              
 211              $this->resource = imagecreatetruecolor($this->width, $this->height);
 212              if(!$this->resource) {
 213                  trigger_error("Unable to create a graph", E_USER_ERROR);
 214              }
 215              
 216              imagealphablending($this->resource, TRUE);
 217              
 218              if($this->antiAliasing and function_exists('imageantialias')) {
 219                  imageantialias($this->resource, TRUE);
 220              }
 221              
 222              $this->drawer = new awDrawer($this->resource);
 223              $this->drawer->setImageSize($this->width, $this->height);
 224              
 225              // Original color
 226              $this->drawer->filledRectangle(
 227                  new awWhite,
 228                  new awLine(
 229                      new awPoint(0, 0),
 230                      new awPoint($this->width, $this->height)
 231                  )
 232              );
 233          
 234              $shadow = $this->shadow->getSpace();
 235              
 236              $p1 = new awPoint($shadow->left, $shadow->top);
 237              $p2 = new awPoint($this->width - $shadow->right - 1, $this->height - $shadow->bottom - 1);
 238          
 239              // Draw image background
 240              $this->drawer->filledRectangle($this->background, new awLine($p1, $p2));
 241              $this->background->free();
 242              
 243              // Draw image border
 244              $this->border->rectangle($this->drawer, $p1, $p2);
 245              
 246          }
 247          
 248      }
 249      
 250      /**
 251       * Draw a component on the image
 252       *
 253       * @var &$component A component
 254       */
 255  	 function drawComponent(&$component) {
 256          
 257          $shadow = $this->shadow->getSpace(); // Image shadow
 258          $border = $this->border->visible() ? 1 : 0; // Image border size
 259      
 260          $drawer = $this->drawer;
 261          $drawer->setImageSize(
 262              $this->width - $shadow->left - $shadow->right - $border * 2,
 263              $this->height - $shadow->top - $shadow->bottom - $border * 2
 264          );
 265      
 266          // No absolute size specified
 267          if($component->w === NULL and $component->h === NULL) {
 268          
 269              list($width, $height) = $drawer->setSize($component->width, $component->height);
 270      
 271              // Set component size in pixels
 272              $component->setAbsSize($width, $height);
 273              
 274          } else {
 275          
 276              $drawer->setAbsSize($component->w, $component->h);
 277          
 278          }
 279          
 280          if($component->top !== NULL and $component->left !== NULL) {
 281              $drawer->setAbsPosition(
 282                  $border + $shadow->left + $component->left,
 283                  $border + $shadow->top + $component->top
 284              );
 285          } else {
 286              $drawer->setPosition($component->x, $component->y);
 287          }
 288          
 289          $drawer->movePosition($border + $shadow->left, $border + $shadow->top);
 290          
 291          list($x1, $y1, $x2, $y2) = $component->getPosition();
 292          
 293          $component->init($drawer);
 294          
 295          $component->drawComponent($drawer, $x1, $y1, $x2, $y2, $this->antiAliasing);
 296          $component->drawEnvelope($drawer, $x1, $y1, $x2, $y2);
 297          
 298          $component->finalize($drawer);
 299      
 300      }
 301      
 302  	 function drawShadow() {
 303      
 304          $drawer = $this->getDrawer();
 305          
 306          $this->shadow->draw(
 307              $drawer,
 308              new awPoint(0, 0),
 309              new awPoint($this->width, $this->height),
 310              SHADOW_IN
 311          );
 312      
 313      }
 314      
 315      /**
 316       * Send the image into a file or to the user browser
 317       *
 318       * @var string $file Save image into a file if you provide a file name
 319       */
 320  	 function send($file = NULL) {
 321      
 322          // Test if format is available
 323          if((imagetypes() & $this->format) === FALSE) {
 324              trigger_error("Format '".$this->format."' is not available on your system. Check that your PHP has been compiled with the good libraries.");
 325          }
 326      
 327          // Get some infos about this image
 328          
 329          switch($this->format) {
 330              case IMAGE_JPEG :
 331                  $function = 'imagejpeg';
 332                  break;
 333              case IMAGE_PNG :
 334                  $function = 'imagepng';
 335                  break;
 336              case IMAGE_GIF :
 337                  $function = 'imagegif';
 338                  break;
 339          }
 340          
 341          // Create image
 342          
 343          if($file !== NULL) {
 344          
 345              $function($this->resource, $file);
 346              
 347          } else {
 348      
 349              // Test some text has been printed
 350              $data = ob_get_contents();
 351              // CMIL : update to not use exit()
 352              if($data !== '') {
 353                  //exit;
 354              }
 355              else {
 356                  // Send headers to the browser
 357                  header("Content-type: image/".$this->getFormat());
 358                  
 359                  $function($this->resource);
 360              }
 361              
 362          }
 363      
 364      }
 365      
 366  	 function getFormat() {
 367          
 368          switch($this->format) {
 369              case IMAGE_JPEG :
 370                  return 'jpeg';
 371              case IMAGE_PNG :
 372                  return 'png';
 373              case IMAGE_GIF :
 374                  return 'gif';
 375          }
 376          
 377      }
 378  
 379  }
 380  
 381  registerClass('Image');
 382  
 383   
 384  /**
 385   * Load an image from a file
 386   *
 387   * @package Artichow
 388   */
 389  class awFileImage extends awImage {
 390  
 391      /**
 392       * Build a new awimage
 393       *
 394       * @param string $file Image file name
 395       */
 396  	 function awFileImage($file) {
 397      
 398          $image = @getimagesize($file);
 399          
 400          if($image and in_array($image[2], array(2, 3))) {
 401          
 402              $this->setSize($image[0], $image[1]);
 403              
 404              switch($image[2]) {
 405              
 406                  case 2 :
 407                      $this->resource = imagecreatefromjpeg($file);
 408                      break;
 409              
 410                  case 3 :
 411                      $this->resource = imagecreatefrompng($file);
 412                      break;
 413              
 414              }
 415          
 416              $this->drawer = new awDrawer($this->resource);
 417              $this->drawer->setImageSize($this->width, $this->height);
 418              
 419          } else {
 420              trigger_error("Artichow does not support this image (must be in PNG or JPEG)", E_USER_ERROR);
 421          }
 422      
 423      }
 424  
 425  }
 426  
 427  registerClass('FileImage');
 428  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics