中国站长下载-为中国站长提供最好最全的建站资源! 首 页发布资源有事留言繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:中国站长下载 -> 文章中心 -> 网页编程 -> PHP编程 -> 文章内容  虚拟主机 域名注册 退出登录 用户管理
栏目导航
· ASP编程 · .NET编程
· PHP编程 · JSP编程
· CGI 专区
热门文章
· sndvol32 - sndvol3...
· [组图] FLASH:《大话李白》...
· 个人网站到底能赚多...
· [图文] Rundll.exe是病毒吗...
· [组图] Flash:制作MV
· 价值12万元的网站SE...
· 网站创业者,你需要...
· 一个成功的网站设计...
· [图文] FLASH:韩国导航条解...
· 中国网站的赚钱模式...
相关文章
· [图文] 使用 PHP 5.0创建图...
· [图文] 使用 PHP 5.0创建图...
· [组图] 使用 PHP 5.0创建图...
· [图文] 使用 PHP 5.0创建图...
· 使用 php4 加速 web...
使用 PHP 5.0创建图形的巧妙方法(5)
作者:不详  来源:不详  发布时间:2006-8-14 10:59:13  发布人:chinazhan

 减小字体 增大字体

     图 8 显示了更新后的 GraphicsEnvironment 类,它具有几个成员变量,用来存储 viewport 的起点和终点坐标:vsx,vsy 和 vex,vey。图形对象并不需要进行修改。
  
  
  清单 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。对象会被重新放置,以适合这个新的坐标系统。
  
    测试代码的输出如图 9 所示。
  
  如果您希望图像的大小是 400X200,就可以采用下面的方法:
  
  
  $ge = new GraphicsEnvironment( 400, 200,
   -1000, -1000, 1000, 1000 );
  
  
  
    您会得到一个纵向缩小后的图像,如图 10 所示。
  
  
  这展示了代码如何自动调整图像的大小来适合所请求的图像。
  
    结束语
  
    动态图可以为应用程序添加一个新的交互层。使用这种面向对象的系统可以让构建复杂图形变得非常简单,比使用标准的 PHP 库中的基本操作来画图更加简单。另外,您还可以实现画不同大小或类型的图像,并且可以长期使用相同的代码来画不同类型的媒介,例如 SVG、PDF、Flash 和其他类型的媒介。
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。

 
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
中国站长下载
中国站长下载

本页只接受PR>=4 IT类站点连接,申请连接,谢谢您们的支持!希望我们的下载站能够真正帮到中国的站长们!
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright © 2005-2006 ChinaZhan.Net. All Rights Reserved .