当前位置: 首页 > 图文教程 > 网络编程 > PHP > 使用 PHP 5.0创建图形的巧妙方法
<?phpclassGraphicsEnvironment{public$width;public$height;public$gdo;public$colors=array();publicfunction__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"));}publicfunctionwidth(){return$this->width;}publicfunctionheight(){return$this->height;}publicfunctionaddColor($name,$r,$g,$b){$this->colors[$name]=imagecolorallocate($this->gdo,$r,$g,$b);}publicfunctiongetGraphicObject(){return$this->gdo;}publicfunctiongetColor($name){return$this->colors[$name];}publicfunctionsaveAsPng($filename){imagepng($this->gdo,$filename);}}abstractclassGraphicsObject{abstractpublicfunctionrender($ge);}classLineextendsGraphicsObject{private$color;private$sx;private$sy;private$ex;private$ey;publicfunction__construct($color,$sx,$sy,$ex,$ey){$this->color=$color;$this->sx=$sx;$this->sy=$sy;$this->ex=$ex;$this->ey=$ey;}publicfunctionrender($ge){imageline($ge->getGraphicObject(),$this->sx,$this->sy,$this->ex,$this->ey,$ge->getColor($this->color));}}?><?phprequire_once("glib.php");$ge=newGraphicsEnvironment(400,400);$ge->addColor("black",0,0,0);$ge->addColor("red",255,0,0);$ge->addColor("green",0,255,0);$ge->addColor("blue",0,0,255);$gobjs=array();$gobjs[]=newLine("black",10,5,100,200);$gobjs[]=newLine("blue",200,150,390,380);$gobjs[]=newLine("red",60,40,10,300);$gobjs[]=newLine("green",5,390,390,10);foreach($gobjsas$gobj){$gobj->render($ge);}$ge->saveAsPng("test.png");?>%phptest.php%
图2显示了所生成的test.png文件在Firefox中的样子。
图2.简单的图形对象测试
这当然不如蒙娜丽莎漂亮,但是可以满足目前的工作需要。
<?phpclassGraphicsEnvironment{public$width;public$height;public$gdo;public$colors=array();publicfunction__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"));}publicfunctionwidth(){return$this->width;}publicfunctionheight(){return$this->height;}publicfunctionaddColor($name,$r,$g,$b){$this->colors[$name]=imagecolorallocate($this->gdo,$r,$g,$b);}publicfunctiongetGraphicObject(){return$this->gdo;}publicfunctiongetColor($name){return$this->colors[$name];}publicfunctionsaveAsPng($filename){imagepng($this->gdo,$filename);}}abstractclassGraphicsObject{abstractpublicfunctionrender($ge);abstractpublicfunctionz();}abstractclassBoxObjectextendsGraphicsObject{protected$color;protected$sx;protected$sy;protected$ex;protected$ey;protected$z;publicfunction__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;}publicfunctionz(){return$this->z;}}classLineextendsBoxObject{publicfunctionrender($ge){imageline($ge->getGraphicObject(),$this->sx,$this->sy,$this->ex,$this->ey,$ge->getColor($this->color));}}classRectangleextendsBoxObject{publicfunctionrender($ge){imagefilledrectangle($ge->getGraphicObject(),$this->sx,$this->sy,$this->ex,$this->ey,$ge->getColor($this->color));}}classOvalextendsBoxObject{publicfunctionrender($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));}}?><?phprequire_once("glib.php");functionzsort($a,$b){if($a->z()<$b->z())return-1;if($a->z()>$b->z())return1;return0;}$ge=newGraphicsEnvironment(400,400);$ge->addColor("black",0,0,0);$ge->addColor("red",255,0,0);$ge->addColor("green",0,255,0);$ge->addColor("blue",0,0,255);$gobjs=array();$gobjs[]=newOval(100,"red",50,50,150,150);$gobjs[]=newRectangle(200,"black",100,100,300,300);usort($gobjs,"zsort");foreach($gobjsas$gobj){$gobj->render($ge);}$ge->saveAsPng("test.png");?>$gobjs[]=newOval(200,"red",50,50,150,150);$gobjs[]=newRectangle(100,"black",100,100,300,300);
functionzsort($a,$b){if($a->z()<$b->z())return-1;if($a->z()>$b->z())return1;return0;}classGroupextendsGraphicsObject{private$z;protected$members=array();publicfunction__construct($z){$this->z=$z;}publicfunctionadd($member){$this->members[]=$member;}publicfunctionrender($ge){usort($this->members,"zsort");foreach($this->membersas$gobj){$gobj->render($ge);}}publicfunctionz(){return$this->z;}}<?phprequire_once("glib.php");$ge=newGraphicsEnvironment(400,400);$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=newGroup(0);$g1->add(newOval(200,"red",50,50,150,150));$g1->add(newRectangle(100,"black",100,100,300,300));$g1->render($ge);$ge->saveAsPng("test.png");?>
现在所有的客户机需要做的是创建一个Group对象。它会处理排序和其他操作。
<?phpclassGraphicsEnvironment{public$width;public$height;public$gdo;public$colors=array();publicfunction__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"));}publicfunctionwidth(){return$this->width;}publicfunctionheight(){return$this->height;}publicfunctionaddColor($name,$r,$g,$b){$this->colors[$name]=imagecolorallocate($this->gdo,$r,$g,$b);}publicfunctiongetGraphicObject(){return$this->gdo;}publicfunctiongetColor($name){return$this->colors[$name];}publicfunctionsaveAsPng($filename){imagepng($this->gdo,$filename);}publicfunctiontx($x){return$x*$this->width;}publicfunctionty($y){return$y*$this->height;}}abstractclassGraphicsObject{abstractpublicfunctionrender($ge);abstractpublicfunctionz();}functionzsort($a,$b){if($a->z()<$b->z())return-1;if($a->z()>$b->z())return1;return0;}classGroupextendsGraphicsObject{private$z;protected$members=array();publicfunction__construct($z){$this->z=$z;}publicfunctionadd($member){$this->members[]=$member;}publicfunctionrender($ge){usort($this->members,"zsort");foreach($this->membersas$gobj){$gobj->render($ge);}}publicfunctionz(){return$this->z;}}abstractclassBoxObjectextendsGraphicsObject{protected$color;protected$sx;protected$sy;protected$ex;protected$ey;protected$z;publicfunction__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;}publicfunctionrender($ge){$rsx=$ge->tx($this->sx);$rsy=$ge->ty($this->sy);$rex=$ge->tx($this->ex);$rey=$ge->ty($this->ey);$this->draw($rsx,$rsy,$rex,$rey,$ge->getGraphicObject(),$ge->getColor($this->color));}abstractpublicfunctiondraw($sx,$sy,$ex,$ey,$gobj,$color);publicfunctionz(){return$this->z;}}classLineextendsBoxObject{publicfunctiondraw($sx,$sy,$ex,$ey,$gobj,$color){imageline($gobj,$sx,$sy,$ex,$ey,$color);}}classRectangleextendsBoxObject{publicfunctiondraw($sx,$sy,$ex,$ey,$gobj,$color){imagefilledrectangle($gobj,$sx,$sy,$ex,$ey,$color);}}classOvalextendsBoxObject{publicfunctiondraw($sx,$sy,$ex,$ey,$gobj,$color){$w=$ex-$sx;$h=$ey-$sy;imagefilledellipse($gobj,$sx+($w/2),$sy+($h/2),$w,$h,$color);}}?>$g1=newGroup(0);$g1->add(newOval(200,"red",0.1,0.1,0.5,0.5));$g1->add(newRectangle(100,"black",0.4,0.4,0.9,0.9));
这非常不错,但是您可能实际上并不希望使用一个0,0与1,1之间的viewport;而是希望使用任意的viewport——例如,在-1000,-1000到1000,1000之间。要让这成为可能,这个图形环境就需要知道viewport的起点和终点。
classGraphicsEnvironment{public$vsx;public$vsy;public$vex;public$vey;public$width;public$height;public$gdo;public$colors=array();publicfunction__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"));}publicfunctionwidth(){return$this->width;}publicfunctionheight(){return$this->height;}publicfunctionaddColor($name,$r,$g,$b){$this->colors[$name]=imagecolorallocate($this->gdo,$r,$g,$b);}publicfunctiongetGraphicObject(){return$this->gdo;}publicfunctiongetColor($name){return$this->colors[$name];}publicfunctionsaveAsPng($filename){imagepng($this->gdo,$filename);}publicfunctiontx($x){$r=$this->width/($this->vex-$this->vsx);return($x-$this->vsx)*$r;}publicfunctionty($y){$r=$this->height/($this->vey-$this->vsy);return($y-$this->vsy)*$r;}}<?phprequire_once("glib.php");$ge=newGraphicsEnvironment(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=newGroup(0);$g1->add(newOval(200,"red",-800,-800,0,0));$g1->add(newRectangle(100,"black",-400,-400,900,900));$g1->render($ge);$ge->saveAsPng("test.png");?>$ge=newGraphicsEnvironment(400,200,-1000,-1000,1000,1000);
评论 (0) All