我將圖形編輯程序分為兩類:一類是繪圖程序,利用這種程序 " /> 久久笫一福利免费导航,国产成人视屏,国产成人精品综合在线

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

本文將展示如何使用 php 構(gòu)建面向?qū)ο蟮膱D形層。使用面向?qū)ο蟮南到y(tǒng)可以用來(lái)構(gòu)建復(fù)雜的圖形,這比使用標(biāo)準(zhǔn) php 庫(kù)中所提供的基本功能來(lái)構(gòu)建圖形簡(jiǎn)單很多。

  我將圖形編輯程序分為兩類:一類是繪圖程序,利用這種程序可以一個(gè)像素一個(gè)像素地繪制圖像;另外一類是制圖程序,這種程序提供了一組對(duì)象,例如線、橢圓和矩形,您可以使用這些對(duì)象來(lái)組合成一幅大圖像,例如 JPEG。繪圖程序非常適合進(jìn)行像素級(jí)的控制。但是對(duì)于業(yè)務(wù)圖形來(lái)說(shuō),制圖程序是比較好的方式,因?yàn)榇蟛糠謭D形都是由矩形、線和橢圓組成的。

  php 內(nèi)置的制圖基本操作與繪圖程序非常類似。它們對(duì)于繪制圖像來(lái)說(shuō)功能非常強(qiáng)大;但是如果您希望自己的圖像是一組對(duì)象集合時(shí),這就不太適合了。本文將向您展示如何在 php 圖形庫(kù)的基礎(chǔ)上構(gòu)建一個(gè)面向?qū)ο蟮膱D形庫(kù)。您將使用 php V5 中提供的面向?qū)ο蟮臄U(kuò)展。

  具有面向?qū)ο蟮膱D形支持之后,您的圖形代碼就非常容易理解和維護(hù)了。您可能還需要從一種單一的圖形源將圖形合成為多種類型的媒介:Flash 電影、SVG 等等。

  目標(biāo)

  創(chuàng)建一個(gè)圖形對(duì)象庫(kù)包括 3 個(gè)主要的目標(biāo):

  從基本操作切換到對(duì)象上

  它不使用 imageline、imagefilledrectangle 以及其他圖形函數(shù),這個(gè)庫(kù)應(yīng)該提供一些對(duì)象,例如 Line、Rectangle 和 Oval,它們可以用來(lái)制作圖像。它應(yīng)該還可以支持構(gòu)建更大的復(fù)雜對(duì)象或?qū)?duì)象進(jìn)行分組的功能。

  可以進(jìn)行 z 值排序

  制圖程序讓畫(huà)家可以在畫(huà)面表面上上下移動(dòng)圖形對(duì)象。這個(gè)庫(kù)應(yīng)該可以支持將一個(gè)對(duì)象放到其他對(duì)象前后的功能:它使用了一個(gè) z 值,用來(lái)定義對(duì)象從制圖平面開(kāi)始的高度。z 值越大的對(duì)象被畫(huà)得越晚,也就出現(xiàn)在那些 z 值較小的對(duì)象之上。

  提供 viewport 的轉(zhuǎn)換

  通常,數(shù)據(jù)的坐標(biāo)空間與圖像的坐標(biāo)空間是不同的。php 中的圖形基本操作是對(duì)圖像的坐標(biāo)平面進(jìn)行操作的。這個(gè)圖形庫(kù)應(yīng)該支持 viewport 的規(guī)范,這樣您就可以在一個(gè)程序員熟悉的坐標(biāo)系統(tǒng)中指定圖形了,并且可以自動(dòng)進(jìn)行伸縮來(lái)適應(yīng)任何圖像的大小。

  由于這里有很多特性,您將一步步地編寫(xiě)代碼來(lái)展示這些代碼如何不斷增加功能。

  基礎(chǔ)知識(shí)

  讓我們首先來(lái)看一個(gè)圖形環(huán)境對(duì)象和一個(gè)名為 GraphicsObject 的接口,它是使用一個(gè) Line 類實(shí)現(xiàn)的,功能就是用來(lái)畫(huà)線。UML 如圖 1 所示。

