正 文

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


www.7dspace.com  更新日期:2006-1-13 1:25:50  七度空间


  图 8 显示了更新后的 GraphicsEnvironment 类,它具有几个成员变量,用来存储 viewport 的起点和终点坐标:vsx,vsy 和 vex,vey。图形对象并不需要进行修改。

  图 8. 具有灵活 viewport 规范的图形环境

  清单 9 显示了更新后的 GraphicsEnvironment 代码。

  清单 9. 更新后的 GraphicsEnvironment 代码

class GraphicsEnvironment
{
  public $vsx;
  public $vsy;
  public $vex;
  public $vey;
  public $width;
  public $height;
  public $gdo;
  public $colors = array(); 

  public function __construct( $width, $height,
    $vsx, $vsy, $vex, $vey )
  {
    $this->vsx = $vsx;
    $this->vsy = $vsy;
    $this->vex = $vex;
    $this->vey = $vey;
    $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 saveAsPng( $filename )
  {
    imagepng( $this->gdo, $filename );
  }
  
  public function tx( $x )
  {
    $r = $this->width / ( $this->vex - $this->vsx );
    return ( $x - $this->vsx ) * $r;
  } 
    
  public function ty( $y )
  {
    $r = $this->height / ( $this->vey - $this->vsy );
    return ( $y - $this->vsy ) * $r;
  }
}

  现在这个构造函数可以利用另外 4 个参数了,它们分别是 viewport 的起点和终点。 tx 和 ty 函数使用新的 viewport 坐标,并将 viewport 坐标转换成物理坐标。

  测试代码如清单 10 所示。

  清单 10. viewport 测试代码

<?php
require_once( "glib.php" );

$ge = new GraphicsEnvironment( 400, 400,
   -1000, -1000, 1000, 1000 );

$ge->addColor( "black", 0, 0, 0 );
$ge->addColor( "red", 255, 0, 0 );
$ge->addColor( "green", 0, 255, 0 );
$ge->addColor( "blue", 0, 0, 255 );

$g1 = new Group( 0 );
$g1->add( new Oval( 200, "red", -800, -800, 0, 0 ) );  
$g1->add( new Rectangle( 100, "black", -400, -400, 900, 900 ) );

$g1->render( $ge );

$ge->saveAsPng( "test.png" );
?>

  这段测试代码会在 -1000,-1000 与 1000,000 之间创建一个 viewport。对象会被重新放置,以适合这个新的坐标系统。

8页,页码:[1] [2] [3] [4] [5] [6] [7] [8] 

上一篇:把你的摄像头架到网上去
下一篇:照片处理:钻戒金属质感完美修复
标题:使用PHP 5.0创建图形的巧妙方法 作者:Jack Herrington 来源:ibm ( 责任编辑:7dspace )
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