PHP 5.0創(chuàng)建圖形的實用方法完整篇
更新時間:2008年01月26日 19:26:30 作者:
PHP 5.0創(chuàng)建圖形的實用方法完整篇
測試代碼只需要稍加修改即可(請參看清單 8)。這些對象現在需要在 0,0 和 1,1 之間的 viewport 中進行指定。
清單 8. 使用新 viewport 坐標的測試代碼
$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 ) ); |
這非常不錯,但是您可能實際上并不希望使用一個 0,0 與 1,1 之間的 viewport;而是希望使用任意的 viewport —— 例如,在 -1000,-1000 到 1000,1000 之間。要讓這成為可能,這個圖形環(huán)境就需要知道 viewport 的起點和終點。
圖 8 顯示了更新后的 GraphicsEnvironment 類,它具有幾個成員變量,用來存儲 viewport 的起點和終點坐標:vsx,vsy 和 vex,vey。圖形對象并不需要進行修改。
圖 8. 具有靈活 viewport 規(guī)范的圖形環(huán)境

清單 9 顯示了更新后的 GraphicsEnvironment 代碼。
清單 9. 更新后的 GraphicsEnvironment 代碼
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;
}
}
|
現在這個構造函數可以利用另外 4 個參數了,它們分別是 viewport 的起點和終點。 tx 和 ty 函數使用新的 viewport 坐標,并將 viewport 坐標轉換成物理坐標。
測試代碼如清單 10 所示。
清單 10. viewport 測試代碼
<?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" ); ?> |
這段測試代碼會在 -1000,-1000 與 1000,000 之間創(chuàng)建一個 viewport。對象會被重新放置,以適合這個新的坐標系統。
測試代碼的輸出如圖 9 所示。
圖 9. viewport 繪制的圖像轉換為一個 400X400 的圖像

如果您希望圖像的大小是 400X200,就可以采用下面的方法:
$ge = new GraphicsEnvironment( 400, 200, -1000, -1000, 1000, 1000 ); |
您會得到一個縱向縮小后的圖像,如圖 10 所示。
圖 10. 圖形的 400X200 版本

這展示了代碼如何自動調整圖像的大小來適合所請求的圖像。
結束語
動態(tài)圖可以為應用程序添加一個新的交互層。使用這種面向對象的系統可以讓構建復雜圖形變得非常簡單,比使用標準的 PHP 庫中的基本操作來畫圖更加簡單。另外,您還可以實現畫不同大小或類型的圖像,并且可以長期使用相同的代碼來畫不同類型的媒介,例如 SVG、PDF、Flash 和其他類型的媒介。
相關文章
探討:php中在foreach中使用foreach ($arr as &$value) 這種類型的解釋
本篇文章是對php中在foreach中使用foreach ($arr as &$value) 這種類型的解釋進行了詳細的分析介紹,需要的朋友參考下2013-06-06
微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼
這篇文章主要為大家詳細介紹了微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼,感興趣的小伙伴們可以參考一下2016-08-08

