当前位置: 首页 > 图文教程 > 网络编程 > PHP > 使用PHP5创建图形的巧妙方法四

PHP
用PHP实现Ftp用户的在线管理
用PHP实现分段下载
最令PHP初学者头痛的十四个问题
用PHP写的MD5加密函数
PHP应用程序加速探索之简介
将SSH与PHP相连接 确保传输数据的安全
PHP制作的仿百度的站内搜索引擎代码
PHP读取汉字点阵数据
PHP实现任意字符集下正常显示网页的方法
利用PHP的OOP特性实现数据保护
关于PHP字符集的问题
新手入门:IIS6环境下的PHP最佳配置方法
新手入门:初学动态网页PHP的18个例子
基于PHP的AJAX技术实现文件异步上传
PHP技巧--通过COM使用ADODB
PHP技巧:正确理解PHP程序编译时的错误信息
PHP技巧:分析利用PHP制作新闻系统的步骤
PHP技巧:通过实例深入剖析require和include的用法
PHP技巧:优化动态网页技术PHP程序的12条技巧
PHP技巧:使用APC缓存优化PHP程序

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 30 ::
收藏到网摘: n/a

图 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 ); 

  您会得到一个纵向缩