1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <?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 ) ); } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php require_once( "glib.php" ); function zsort( $a, $b ) { if ( $a->z() < $b->z() ) return -1; if ( $a->z() > $b->z() ) return 1; return 0; } $ge = new GraphicsEnvironment( 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 []= new Oval( 100, "red", 50, 50, 150, 150 ); $gobjs []= new Rectangle( 200, "black", 100, 100, 300, 300 ); usort( $gobjs, "zsort" ); foreach( $gobjs as $gobj ) { $gobj->render( $ge ); } $ge->saveAsPng( "test.png" ); ?> |
1 2 | $gobjs []= new Oval( 200, "red", 50, 50, 150, 150 ); $gobjs []= new Rectangle( 100, "black", 100, 100, 300, 300 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function zsort( $a, $b ) { if ( $a->z() < $b->z() ) return -1; if ( $a->z() > $b->z() ) return 1; return 0; } class Group extends GraphicsObject { private $z; protected $members = array(); public function __construct( $z ) { $this->z = $z; } public function add( $member ) { $this->members []= $member; } public function render( $ge ) { usort( $this->members, "zsort" ); foreach( $this->members as $gobj ) { $gobj->render( $ge ); } } public function z() { return $this->z; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php require_once( "glib.php" ); $ge = new GraphicsEnvironment( 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 = new Group( 0 ); $g1->add( new Oval( 200, "red", 50, 50, 150, 150 ) ); $g1->add( new Rectangle( 100, "black", 100, 100, 300, 300 ) ); $g1->render( $ge ); $ge->saveAsPng( "test.png" ); ?> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |