手机版

在PHP 5.0中创建图形的实用方法 整篇文章的1/3页

时间:2021-11-26 来源:互联网 编辑:宝哥软件园 浏览:

本文将展示如何使用PHP构建一个面向对象的图形层。使用面向对象的系统可以用来构建复杂的图形,这比使用标准PHP库提供的基本功能来构建图形要简单得多。我把图形编辑程序分为两类:一类是绘图程序,可以逐像素绘制图像;另一个是绘图程序,它提供了一组对象,如直线、椭圆和矩形,您可以使用它们组合成一个大图像,如JPEG。绘图程序非常适合像素级控制。但是对于商业图形来说,画图程序是一种更好的方式,因为大多数图形是由矩形、直线和椭圆组成的。PHP内置的基本绘图操作与绘图程序非常相似。它们在绘制图像方面非常强大。但是,如果您希望您的图像是对象的集合,这是不合适的。本文将向您展示如何基于PHP图形库构建一个面向对象的图形库。您将使用PHP V5中提供的面向对象扩展。有了面向对象的图形支持,您的图形代码非常容易理解和维护。您可能还需要将单个图形源中的图形合成到多种类型的媒体中:Flash电影、SVG等。目标创建图形对象库包括三个主要目标:从基本操作切换到对象。它不使用imageLine、imagefilledRectangle等图形功能。库应该提供一些对象,比如线,矩形,椭圆形,可以用来制作图像。它还应该支持构建更大的复杂对象或组对象的能力。一个可以排序z值的绘图程序允许艺术家在屏幕上上下移动图形对象。这个库应该能够支持将一个对象放在其他对象前面和后面的功能:它使用z值来定义对象离绘图平面的高度。z值越大,对象绘制得越晚,它将出现在那些z值较小的对象上。为了提供视口的变换,通常数据的坐标空间不同于图像的坐标空间。PHP中图形的基本操作是操作图像的坐标平面。这个图形库应该支持视口的规范,这样就可以在程序员熟悉的坐标系中指定图形,并自动缩放以适应任何图像大小。因为这里有很多特性,所以您将一步一步地编写代码,以展示代码如何不断增加其功能。基础知识,我们先来看一个图形环境对象和一个名为GraphicsObject的接口,它是用Line类实现的,它的功能是画线。UML如图1所示。图1。图形环境和图形对象接口。

