使用PHP5创建图形的巧妙方法四

2016-01-29 13:29 2 1 收藏

使用PHP5创建图形的巧妙方法四,使用PHP5创建图形的巧妙方法四

【 tulaoshi.com - PHP 】

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

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

具有灵活 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。对象会被重新放置,以适合这个新的坐标系统。

  测试代码的输出如图 9 所示。

  图 9. viewport 绘制的图像转换为一个 400X400 的图像

viewport 绘制的图像转换为一个 400X400 的图像

  如果您希望图像的大小是 400X200,就可以采用下面的方法:

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

  您会得到一个纵向缩

来源:https://www.tulaoshi.com/n/20160129/1490368.html

延伸阅读
标签: PHP
抽象类 抽象类不能被实例化。 抽象类与其它类一样,允许定义变量及方法。 抽象类同样可以定义一个抽象的方法,抽象类的方法不会被执行,不过将有可能会在其派生类中执行。 例六:抽象类 <?php abstract class foo { protected $x; abstract function display(); function setX($x) { $this-x = $x; } } class foo2 extend...
标签: PHP
看了几个的关于web架构方面的获奖作品,感受颇深,xml和php结合应用越来多了,里面几乎所有的作品在设计上都用到了xml这个东西.....-_-! 又落伍了不少. 建议朋友们都去看看. 所以自己这几天在疯狂的学习php和xml结合的相关知识. 这其中就遇到了XML-RPC服务, 在网上可以找到的资料不多, 而且大多数都是用了其他第三方用php开发的XML-RPC class, 而...
标签: PHP
/* ------------------------------------------------------------------------------- | = 本文为Haohappy读<<Core PHP Programming | = 中Classes and Objects一章的笔记 | = 翻译为主 个人心得 | = 为避免可能发生的不必要的麻烦请勿转载,谢谢 | = 欢迎批评指正,希望和所有PHP爱好者共同进步! | = PHP5研究中心:...
标签: PHP
  PHP5中增强了XML的支持,使用DOM扩展了XML操作的能耐。这些函数作为 PHP5 核心的一部分,无需被安装即可使用。 下面的例子简单的演示了DOM对XML的操作,详细解释请看代码中的注释 <? /************************************************ **              ...
标签: PHP
PDO(PHP Data Object) 是PHP 5新出来的东西,在PHP 6都要出来的时候,PHP 6只默认使用PDO来处理数据库,将把所有的数据库扩展移到了PECL,那么默认就是没有了我们喜爱的php_mysql.dll之类的了,那怎么办捏,我们只有与时俱进了,我就小试了一把PDO。(本文只是入门级的,高手可以略过,呵呵) 【PDO是啥】 PDO是PHP 5新加入的一个重...