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 | <?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 ); } class Line extends GraphicsObject { private $color; private $sx; private $sy; private $ex; private $ey; public 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 ) ); } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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 ); $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->saveAsPng( "test.png" ); ?> |
1 2 | % php test.php % |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |