Class เกี่ยวกับกราฟฟิก ด้วย php


By izeal - Posted on 19 December 2007

<?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 ) );
}
}
class Text extends GraphicsObject
{
private $color;
private $message;
private $posx;
private $posy;
private $size;

public function __construct( $color, $message, $posx, $posy, $size )
{
$this->color = $color;
$this->mes = $message;
$this->sx = $posx;
$this->sy = $posy;
$this->size = $size;
}

public function render( $ge )
{
imagestring($ge->getGraphicObject(), $this->sx, $this->sy, $this->size, $this->mes, $ge->getColor( $this->color ));

}
}

วิธีใช้

<?php
require_once( "glib.php" );

$ge = new GraphicsEnvironment( 440, 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 );
$ge->addColor( "gray", 219, 219, 219 );

$gobjs = array();
//วาดเส้น
$gobjs []= new Line( "gray", 170 , 20 , 170 , 220 );
$gobjs []= new Line( "black", 20, 10, 20, 220 );
$gobjs []= new Line( "black", 20, 220, 430, 220 );

//เขียนตัวหนังสือ
$gobjs []= new Text( "black", TEST, 1, 0 , 0 );

foreach( $gobjs as $gobj ) { $gobj->render( $ge );

$ge->saveAsPng( "test.png" );
echo "";
?>

เขียนเองป่าวนิ :-)

Copy มา

ฮ่าๆๆ

Copy !!555

555

พี่โอ๊ดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดดด 

...  sawinee ...

 capture ผลลัพธ์ มาให้ดูหน่อยได้ไหม

 พอดีผม ไม่ค่อยเป็น นะครับ

 

ใครเขียนว่ะเนี่ย izeal หรือ 

แล้ว izeal นี่ใครว่ะ 

เรียนคุณ BalLaemon

izeal จะมีนิสัยไม่ชอบอาบน้ำ ชอบอยู่ในที่สกปรก ไม่ชอบคุยกับรุ่นเดียวกันส่วนใหญ่จะพูดคุยกับน้องปี 1 ชอบทำงานให้น้องเค้า เพื่อที่จะได้กินข้าวฟรีเพื่อความอยู่รอดในแต่ละวัน ช่วงหลังๆมานี้จะตั้งใจอ่านหนังสือ แต่ก็ยังคงความสกปรกไว้เช่นเดิม .................... แค่นี้พอเดี่ยว ท่าน izeal จะโมโหไม่อาบน้ำสองอาทิตย์ซ้อน... คุณ BalLaemon คงทราบแล้วนะครับว่าเป็นใคร

ช่างเป็นสิ่งมีชีวิตที่ประหลาดมากแต่ก็ยังคงรักษาความสกปรกไว้น่ายกย่อง

 

Bohung คิดได้ไงนี้โดนใจจริง



User login