圖 1. 圖形環(huán)境和圖形對(duì)象接口
圖形環(huán)境和圖形對(duì)象接口

  GraphicsEnvironment 類中保存了圖形對(duì)象和一組顏色,還包括寬度和高度。saveASPng 方法負(fù)責(zé)將當(dāng)前的圖像輸出到指定的文件中。

  GraphicsObject 是任何圖形對(duì)象都必須使用的接口。要開(kāi)始使用這個(gè)接口,您所需要做的就是使用 render 方法來(lái)畫(huà)這個(gè)對(duì)象。它是由一個(gè) Line 類實(shí)現(xiàn)的,它利用 4 個(gè)坐標(biāo):開(kāi)始和結(jié)束的 x 值,開(kāi)始和結(jié)束的 y 值。它還有一個(gè)顏色。當(dāng)調(diào)用 render 時(shí),這個(gè)對(duì)象從 sx,sy 到 ex,ey 畫(huà)一條由名字指定的顏色的線。

  這個(gè)庫(kù)的代碼如清單 1 所示。

  清單 1. 基本的圖形庫(kù)

 <?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 ) );  } } ?> 

  測(cè)試代碼如清單 2 所示:

  清單 2. 基本圖形庫(kù)的測(cè)試代碼

 <?php require_once( "glib.php" ); $ge = new GraphicsEnvironment( 400, 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 ); $gobjs = array(); $gobjs []= new Line( "black", 10, 5, 100, 200 ); $gobjs []= new Line( "blue", 200, 150, 390, 380 ); $gobjs []= new Line( "red", 60, 40, 10, 300 ); $gobjs []= new Line( "green", 5, 390, 390, 10 ); foreach( $gobjs as $gobj ) { $gobj->render( $ge ); } $ge->saveASPng( "test.png" ); ?> 

  這個(gè)測(cè)試程序創(chuàng)建了一個(gè)圖形環(huán)境。然后創(chuàng)建幾條線,它們指向不同的方向,具有不同的顏色。然后,render 方法可以將它們畫(huà)到圖形平面上。最后,這段代碼將這個(gè)圖像保存為 test.png。

  在本文中,都是使用下面的命令行解釋程序來(lái)運(yùn)行這段代碼,如下所示:

 % php test.php % 


  圖 2 顯示了所生成的 test.png 文件在 Firefox 中的樣子。

  圖2. 簡(jiǎn)單的圖形對(duì)象測(cè)試

簡(jiǎn)單的圖形對(duì)象測(cè)試

  這當(dāng)然不如蒙娜麗莎漂亮,但是可以滿足目前的工作需要。

[NextPage]

添加維數(shù)

  我們的第一個(gè)需求 ―― 提供圖形對(duì)象的能力 ―― 已經(jīng)滿足了,現(xiàn)在應(yīng)該開(kāi)始滿足第二個(gè)需求了:可以使用一個(gè) z 值將一個(gè)對(duì)象放到其他對(duì)象的上面或下面。

  我們可以將每個(gè) z 值當(dāng)作是原始圖像的一個(gè)面。所畫(huà)的元素是按照 z 值從最小到最大的順序來(lái)畫(huà)的。例如,讓我們畫(huà)兩個(gè)圖形元素:一個(gè)紅色的圓和一個(gè)黑色的方框。圓的 z 值是 100,而黑方框的 z 值是 200。這樣會(huì)將圓放到方框之后,如圖 3 所示:

  圖3. 不同 z 值的面

不同 z 值的面

  我們只需要修改一下 z 值就可以將這個(gè)紅圓放到黑方框之上。要實(shí)現(xiàn)這種功能,我們需要讓每個(gè) GraphicsObject 都具有一個(gè) z() 方法,它返回一個(gè)數(shù)字,就是 z 值。由于您需要?jiǎng)?chuàng)建不同的圖形對(duì)象(Line、Oval 和 Rectangle),您還需要?jiǎng)?chuàng)建一個(gè)基本的類 BoxObject,其他 3 個(gè)類都使用它來(lái)維護(hù)起點(diǎn)和終點(diǎn)的坐標(biāo)、z 值和這個(gè)對(duì)象的顏色(請(qǐng)參看圖 4)。

  圖 4. 給系統(tǒng)添加另外一維:z 值
