PHP 5.0創(chuàng)建圖形的實(shí)用方法完整篇第3/3頁(yè)
測(cè)試代碼只需要稍加修改即可(請(qǐng)參看清單 8)。這些對(duì)象現(xiàn)在需要在 0,0 和 1,1 之間的 viewport 中進(jìn)行指定。
清單 8. 使用新 viewport 坐標(biāo)的測(cè)試代碼
$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 ) ); |
這非常不錯(cuò),但是您可能實(shí)際上并不希望使用一個(gè) 0,0 與 1,1 之間的 viewport;而是希望使用任意的 viewport —— 例如,在 -1000,-1000 到 1000,1000 之間。要讓這成為可能,這個(gè)圖形環(huán)境就需要知道 viewport 的起點(diǎn)和終點(diǎn)。
圖 8 顯示了更新后的 GraphicsEnvironment 類,它具有幾個(gè)成員變量,用來(lái)存儲(chǔ) viewport 的起點(diǎn)和終點(diǎn)坐標(biāo):vsx,vsy 和 vex,vey。圖形對(duì)象并不需要進(jìn)行修改。
圖 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è)構(gòu)造函數(shù)可以利用另外 4 個(gè)參數(shù)了,它們分別是 viewport 的起點(diǎn)和終點(diǎn)。 tx 和 ty 函數(shù)使用新的 viewport 坐標(biāo),并將 viewport 坐標(biāo)轉(zhuǎn)換成物理坐標(biāo)。
測(cè)試代碼如清單 10 所示。
清單 10. viewport 測(cè)試代碼
<?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" ); ?> |
這段測(cè)試代碼會(huì)在 -1000,-1000 與 1000,000 之間創(chuàng)建一個(gè) viewport。對(duì)象會(huì)被重新放置,以適合這個(gè)新的坐標(biāo)系統(tǒng)。
測(cè)試代碼的輸出如圖 9 所示。
圖 9. viewport 繪制的圖像轉(zhuǎn)換為一個(gè) 400X400 的圖像

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

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