使用PHP5创建图形巧妙方法(二)

2016-01-29 13:30 7 1 收藏

使用PHP5创建图形巧妙方法(二),使用PHP5创建图形巧妙方法(二)

【 tulaoshi.com - PHP 】

添加维数

  我们的第一个需求 提供图形对象的能力 已经满足了,现在应该开始满足第二个需求了:可以使用一个 z 值将一个对象放到其他对象的上面或下面。

  我们可以将每个 z 值当作是原始图像的一个面。所画的元素是按照 z 值从最小到最大的顺序来画的。例如,让我们画两个图形元素:一个红色的圆和一个黑色的方框。圆的 z 值是 100,而黑方框的 z 值是 200。这样会将圆放到方框之后,如图 3 所示:

  图3. 不同 z 值的面

不同 z 值的面

  我们只需要修改一下 z 值就可以将这个红圆放到黑方框之上。要实现这种功能,我们需要让每个 GraphicsObject 都具有一个 z() 方法,它返回一个数字,就是 z 值。由于您需要创建不同的图形对象(Line、Oval 和 Rectangle),您还需要创建一个基本的类 BoxObject,其他 3 个类都使用它来维护起点和终点的坐标、z 值和这个对象的颜色(请参看图 4)。

  图 4. 给系统添加另外一维:z 值
给系统添加另外一维:z 值

  这个图形库的新代码如清单 3 所示:

  清单 3. 可以处理 z 信息的图形库

 <?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 saveAsPng( $filename )   {     imagepng( $this-gdo, $filename );   } } abstract class GraphicsObject {   abstract public function render( $ge );   abstract public function z(); } abstract class BoxObject extends GraphicsObject {   protected $color;   protected $sx;   protected $sy;   protected $ex;   protected $ey;   protected $z;   public function __construct( $z, $color, $sx, $sy, $ex, $ey )   {     $this-z = $z;     $this-color = $color;     $this-sx = $sx;     $this-sy = $sy;     $this-ex = $ex;     $this-ey = $ey;   }   public function z() { return $this-z; } } class Line extends BoxObject {   public function render( $ge )   {     imageline( $ge-getGraphicObject(),       $this-sx, $this-sy,       $this-ex, $this-ey,       $ge-getColor( $this-color ) );   } } class Rectangle extends BoxObject {   public function render( $ge )   {     imagefilledrectangle( $ge-getGraphicObject(),       $this-sx, $this-sy,       $this-ex, $this-ey,       $ge-getColor( $this-color ) );   } } class Oval extends BoxObject {   public function render( $ge )   {     $w = $this-ex - $this-sx;     $h = $this-ey - $this-sy;     imagefilledellipse( $ge-getGraphicObject(),       $this-sx + ( $w / 2 ),       $this-sy + ( $h / 2 ),       $w, $h,       $ge-getColor( $this-color ) );   } } ? 

  测试代码也需要进行更新,如清单 4 所示。

  清单 4. 更新后的测试代码

来源:http://www.tulaoshi.com/n/20160129/1490391.html

延伸阅读
标签: PHP
/* +-------------------------------------------------------------------------------+ | = 本文为Haohappy读<<Core PHP Programming | = 中Classes and Objects一章的笔记 | = 翻译为主+个人心得 | = 为避免可能发生的不必要的麻烦请勿转载,谢谢 | = 欢迎批评指正,希望和所有PHP爱好者共同进步! +---------------...
标签: PHP
虽然 PHP5 还没有正式发布(开发版本已经提供下载),但我们现在就可以开始体验一下新的版本 将要带给我们的惊喜。在以下的介绍中,我们将重点讲述 PHP5 中的三大特色功能。这三大特点为: * 新的对象模式 (New Object Mode) * 异常处理 (Exceptions) * 名称空间 (Namespace) 在开始之前,要声明两点: * 文章中的例子为了说...
标签: Web开发
/* +-------------------------------------------------------------------------------+ | = 本文为Haohappy读Core PHP Programming | = 中Classes and Objects一章的笔记 | = 翻译为主+个人心得 | = 为避免可能发生的不必要的麻烦请勿转载,谢谢 | = 欢迎批评指正,希望和所有PHP爱好者共同进步! | = ...
标签: PHP
  PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释 <? /************************************************ **              ...
标签: PHP
看了几个的关于web架构方面的获奖作品,感受颇深,xml和php结合应用越来多了,里面几乎所有的作品在设计上都用到了xml这个东西.....-_-! 又落伍了不少. 建议朋友们都去看看. 所以自己这几天在疯狂的学习php和xml结合的相关知识. 这其中就遇到了XML-RPC服务, 在网上可以找到的资料不多, 而且大多数都是用了其他第三方用php开发的XML-RPC class, 而...

经验教程

136

收藏

82
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部