給系統(tǒng)添加另外一維:z 值

  這個(gè)圖形庫(kù)的新代碼如清單 3 所示:

  清單 3. 可以處理 z 信息的圖形庫(kù)

 <?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 );  abstract public function z(); } abstract class BoxObject extends GraphicsObject {  protected $color;  protected $sx;  protected $sy;  protected $ex;  protected $ey;  protected $z;  public function __construct( $z, $color, $sx, $sy, $ex, $ey )  {  $this->z = $z;  $this->color = $color;  $this->sx = $sx;  $this->sy = $sy;  $this->ex = $ex;  $this->ey = $ey;  }  public function z() { return $this->z; } } class Line extends BoxObject {  public function render( $ge )  {  imageline( $ge->getGraphicObject(),   $this->sx, $this->sy,   $this->ex, $this->ey,   $ge->getColor( $this->color ) );  } } class Rectangle extends BoxObject {  public function render( $ge )  {  imagefilledrectangle( $ge->getGraphicObject(),   $this->sx, $this->sy,   $this->ex, $this->ey,   $ge->getColor( $this->color ) );  } } class Oval extends BoxObject {  public function render( $ge )  {  $w = $this->ex - $this->sx;  $h = $this->ey - $this->sy;  imagefilledellipse( $ge->getGraphicObject(),   $this->sx + ( $w / 2 ),   $this->sy + ( $h / 2 ),   $w, $h,   $ge->getColor( $this->color ) );  } } ?> 

  測(cè)試代碼也需要進(jìn)行更新,如清單 4 所示。

  清單 4. 更新后的測(cè)試代碼

 <?php require_once( "glib.php" ); function zsort( $a, $b ) {  if ( $a->z() < $b->z() ) return -1;  if ( $a->z() > $b->z() ) return 1;  return 0; } $ge = new GraphicsEnvironment( 400, 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 ); $gobjs = array(); $gobjs []= new Oval( 100, "red", 50, 50, 150, 150 ); $gobjs []= new Rectangle( 200, "black", 100, 100, 300, 300 ); usort( $gobjs, "zsort" ); foreach( $gobjs as $gobj ) { $gobj->render( $ge ); } $ge->saveASPng( "test.png" ); ?> 

  此處需要注意兩件事情。首先是我們添加了創(chuàng)建 Oval 和 Rectangle 對(duì)象的過(guò)程,其中第一個(gè)參數(shù)是 z 值。其次是調(diào)用了 usort,它使用了 zsort 函數(shù)來(lái)對(duì)圖形對(duì)象根據(jù) z 值進(jìn)行排序。

php技術(shù)PHP 5.0創(chuàng)建圖形的實(shí)用方法完整篇第1/3頁(yè),轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国外免费精品视频在线观看 | 欧美精品色视频 | 亚洲一区二区视频在线观看 | 精品视频专区 | 国产一区二 | 九九精品在线 | 91欧美在线| 天天射天天干天天色 | 精品9e精品视频在线观看 | 成人久久免费视频 | 另类视频色综合 | 成人免费观看www在线 | 香蕉97超级碰碰碰免费公 | 草草草在线 | 一区二区三区四区在线免费观看 | 麻豆精品国产自产在线 | 91aaa免费观看在线观看资源 | 国产99免费视频 | 99热精品成人免费观看 | 久久久精品免费 | 好吊妞视频一区二区 | 亚洲国产成人久久综合野外 | 国产激情在线观看 | 精品一区二区三区在线视频 | 国产精品亚洲欧美一级久久精品 | 午夜激情在线视频 | 伊人五月婷婷 | 起碰97| 国产精品伦理久久久久 | 午夜视频福利 | 国产精品一区二区三区四区 | 最近手机中文字幕无吗 | 欧美一级久久久久久久大片 | 久久精品免视着国产成人 | 日本色婷婷 | 久久青青草原精品无线观看 | www.麻豆91 | 亚洲影视一区 | 欧美日韩在线观看视频 | 大尺度毛片 | 国产伦精品一区二区三区视频小说 |