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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | <?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 ); } public function tx( $x ) { return $x * $this->width; } public function ty( $y ) { return $y * $this->height; } } abstract class GraphicsObject { abstract public function render( $ge ); abstract public function z(); } 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; } } 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 render( $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 ) ); } abstract public function draw( $sx, $sy, $ex, $ey, $gobj, $color ); public function z() { return $this->z; } } class Line extends BoxObject { public function draw( $sx, $sy, $ex, $ey, $gobj, $color ) { imageline( $gobj, $sx, $sy, $ex, $ey, $color ); } } class Rectangle extends BoxObject { public function draw( $sx, $sy, $ex, $ey, $gobj, $color ) { imagefilledrectangle( $gobj, $sx, $sy, $ex, $ey, $color ); } } class Oval extends BoxObject { public function draw( $sx, $sy, $ex, $ey, $gobj, $color ) { $w = $ex - $sx; $h = $ey - $sy; imagefilledellipse( $gobj, $sx + ( $w / 2 ), $sy + ( $h / 2 ), $w, $h, $color ); } } ?> |
1 2 3 | $g1 = new Group( 0 ); $g1->add( new Oval( 200, "red", 0.1, 0.1, 0.5, 0.5 ) ); $g1->add( new Rectangle( 100, "black", 0.4, 0.4, 0.9, 0.9 ) ); |
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 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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" ); ?> |
1 2 | $ge = new GraphicsEnvironment( 400, 200, -1000, -1000, 1000, 1000 ); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |