PHP 5.0創(chuàng)建圖形的實用方法完整篇
更新時間:2008年01月26日 19:26:30 作者:
PHP 5.0創(chuàng)建圖形的實用方法完整篇
測試代碼只需要稍加修改即可(請參看清單 8)。這些對象現(xiàn)在需要在 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; } } |
現(xiàn)在這個構(gòu)造函數(shù)可以利用另外 4 個參數(shù)了,它們分別是 viewport 的起點和終點。 tx 和 ty 函數(shù)使用新的 viewport 坐標,并將 viewport 坐標轉(zhuǎn)換成物理坐標。
測試代碼如清單 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。對象會被重新放置,以適合這個新的坐標系統(tǒng)。
測試代碼的輸出如圖 9 所示。
圖 9. viewport 繪制的圖像轉(zhuǎn)換為一個 400X400 的圖像

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

這展示了代碼如何自動調(diào)整圖像的大小來適合所請求的圖像。
結(jié)束語
動態(tài)圖可以為應(yīng)用程序添加一個新的交互層。使用這種面向?qū)ο蟮南到y(tǒng)可以讓構(gòu)建復(fù)雜圖形變得非常簡單,比使用標準的 PHP 庫中的基本操作來畫圖更加簡單。另外,您還可以實現(xiàn)畫不同大小或類型的圖像,并且可以長期使用相同的代碼來畫不同類型的媒介,例如 SVG、PDF、Flash 和其他類型的媒介。
相關(guān)文章
探討:php中在foreach中使用foreach ($arr as &$value) 這種類型的解釋
本篇文章是對php中在foreach中使用foreach ($arr as &$value) 這種類型的解釋進行了詳細的分析介紹,需要的朋友參考下2013-06-06php關(guān)聯(lián)數(shù)組快速排序的方法
這篇文章主要介紹了php關(guān)聯(lián)數(shù)組快速排序的方法,涉及php數(shù)組排序的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04php計數(shù)排序算法的實現(xiàn)代碼(附四個實例代碼)
計數(shù)排序(Counting sort)是一種根據(jù)小整數(shù)鍵對一組對象排序的算法;也就是說,它是一個整數(shù)排序算法。它通過計算具有不同鍵值的對象的數(shù)量,并對這些數(shù)量使用算術(shù)來確定輸出序列中每個鍵值的位置2020-03-03幾個有用的php字符串過濾,轉(zhuǎn)換函數(shù)代碼
幾個有用的php字符串過濾,轉(zhuǎn)換函數(shù),主要是一些字符的安全處理與字符串處理2012-05-05PHP實現(xiàn)快速向MySQL插入千萬條數(shù)據(jù)
在開發(fā)中,有時需要向數(shù)據(jù)庫中插入大量數(shù)據(jù),本文將探討如何使用PHP來快速向MySQL數(shù)據(jù)庫插入1000萬條數(shù)據(jù),并分享一些優(yōu)化方法,以確保性能的最優(yōu)2023-08-08微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼
這篇文章主要為大家詳細介紹了微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼,感興趣的小伙伴們可以參考一下2016-08-08