亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP 5.0創(chuàng)建圖形的實(shí)用方法完整篇第3/3頁(yè)

 更新時(shí)間:2008年01月26日 19:26:30   作者:  
PHP 5.0創(chuàng)建圖形的實(shí)用方法完整篇

  測(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)境

具有靈活 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 的圖像

viewport 繪制的圖像轉(zhuǎn)換為一個(gè) 400X400 的圖像

  如果您希望圖像的大小是 400X200,就可以采用下面的方法:

 
$ge = new GraphicsEnvironment( 400, 200, 
 -1000, -1000, 1000, 1000 ); 

  您會(huì)得到一個(gè)縱向縮小后的圖像,如圖 10 所示。

  圖 10. 圖形的 400X200 版本

圖形的 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)文章

最新評(píng)論