图形对象和一组颜色(包括宽度和高度)存储在GraphicsEnvironment类中。SaveAsPng方法负责将当前图像输出到指定的文件。GraphicsObject是任何图形对象都必须使用的接口。要开始使用这个接口,您所需要做的就是使用render方法绘制这个对象。它由Line类实现,该类使用四个坐标:开始和结束处的x值,以及开始和结束处的y值。它也有颜色。当调用渲染时,这个对象用sx,sy到ex,ey的名称指定的颜色绘制一条线。这个库的代码如清单1所示。清单1。基本图形库?php类GraphicsEnvironment { public $ width;公共$ height公共$ gdopublic $ colors=array();public function _ _ construct($ width,$ height){ $ this-width=$ width;$ this-height=$ height;$ this-gdo=imagecreatetrue color($ width,$ height);$this-addColor('white ',255,255,255);imagefiledrectangle($ this-gdo,0,0,$width,$height,$ this-getColor(' white ');} public function width(){ return $ this-width;} public function height(){ return $ this-height;}公共函数addColor($name,$r,$g,$ b){ $ this-colors[$ name]=imagecolor allocate($ this-gdo,$r,$g,$ b);}公共函数GetGraphiCobject(){ return $ this-gdo;}公共函数GetColor($ name){ return $ this-colors[$ name];} public function SaveAspen($ filename){ image png($ this-gdo,$ filename);} }抽象类GraphicsObject {抽象公共函数render($ ge);Line类扩展了GraphicsObject { private $ colorprivate $ sx二等兵$ sy私人$ ex二等兵$ eypublic function _ _ construct($ color,$sx,$sy,$ex,$ ey){ $ this-color=$ color;$ this-sx=$ sx;$ this-sy=$ sy;$ this-ex=$ ex;$ this-ey=$ ey;} public function render($ ge){ imageline($ ge-getGraphicObject(),$this-sx,$this-sy,$this-ex,$this-ey,$ ge-getColor($ this-color));} } ?测试代码如清单2所示:清单2。基本图形库的测试代码?PHP require _ once(' glib . PHP ');$ge=新的GraphicsEnvironment(400,400);$ge-addColor('black ',0,0,0);$ge-addColor('red ',255,0,0);$ge-addColor('绿色',0,255,0);$ge-addColor('blue ',0,0,255);$ gobjs=array();$gobjs []=new Line('black ',10,5,100,200);$gobjs []=new Line('blue ',200,150,390,380);$gobjs []=new Line('red ',60,40,10,300);$gobjs []=new Line('green ',5,390,390,10);foreach($ gobjs as $ gobj){ $ gobj-render($ ge);} $ ge-SaveAspen(' test . png ');这个测试程序创建了一个图形环境。然后创建几条线,它们指向不同的方向,有不同的颜色。然后,渲染方法可以在图形平面上绘制它们。最后,此代码将此图像保存为test.png。在本文中,使用以下命令行解释器来运行这段代码,如下所示:% PHP test.php%图2显示了在Firefox中生成的test.php文件的外观。图2。简单图形对象测试简单的图形对象测试

这个当然没有蒙娜丽莎漂亮,但是可以满足现在工作的需要。[下一页]我们添加维度的第一个要求,——,提供图形对象的能力,——,已经满足。现在是时候满足第二个要求了:您可以使用z值将一个对象放置在其他对象的上面或下面。我们可以将每个z值视为原始图像的一个面。元素按z值从最小到最大的顺序绘制。例如,让我们画两个图形元素:一个红色圆圈和一个黑色方框。圆的z值是100,黑盒的z值是200。这将把圆圈放在盒子后面,如图3所示:图3。Z值不同的脸不同 z  值的面

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

这个图形库的新代码如清单3所示:清单3.可以处理z信息的图形库?服务器端编程语言(Professional Hypertext Preprocessor的缩写)类graphics environment { public $ width;公共$高度公共$ gdoppublic $ colors=array();public function _ _ construct($ width,$ height){ $ this-width=$ width;$ this-height=$ height;$ this-gdo=imagecreatetrue color($ width,$ height);$this-addColor('white ',255,255,255);imagefiledrectangle($ this-gdo,0,0,$width,$height,$ this-getColor(' white ');} public function width(){ return $ this-width;} public function height(){ return $ this-height;}公共函数addColor($name,$r,$g,$ b){ $ this-colors[$ name]=imagecolor allocate($ this-gdo,$r,$g,$ b);}公共函数getgraphicobeject(){ return $ this-gdo;}公共函数GetColor($ name){ return $ this-colors[$ name];}公共函数SaveAspen($ filename){ image png($ this-gdo,$ filename);} }抽象类GraphicsObject {抽象公共函数render($ ge);抽象公共函数z();}抽象类BoxObject扩展了graphics object { protected $ color;受保护的$ sx受保护的$ sy受保护的$ ex受保护的$ ey受保护的$ 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;} }类线条扩展了box object { public function render($ ge){ imageline($ ge-getgraphicobobject(),$this-sx,$this-sy,$this-ex,$this-ey,$ ge-getColor($ this-color));} }类矩形扩展了box object { public function render($ ge){ imagefiledrectangle($ ge-getgraphicobeject(),$this-sx,$this-sy,$this-ex,$this-ey,$ ge-getColor($ this-color));} }类卵形的扩展了BoxObject { public function render($ ge){ $ w=$ this-ex-$ this-sx;$ h=$ this-ey-$ this-sy;imagefildellipse($ ge-getGraphicObject(),$this-sx ($w/2),$this-sy ($h/2),$w,$h,$ ge-getColor($ this-color));} } ?测试代码也需要进行更新,如清单四所示。清单4.更新后的测试代码?PHP需要_ once(' glib。PHP’);函数zsort($a,$b ) { if ($a-z() $b-z())返回-1;if ($a-z() $b-z())返回1;返回0;} $ ge=新的图形环境(400,400);$ge-addColor('black ',0,0,0);$ge-addColor('red ',255,0,0);$ge-addColor('绿色',0,255,0);$ge-addColor('blue ',0,0,255);$ gobjs=array();$gobjs []=新椭圆形(100 ')红色',50,50,150,150);$gobjs []=新矩形(200,‘黑色’,100,100,300,300);usort($gobjs,' z sort ');foreach($ gobjs as $ gobj){ $ gobj-render($ ge);} $ ge-SaveAspen('测试。png ');此处需要注意两件事情。首先是我们添加了创建卵形的和矩形对象的过程,其中第一个参数是z值。其次是调用了乌索特,它使用了zsort函数来对图形对象根据z值进行排序。 123下一页阅读全文

版权声明:在PHP 5.0中创建图形的实用方法 整篇文章的1/3页是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。