linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
使用 PHP 5.0创建图形的巧妙方法(3)
[ 作者:  加入时间:2008-02-13 11:26:43  来自:Linux联盟收集整理 ]
添加维数 qrBLinux联盟
   qrBLinux联盟
    我们的第一个需求 —— 提供图形对象的能力 —— 已经满足了,现在应该开始满足第二个需求了:可以使用一个 z 值将一个对象放到其他对象的上面或下面。 qrBLinux联盟
   qrBLinux联盟
    我们可以将每个 z 值当作是原始图像的一个面。所画的元素是按照 z 值从最小到最大的顺序来画的。例如,让我们画两个图形元素:一个红色的圆和一个黑色的方框。圆的 z 值是 100,而黑方框的 z 值是 200。这样会将圆放到方框之后,如图 3 所示: qrBLinux联盟
   qrBLinux联盟
  我们只需要修改一下 z 值就可以将这个红圆放到黑方框之上。要实现这种功能,我们需要让每个 GraphicsObject 都具有一个 z() 方法,它返回一个数字,就是 z 值。由于您需要创建不同的图形对象(Line、Oval 和 Rectangle),您还需要创建一个基本的类 BoxObject,其他 3 个类都使用它来维护起点和终点的坐标、z 值和这个对象的颜色(请参看图 4)。 qrBLinux联盟
  %20  这个图形库的新代码如清单%203%20所示:%20  %20    清单%203.%20可以处理%20z%20信息的图形库%20  %20  %20  <?PHP%20  class%20GraphicsEnvironment%20  {%20  %20public%20$width;%20  %20public%20$height;%20  %20public%20$gdo;%20  %20public%20$colors%20=%20array();%20  %20  %20public%20function%20__construct(%20$width,%20$height%20)%20  %20{%20  %20$this->width%20=%20$width;%20  %20$this->height%20=%20$height;%20  %20$this->gdo%20=%20imagecreatetruecolor(%20$width,%20$height%20);%20  %20$this->addColor(%20"white",%20255,%20255,%20255%20);%20  %20imagefilledrectangle(%20$this->gdo,%200,%200,%20  %20$width,%20$height,%20  %20$this->getColor(%20"white"%20)%20);%20  %20}%20  %20  %20public%20function%20width()%20{%20return%20$this->width;%20}%20  %20  %20public%20function%20height()%20{%20return%20$this->height;%20}%20  %20  %20public%20function%20addColor(%20$name,%20$r,%20$g,%20$b%20)%20  %20{%20  %20$this->colors[%20$name%20]%20=%20imagecolorallocate(%20  %20$this->gdo,%20  %20$r,%20$g,%20$b%20);%20  %20}%20  %20  %20public%20function%20getGraphicObject()%20  %20{%20  %20return%20$this->gdo;%20  %20}%20  %20  %20public%20function%20getColor(%20$name%20)%20  %20{%20  %20return%20$this->colors[%20$name%20];%20  %20}%20  %20  %20public%20function%20saveASPng( $filename ) qrBLinux联盟
   { qrBLinux联盟
   imagepng( $this->gdo, $filename ); qrBLinux联盟
   } qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  abstract class GraphicsObject qrBLinux联盟
  { qrBLinux联盟
   abstract public function render( $ge ); qrBLinux联盟
   abstract public function z(); qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  abstract class BoxObject extends GraphicsObject qrBLinux联盟
  { qrBLinux联盟
   protected $color; qrBLinux联盟
   protected $sx; qrBLinux联盟
   protected $sy; qrBLinux联盟
   protected $ex; qrBLinux联盟
   protected $ey; qrBLinux联盟
   protected $z; qrBLinux联盟
   qrBLinux联盟
   public function __construct( $z, $color, $sx, $sy, $ex, $ey ) qrBLinux联盟
   { qrBLinux联盟
   $this->z = $z; qrBLinux联盟
   $this->color = $color; qrBLinux联盟
   $this->sx = $sx; qrBLinux联盟
   $this->sy = $sy; qrBLinux联盟
   $this->ex = $ex; qrBLinux联盟
   $this->ey = $ey; qrBLinux联盟
   } qrBLinux联盟
   qrBLinux联盟
   public function z() { return $this->z; } qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  class Line extends BoxObject qrBLinux联盟
  { qrBLinux联盟
   public function render( $ge ) qrBLinux联盟
   { qrBLinux联盟
   imageline( $ge->getGraphicObject(), qrBLinux联盟
   $this->sx, $this->sy, qrBLinux联盟
   $this->ex, $this->ey, qrBLinux联盟
   $ge->getColor( $this->color ) ); qrBLinux联盟
   } qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  class Rectangle extends BoxObject qrBLinux联盟
  { qrBLinux联盟
   public function render( $ge ) qrBLinux联盟
   { qrBLinux联盟
   imagefilledrectangle( $ge->getGraphicObject(), qrBLinux联盟
   $this->sx, $this->sy, qrBLinux联盟
   $this->ex, $this->ey, qrBLinux联盟
   $ge->getColor( $this->color ) ); qrBLinux联盟
   } qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  class Oval extends BoxObject qrBLinux联盟
  { qrBLinux联盟
   public function render( $ge ) qrBLinux联盟
   { qrBLinux联盟
   $w = $this->ex - $this->sx; qrBLinux联盟
   $h = $this->ey - $this->sy; qrBLinux联盟
   imagefilledellipse( $ge->getGraphicObject(), qrBLinux联盟
   $this->sx + ( $w / 2 ), qrBLinux联盟
   $this->sy + ( $h / 2 ), qrBLinux联盟
   $w, $h, qrBLinux联盟
   $ge->getColor( $this->color ) ); qrBLinux联盟
   } qrBLinux联盟
  } qrBLinux联盟
  ?> qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
    测试代码也需要进行更新,如清单 4 所示。 qrBLinux联盟
   qrBLinux联盟
    清单 4. 更新后的测试代码 qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  <?php qrBLinux联盟
  require_once( "glib.php" ); qrBLinux联盟
   qrBLinux联盟
  function zsort( $a, $b ) qrBLinux联盟
  { qrBLinux联盟
   if ( $a->z() < $b->z() ) return -1; qrBLinux联盟
   if ( $a->z() > $b->z() ) return 1; qrBLinux联盟
   return 0; qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  $ge = new GraphicsEnvironment( 400, 400 ); qrBLinux联盟
   qrBLinux联盟
  $ge->addColor( "black", 0, 0, 0 ); qrBLinux联盟
  $ge->addColor( "red", 255, 0, 0 ); qrBLinux联盟
  $ge->addColor( "green", 0, 255, 0 ); qrBLinux联盟
  $ge->addColor( "blue", 0, 0, 255 ); qrBLinux联盟
   qrBLinux联盟
  $gobjs = array(); qrBLinux联盟
  $gobjs []= new Oval( 100, "red", 50, 50, 150, 150 ); qrBLinux联盟
  $gobjs []= new Rectangle( 200, "black", 100, 100, 300, 300 ); qrBLinux联盟
   qrBLinux联盟
  usort( $gobjs, "zsort" ); qrBLinux联盟
   qrBLinux联盟
  foreach( $gobjs as $gobj ) { $gobj->render( $ge ); } qrBLinux联盟
   qrBLinux联盟
  $ge->saveAsPng( "test.png" ); qrBLinux联盟
  ?> qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
    此处需要注意两件事情。首先是我们添加了创建 Oval 和 Rectangle 对象的过程,其中第一个参数是 z 值。其次是调用了 usort,它使用了 zsort 函数来对图形对象根据 z 值进行排序。 qrBLinux联盟
   qrBLinux联盟
    在运行这个程序时,test.png 文件应该如图 5 所示。 qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  现在修改下面的代码: qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  $gobjs []= new Oval( 200, "red", 50, 50, 150, 150 ); qrBLinux联盟
  $gobjs []= new Rectangle( 100, "black", 100, 100, 300, 300 ); qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
    再次运行这个代码,突然这个椭圆就在这个方框上面了,如图 6 所示。 qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  红圆现在就出现在黑方框上面了,尽管它是先创建的,也是首先添加到数组中的。这就是 z 值的实际价值:您可以按照任何顺序来创建对象,并可以通过调整每个对象的 z 值来调整彼此之间的相对位置。 qrBLinux联盟
   qrBLinux联盟
    在这段代码中,z 值排序是在这个库之外实现的。让我们通过创建一个新容器对象 Group 来实现这种功能,其中保存了一组 GraphicsObject 对象。Group 对象然后再处理排序的问题。 qrBLinux联盟
   qrBLinux联盟
    Group 类的代码如清单 5 所示。 qrBLinux联盟
   qrBLinux联盟
    清单 5. Group 类 qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  function zsort( $a, $b ) qrBLinux联盟
  { qrBLinux联盟
   if ( $a->z() < $b->z() ) return -1; qrBLinux联盟
   if ( $a->z() > $b->z() ) return 1; qrBLinux联盟
   return 0; qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
  class Group extends GraphicsObject qrBLinux联盟
  { qrBLinux联盟
   private $z; qrBLinux联盟
   protected $members = array(); qrBLinux联盟
   public function __construct( $z ) qrBLinux联盟
   { qrBLinux联盟
   $this->z = $z; qrBLinux联盟
   } qrBLinux联盟
   public function add( $member ) qrBLinux联盟
   { qrBLinux联盟
   $this->members []= $member; qrBLinux联盟
   } qrBLinux联盟
   public function render( $ge ) qrBLinux联盟
   { qrBLinux联盟
   usort( $this->members, "zsort" ); qrBLinux联盟
   foreach( $this->members as $gobj ) qrBLinux联盟
   { qrBLinux联盟
   $gobj->render( $ge ); qrBLinux联盟
   } qrBLinux联盟
   } qrBLinux联盟
   public function z() { return $this->z; } qrBLinux联盟
  } qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
    Group 对象的任务是保持一个对象数组,然后在画图时,逐个对对象zo进行排序和画图。 qrBLinux联盟
   qrBLinux联盟
    更新后的测试代码如清单 6 所示。 qrBLinux联盟
   qrBLinux联盟
    清单 6. 更新后的测试代码 qrBLinux联盟
   qrBLinux联盟
   qrBLinux联盟
  <?php qrBLinux联盟
  require_once( "glib.php" ); qrBLinux联盟
   qrBLinux联盟
  $ge = new GraphicsEnvironment( 400, 400 ); qrBLinux联盟
   qrBLinux联盟
  $ge->addColor( "black", 0, 0, 0 ); qrBLinux联盟
  $ge->addColor( "red", 255, 0, 0 ); qrBLinux联盟
  $ge->addColor( "green", 0, 255, 0 ); qrBLinux联盟
  $ge->addColor( "blue", 0, 0, 255 ); qrBLinux联盟
   qrBLinux联盟
  $g1 = new Group( 0 ); qrBLinux联盟
  $g1->add( new Oval( 200, "red", 50, 50, 150, 150 ) ); qrBLinux联盟
  $g1->add( new Rectangle( 100, "black", 100, 100, 300, 300 ) ); qrBLinux联盟
   qrBLinux联盟
  $g1->render( $ge ); qrBLinux联盟
   qrBLinux联盟
  $ge->saveAsPng( "test.png" ); qrBLinux联盟
  ?> qrBLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·使用 PHP 5.0创建图形的巧妙方法 (2)  (2008-02-13 11:25:14)
 ·使用 PHP 5.0创建图形的巧妙方法 (1)  (2008-02-13 11:23:14)
 ·PHP 5.0对象模型深度探索之类的静态成员  (2008-02-05 10:28:11)
 ·PHP 5.0对象模型深度探索之属性和方法  (2008-01-28 14:24:06)
 ·PHP 5.0对象模型深度探索之定义一个类  (2008-01-28 14:23:15)
 ·Linux下Apache 2.2, MySQL 5.0和PHP 5.0的安装与配置  (2007-01-22 02:09:29)
 ·PHP 5.0中多态性的实现方案浅析  (2007-01-22 01:39:52)
 ·PHP 5.0中多态性的实现方案  (2006-06-09 11:44:16)
 ·PHP 5.0对象模型深度探索之绑定  (2006-06-08 22:39:05)