[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

/lib/Horde/Image/ -> swf.php (source)

   1  <?php
   2  
   3  require_once dirname(__FILE__) . '/../Image.php';
   4  
   5  /**
   6   * This class implements the Horde_Image:: API for SWF, using the PHP
   7   * Ming extension.
   8   *
   9   * $Horde: framework/Image/Image/swf.php,v 1.24.10.6 2006/01/01 21:28:22 jan Exp $
  10   *
  11   * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
  12   *
  13   * See the enclosed file COPYING for license information (LGPL). If you
  14   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  15   *
  16   * @author  Chuck Hagenbuch <chuck@horde.org>
  17   * @since   Horde 3.0
  18   * @package Horde_Image
  19   */
  20  class Horde_Image_swf extends Horde_Image {
  21  
  22      /**
  23       * Capabilites of this driver.
  24       *
  25       * @var array
  26       */
  27      var $_capabilities = array('canvas');
  28  
  29      /**
  30       * SWF root movie.
  31       *
  32       * @var resource
  33       */
  34      var $_movie;
  35  
  36      function Horde_Image_swf($params)
  37      {
  38          parent::Horde_Image($params);
  39  
  40          $this->_movie = &new SWFMovie();
  41          $this->_movie->setDimension($this->_width, $this->_height);
  42  
  43          // FIXME: honor the 'background' parameter here.
  44          $this->_movie->setBackground(0xff, 0xff, 0xff);
  45          $this->_movie->setRate(30);
  46      }
  47  
  48      function getContentType()
  49      {
  50          return 'application/x-shockwave-flash';
  51      }
  52  
  53      /**
  54       * Display the movie.
  55       */
  56      function display()
  57      {
  58          $this->headers();
  59          $this->_movie->output();
  60      }
  61  
  62      /**
  63       * Return the raw data for this image.
  64       *
  65       * @return string  The raw image data.
  66       */
  67      function raw()
  68      {
  69          ob_start();
  70          $this->_movie->output();
  71          $data = ob_get_contents();
  72          ob_end_clean();
  73  
  74          return $data;
  75      }
  76  
  77      /**
  78       * Creates a color that can be accessed in this object. When a
  79       * color is set, the rgba values are returned in an array.
  80       *
  81       * @param string $name  The name of the color.
  82       *
  83       * @return array  The red, green, blue, alpha values of the color.
  84       */
  85      function allocateColor($name)
  86      {
  87          list($r, $g, $b) = $this->getRGB($name);
  88          return array('red' => $r,
  89                       'green' => $g,
  90                       'blue' => $b,
  91                       'alpha' => 255);
  92      }
  93  
  94      function getFont($font)
  95      {
  96          switch ($font) {
  97          case 'sans-serif':
  98              return '_sans';
  99  
 100          case 'serif':
 101              return '_serif';
 102  
 103          case 'monospace':
 104              return '_typewriter';
 105  
 106          default:
 107              return $font;
 108          }
 109      }
 110  
 111      /**
 112       * Draws a text string on the image in a specified location, with
 113       * the specified style information.
 114       *
 115       * @param string  $text       The text to draw.
 116       * @param integer $x          The left x coordinate of the start of the text string.
 117       * @param integer $y          The top y coordinate of the start of the text string.
 118       * @param string  $font       The font identifier you want to use for the text.
 119       * @param string  $color      The color that you want the text displayed in.
 120       * @param integer $direction  An integer that specifies the orientation of the text.
 121       */
 122      function text($string, $x, $y, $font = 'monospace', $color = 'black', $direction = 0)
 123      {
 124          $color = $this->allocateColor($color);
 125  
 126          if (substr(PHP_OS, 0, 3) == 'WIN') {
 127              $text = &new SWFTextField(SWFTEXTFIELD_NOEDIT);
 128          } else {
 129              $text = &new SWFText();
 130          }
 131          $text->setColor($color['red'], $color['green'], $color['blue'], $color['alpha']);
 132          $text->addString($string);
 133          $text->setFont(new SWFFont($this->getFont($font)));
 134  
 135          $t = $this->_movie->add($text);
 136          $t->moveTo($x, $y);
 137          $t->rotate($direction);
 138  
 139          return $t;
 140      }
 141  
 142      /**
 143       * Draw a circle.
 144       *
 145       * @param integer $x      The x co-ordinate of the centre.
 146       * @param integer $y      The y co-ordinate of the centre.
 147       * @param integer $r      The radius of the circle.
 148       * @param string  $color  The line color of the circle.
 149       * @param string  $fill   The color to fill the circle.
 150       */
 151      function circle($x, $y, $r, $color, $fill = 'none')
 152      {
 153          $s = &new SWFShape();
 154          $color = $this->allocateColor($color);
 155          $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 156  
 157          if ($fill != 'none') {
 158              $fillColor = $this->allocateColor($fill);
 159              $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
 160              $s->setRightFill($f);
 161          }
 162  
 163          $a = $r * 0.414213562; // = tan(22.5 deg)
 164          $b = $r * 0.707106781; // = sqrt(2)/2 = sin(45 deg)
 165  
 166          $s->movePenTo($x + $r, $y);
 167  
 168          $s->drawCurveTo($x + $r, $y - $a, $x + $b, $y - $b);
 169          $s->drawCurveTo($x + $a, $y - $r, $x, $y - $r);
 170          $s->drawCurveTo($x - $a, $y - $r, $x - $b, $y - $b);
 171          $s->drawCurveTo($x - $r, $y - $a, $x - $r, $y);
 172          $s->drawCurveTo($x - $r, $y + $a, $x - $b, $y + $b);
 173          $s->drawCurveTo($x - $a, $y + $r, $x, $y + $r);
 174          $s->drawCurveTo($x + $a, $y + $r, $x + $b, $y + $b);
 175          $s->drawCurveTo($x + $r, $y + $a, $x + $r, $y);
 176  
 177          return $this->_movie->add($s);
 178      }
 179  
 180      /**
 181       * Draw a polygon based on a set of vertices.
 182       *
 183       * @param array $vertices  An array of x and y labeled arrays
 184       *                         (eg. $vertices[0]['x'], $vertices[0]['y'], ...).
 185       * @param string $color    The color you want to draw the polygon with.
 186       * @param string $fill     The color to fill the polygon.
 187       */
 188      function polygon($verts, $color, $fill = 'none')
 189      {
 190          $color = $this->allocateColor($color);
 191  
 192          if (is_array($color) && is_array($verts) && (sizeof($verts) > 2)) {
 193              $shape = &new SWFShape();
 194              $shape->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 195  
 196              if ($fill != 'none') {
 197                  $fillColor = $this->allocateColor($fill);
 198                  $f = $shape->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
 199                  $shape->setRightFill($f);
 200              }
 201  
 202              $first_done = false;
 203              foreach ($verts as $value) {
 204                  if (!$first_done) {
 205                      $shape->movePenTo($value['x'], $value['y']);
 206                      $first_done = true;
 207                      $first_x = $value['x'];
 208                      $first_y = $value['y'];
 209                  }
 210                  $shape->drawLineTo($value['x'], $value['y']);
 211              }
 212              $shape->drawLineTo($first_x, $first_y);
 213  
 214              return $this->_movie->add($shape);
 215          } else {
 216              // If the color is an array and the vertices is a an array
 217              // of more than 2 points.
 218              return false;
 219          }
 220      }
 221  
 222      /**
 223       * Draw a rectangle.
 224       *
 225       * @param integer $x       The left x-coordinate of the rectangle.
 226       * @param integer $y       The top y-coordinate of the rectangle.
 227       * @param integer $width   The width of the rectangle.
 228       * @param integer $height  The height of the rectangle.
 229       * @param string  $color   The line color of the rectangle.
 230       * @param string  $fill    The color to fill the rectangle.
 231       */
 232      function rectangle($x, $y, $width, $height, $color, $fill = 'none')
 233      {
 234          $verts[0] = array('x' => $x, 'y' => $y);
 235          $verts[1] = array('x' => $x + $width, 'y' => $y);
 236          $verts[2] = array('x' => $x + $width, 'y' => $y + $height);
 237          $verts[3] = array('x' => $x, 'y' => $y + $height);
 238  
 239          return $this->polygon($verts, $color, $fill);
 240      }
 241  
 242      /**
 243       * Draw a rectangle.
 244       *
 245       * @param integer $x       The left x-coordinate of the rectangle.
 246       * @param integer $y       The top y-coordinate of the rectangle.
 247       * @param integer $width   The width of the rectangle.
 248       * @param integer $height  The height of the rectangle.
 249       * @param integer $round   The width of the corner rounding.
 250       * @param string $color    The line color of the rectangle.
 251       * @param string $fill     The color to fill the rectangle.
 252       */
 253      function roundedRectangle($x, $y, $width, $height, $round, $color = 'black', $fill = 'none')
 254      {
 255          if ($round <= 0) {
 256              // Optimize out any calls with no corner rounding.
 257              return $this->rectangle($x, $y, $width, $height, $color, $fill);
 258          }
 259  
 260          $s = &new SWFShape();
 261          $color = $this->allocateColor($color);
 262          $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 263  
 264          if ($fill != 'none') {
 265              $fillColor = $this->allocateColor($fill);
 266              $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
 267              $s->setRightFill($f);
 268          }
 269  
 270          // Set corner points to avoid lots of redundant math.
 271          $x1 = $x + $round;
 272          $y1 = $y + $round;
 273  
 274          $x2 = $x + $width - $round;
 275          $y2 = $y + $round;
 276  
 277          $x3 = $x + $width - $round;
 278          $y3 = $y + $height - $round;
 279  
 280          $x4 = $x + $round;
 281          $y4 = $y + $height - $round;
 282  
 283          // Start in the upper left.
 284          $p1 = $this->_arcPoints($round, 180, 225);
 285          $p2 = $this->_arcPoints($round, 225, 270);
 286  
 287          // Start at the lower left corner of the top left curve.
 288          $s->movePenTo($x1 + $p1['x1'], $y1 + $p1['y1']);
 289  
 290          // Draw the upper left corner.
 291          $s->drawCurveTo($x1 + $p1['x3'], $y1 + $p1['y3'], $x1 + $p1['x2'], $y1 + $p1['y2']);
 292          $s->drawCurveTo($x1 + $p2['x3'], $y1 + $p2['y3'], $x1 + $p2['x2'], $y1 + $p2['y2']);
 293  
 294          // Calculate the upper right points.
 295          $p3 = $this->_arcPoints($round, 270, 315);
 296          $p4 = $this->_arcPoints($round, 315, 360);
 297  
 298          // Connect the top left and right curves.
 299          $s->drawLineTo($x2 + $p3['x1'], $y2 + $p3['y1']);
 300  
 301          // Draw the upper right corner.
 302          $s->drawCurveTo($x2 + $p3['x3'], $y2 + $p3['y3'], $x2 + $p3['x2'], $y2 + $p3['y2']);
 303          $s->drawCurveTo($x2 + $p4['x3'], $y2 + $p4['y3'], $x2 + $p4['x2'], $y2 + $p4['y2']);
 304  
 305          // Calculate the lower right points.
 306          $p5 = $this->_arcPoints($round, 0, 45);
 307          $p6 = $this->_arcPoints($round, 45, 90);
 308  
 309          // Connect the top right and lower right curves.
 310          $s->drawLineTo($x3 + $p5['x1'], $y3 + $p5['y1']);
 311  
 312          // Draw the lower right corner.
 313          $s->drawCurveTo($x3 + $p5['x3'], $y3 + $p5['y3'], $x3 + $p5['x2'], $y3 + $p5['y2']);
 314          $s->drawCurveTo($x3 + $p6['x3'], $y3 + $p6['y3'], $x3 + $p6['x2'], $y3 + $p6['y2']);
 315  
 316          // Calculate the lower left points.
 317          $p7 = $this->_arcPoints($round, 90, 135);
 318          $p8 = $this->_arcPoints($round, 135, 180);
 319  
 320          // Connect the bottom right and bottom left curves.
 321          $s->drawLineTo($x4 + $p7['x1'], $y4 + $p7['y1']);
 322  
 323          // Draw the lower left corner.
 324          $s->drawCurveTo($x4 + $p7['x3'], $y4 + $p7['y3'], $x4 + $p7['x2'], $y4 + $p7['y2']);
 325          $s->drawCurveTo($x4 + $p8['x3'], $y4 + $p8['y3'], $x4 + $p8['x2'], $y4 + $p8['y2']);
 326  
 327          // Close the shape.
 328          $s->drawLineTo($x1 + $p1['x1'], $y1 + $p1['y1']);
 329  
 330          return $this->_movie->add($s);
 331      }
 332  
 333      /**
 334       * Draw a line.
 335       *
 336       * @param integer $x0     The x co-ordinate of the start.
 337       * @param integer $y0     The y co-ordinate of the start.
 338       * @param integer $x1     The x co-ordinate of the end.
 339       * @param integer $y1     The y co-ordinate of the end.
 340       * @param string  $color  The line color.
 341       * @param string  $width  The width of the line.
 342       */
 343      function line($x1, $y1, $x2, $y2, $color = 'black', $width = 1)
 344      {
 345          $color = $this->allocateColor($color);
 346  
 347          if (is_array($color)) {
 348              $shape = &new SWFShape();
 349              $shape->setLine($width, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 350              $shape->movePenTo($x1, $y1);
 351              $shape->drawLineTo($x2, $y2);
 352  
 353              return $this->_movie->add($shape);
 354          } else {
 355              return false;
 356          }
 357      }
 358  
 359      /**
 360       * Draw a dashed line.
 361       *
 362       * @param integer $x0           The x co-ordinate of the start.
 363       * @param integer $y0           The y co-ordinate of the start.
 364       * @param integer $x1           The x co-ordinate of the end.
 365       * @param integer $y1           The y co-ordinate of the end.
 366       * @param string  $color        The line color.
 367       * @param string  $width        The width of the line.
 368       * @param integer $dash_length  The length of a dash on the dashed line
 369       * @param integer $dash_space   The length of a space in the dashed line
 370       */
 371      function dashedLine($x0, $y0, $x1, $y1, $color = 'black', $width = 1, $dash_length = 2, $dash_space = 2)
 372      {
 373          // Get the length of the line in pixels.
 374          $line_length = max(ceil(sqrt(pow(($x1 - $x0), 2) + pow(($y1 - $y0), 2))), 2);
 375  
 376          $cosTheta = ($x1 - $x0) / $line_length;
 377          $sinTheta = ($y1 - $y0) / $line_length;
 378          $lastx = $x0;
 379          $lasty = $y0;
 380  
 381          // Draw the dashed line.
 382          for ($i = 0; $i < $line_length; $i += ($dash_length + $dash_space)) {
 383              $x = ($dash_length * $cosTheta) + $lastx;
 384              $y = ($dash_length * $sinTheta) + $lasty;
 385  
 386              $this->line($lastx, $lasty, $x, $y, $color);
 387  
 388              $lastx = $x + ($dash_space * $cosTheta);
 389              $lasty = $y + ($dash_space * $sinTheta);
 390          }
 391      }
 392  
 393      /**
 394       * Draw a polyline (a non-closed, non-filled polygon) based on a
 395       * set of vertices.
 396       *
 397       * @param array   $vertices  An array of x and y labeled arrays
 398       *                           (eg. $vertices[0]['x'], $vertices[0]['y'], ...).
 399       * @param string  $color     The color you want to draw the line with.
 400       * @param string  $width     The width of the line.
 401       */
 402      function polyline($verts, $color, $width = 1)
 403      {
 404          $color = $this->allocateColor($color);
 405  
 406          $shape = &new SWFShape();
 407          $shape->setLine($width, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 408  
 409          $first_done = false;
 410          foreach ($verts as $value) {
 411              if (!$first_done) {
 412                  $shape->movePenTo($value['x'], $value['y']);
 413                  $first_done = true;
 414              }
 415              $shape->drawLineTo($value['x'], $value['y']);
 416          }
 417  
 418          return $this->_movie->add($shape);
 419      }
 420  
 421      /**
 422       * Draw an arc.
 423       *
 424       * @param integer $x      The x co-ordinate of the centre.
 425       * @param integer $y      The y co-ordinate of the centre.
 426       * @param integer $r      The radius of the arc.
 427       * @param integer $start  The start angle of the arc.
 428       * @param integer $end    The end angle of the arc.
 429       * @param string $color   The line color of the arc.
 430       * @param string $fill    The fill color of the arc.
 431       */
 432      function arc($x, $y, $r, $start, $end, $color = 'black', $fill = 'none')
 433      {
 434          $s = &new SWFShape();
 435          $color = $this->allocateColor($color);
 436          $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 437  
 438          if ($fill != 'none') {
 439              $fillColor = $this->allocateColor($fill);
 440              $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
 441              $s->setRightFill($f);
 442          }
 443  
 444          if ($end - $start <= 45) {
 445              $pts = $this->_arcPoints($r, $start, $end);
 446              $s->movePenTo($x, $y);
 447              $s->drawLineTo($pts['x1'] + $x, $pts['y1'] + $y);
 448              $s->drawCurveTo($pts['x3'] + $x, $pts['y3'] + $y, $pts['x2'] + $x, $pts['y2'] + $y);
 449              $s->drawLineTo($x, $y);
 450          } else {
 451              $sections = ceil(($end - $start) / 45);
 452              for ($i = 0; $i < $sections; $i++) {
 453                  $pts = $this->_arcPoints($r, $start + ($i * 45), ($start + (($i + 1) * 45) > $end)
 454                                           ? $end
 455                                           : ($start + (($i + 1) * 45)));
 456  
 457                  // If we are on the first section, move the pen to the
 458                  // centre and draw out to the edge.
 459                  if ($i == 0 && $fill != 'none') {
 460                      $s->movePenTo($x, $y);
 461                      $s->drawLineTo($pts['x1'] + $x, $pts['y1'] + $y);
 462                  } else {
 463                      $s->movePenTo($pts['x1'] + $x, $pts['y1'] + $y);
 464                  }
 465  
 466                  // Draw the arc.
 467                  $s->drawCurveTo($pts['x3'] + $x, $pts['y3'] + $y, $pts['x2'] + $x, $pts['y2'] + $y);
 468              }
 469  
 470              if ($fill != 'none') {
 471                  // Draw a line from the edge back to the centre to close
 472                  // off the segment.
 473                  $s->drawLineTo($x, $y);
 474              }
 475          }
 476  
 477          return $this->_movie->add($s);
 478      }
 479  
 480      /**
 481       * Draw a rectangle filled with a gradient from $color1 to
 482       * $color2.
 483       *
 484       * @param integer $x       The left x-coordinate of the rectangle.
 485       * @param integer $y       The top y-coordinate of the rectangle.
 486       * @param integer $width   The width of the rectangle.
 487       * @param integer $height  The height of the rectangle.
 488       * @param string  $color   The outline color of the rectangle.
 489       * @param string  $fill1   The name of the start color for the gradient.
 490       * @param string  $fill2   The name of the end color for the gradient.
 491       */
 492      function gradientRectangle($x, $y, $width, $height, $color = 'black', $fill1 = 'black', $fill2 = 'white')
 493      {
 494          $s = &new SWFShape();
 495  
 496          if ($color != 'none') {
 497              $color = $this->allocateColor($color);
 498              $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
 499          }
 500  
 501          $fill1 = $this->allocateColor($fill1);
 502          $fill2 = $this->allocateColor($fill2);
 503          $gradient = &new SWFGradient();
 504          $gradient->addEntry(0.0, $fill1['red'], $fill1['green'], $fill1['blue'], $fill1['alpha']);
 505          $gradient->addEntry(1.0, $fill2['red'], $fill2['green'], $fill2['blue'], $fill2['alpha']);
 506  
 507          $f = $s->addFill($gradient, SWFFILL_LINEAR_GRADIENT);
 508          $f->scaleTo($width / $this->_width);
 509          $f->moveTo($x, $y);
 510          $s->setRightFill($f);
 511  
 512          $verts[0] = array('x' => $x, 'y' => $y);
 513          $verts[1] = array('x' => $x + $width, 'y' => $y);
 514          $verts[2] = array('x' => $x + $width, 'y' => $y + $height);
 515          $verts[3] = array('x' => $x, 'y' => $y + $height);
 516  
 517          $first_done = false;
 518          foreach ($verts as $vert) {
 519              if (!$first_done) {
 520                  $s->movePenTo($vert['x'], $vert['y']);
 521                  $first_done = true;
 522                  $first_x = $vert['x'];
 523                  $first_y = $vert['y'];
 524              }
 525              $s->drawLineTo($vert['x'], $vert['y']);
 526          }
 527          $s->drawLineTo($first_x, $first_y);
 528  
 529          return $this->_movie->add($s);
 530      }
 531  
 532  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7