使用 PHP 5.0创建图形的巧妙方法 (2)

时间:2010-01-13 08:36:12   来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:  public $gdo;    public $colors = array();       public function __construct( $width, $height )    {    $this->width = $width;    $this->height = $height;    $this->gdo = imagecreatetruecolor( $width, $height );    $t...
  正文:

 清单 1. 基础的图形库
  
  
  <?PHP
  class GraphicsEnvironment
  {
   public $width;
   public $height;
   public $gdo;
   public $colors = array();
  
   public function __construct( $width, $height )
   {
   $this->width = $width;
   $this->height = $height;
   $this->gdo = imagecreatetruecolor( $width, $height );
   $this->addColor( "white", 255, 255, 255 );
   imagefilledrectangle( $this->gdo, 0, 0,
   $width, $height,
   $this->getColor( "white" ) );
   }
  
   public function width() { return $this->width; }
  
   public function height() { return $this->height; }
  
   public function addColor( $name, $r, $g, $b )
   {
   $this->colors[ $name ] = imagecolorallocate(
   $this->gdo,
   $r, $g, $b );
   }
  
   public function getGraphicObject()
   {
   return $this->gdo;
   }
  
   public function getColor( $name )
   {
   return $this->colors[ $name ];
   }
  
   public function saveASP/ target=_blank class=infotextkey>aspng( $filename )
   {
   imagepng( $this->gdo, $filename );
   }
  }
  
  abstract class GraphicsObject
  {
   abstract public function render( $ge );
  }
  
  class Line extends GraphicsObject
  {
   private $color;
   private $sx;
   private $sy;
   private $ex;
   private $ey;
  
   public function __construct( $color, $sx, $sy, $ex, $ey )
   {
   $this->color = $color;
   $this->sx = $sx;
   $this->sy = $sy;
   $this->ex = $ex;
   $this->ey = $ey;
   }
  
   public function render( $ge )
   {
   imageline( $ge->getGraphicObject(),
   $this->sx, $this->sy,
   $this->ex, $this->ey,
   $ge->getColor( $this->color ) );
   }
  }
  ?>
  
  
  
    测试代码如清单 2 所示:
  
    清单 2. 基础图形库的测试代码
  
  
  <?php
  require_once( "glib.php" );
  
  $ge = new GraphicsEnvironment( 400, 400 );
  
  $ge->addColor( "black", 0, 0, 0 );
  $ge->addColor( "red", 255, 0, 0 );
  $ge->addColor( "green", 0, 255, 0 );
  $ge->addColor( "blue", 0, 0, 255 );
  
  $gobjs = array();
  $gobjs []= new Line( "black", 10, 5, 100, 200 );
  $gobjs []= new Line( "blue", 200, 150, 390, 380 );
  $gobjs []= new Line( "red", 60, 40, 10, 300 );
  $gobjs []= new Line( "green", 5, 390, 390, 10 );
  
  foreach( $gobjs as $gobj ) { $gobj->render( $ge ); }
来源:http://www.002pc.com/master/College/Programming/PHP/11808.html

收藏到:

关于《使用 PHP 5.0创建图形的巧妙方法 (2)》文章的评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面

随机文章

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by rand() limit 10

顶出来的热门

    SQL Error: select * from ***_ecms_article where classid='86' and checked=1 order by diggtop desc,id desc limit 10
站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td