作者:江宁 时间:2026-06-04 11:44:59 浏览:556
分享
本文介绍在 PHP 5.0 中构建面向对象的图形库,以简化复杂图形的开发。通过封装 Line、Rectangle 等对象替代基础绘图函数,实现代码的可维护性。核心功能包括基于 Z 值的图层排序以及 Viewport 坐标转换,支持将图形合成多种媒介格式。
展开
PHP 面向对象图形库构建概述 本内容演示如何利用 PHP 搭建面向对象的图形处理层。借助面向对象架构来生成复杂图像,其便捷性远超依赖标准 PHP 库基本功能的传统方式。
图形编辑工具通常划分为两类:一是绘图程序,支持逐像素绘制;二是制图程序,提供线、椭圆、矩形等对象组件,便于组合成如 JPEG 等大图像。前者适合像素级精细控制,而后者因业务图形多由基本几何体构成,成为更优选择。
PHP 内置的制图基础操作虽强大,但侧重于直接绘制,若需将图像视为对象集合则显得力不从心。本文旨在展示如何基于现有 PHP 图形库,利用 PHP V5 的面向对象特性构建更高级的图形库。
引入面向对象图形支持后,代码的可读性与可维护性显著提升。此外,该架构还便于从单一图形源将内容合成为 Flash 电影、SVG 等多种媒体格式。
目标
构建图形对象库主要围绕三个核心目标展开:
从基本操作切换到对象上
该库不直接使用 imageline、imagefilledrectangle 等底层函数,而是提供 Line、Rectangle、Oval 等对象类来组装图像。同时,它应支持复杂对象的构建及对象分组功能。
可以进行 z 值排序
制图程序允许画家在画面表层上下移动对象。本库需支持对象的前后遮挡关系,即通过 z 值定义对象相对于制图平面的高度。z 值较大的对象最后绘制,从而覆盖在 z 值较小的对象之上。
提供 viewport 的转换
数据坐标空间通常与图像坐标空间不一致。PHP 基础操作直接作用于图像平面,而本库需支持 viewport 规范,使开发者能在熟悉的坐标系中指定图形,并自动缩放以适应任意图像尺寸。
鉴于涉及众多特性,下文将通过逐步编写代码的方式,展示各项功能如何逐步增强。
基础知识
首先考察图形环境对象及 GraphicsObject 接口,该接口由 Line 类实现,用于绘制线条。UML 结构如图 1 所示。
图 1. 图形环境和图形对象接口
GraphicsEnvironment 类负责存储图形对象、颜色集以及图像的宽高。其 saveAsPng 方法用于将当前图像输出至指定文件。
GraphicsObject 是所有图形对象必须实现的接口,核心在于 render 方法以执行绘制。Line 类实现了该接口,接收起始和结束的 x、y 四个坐标值及颜色参数。调用 render 时,对象将从 (sx, sy) 到 (ex, ey) 绘制指定颜色的线条。
相关代码见清单 1。
清单 1. 基本的图形库
<?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 ) );
}
}
?>
测试代码见清单 2。
清单 2. 基本图形库的测试代码
<?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" );
?>
测试程序实例化图形环境后,创建多条不同方向、颜色的线条,并通过 render 方法将其绘制到画布上,最终保存为 test.png。
本文使用以下命令行解释器运行代码:
图 2 展示了 Firefox 中生成的 test.png 效果。
图2. 简单的图形对象测试
尽管不如蒙娜丽莎精美,但足以满足当前工作需求。
[NextPage]
添加维数
首个需求——提供图形对象能力——已达成,现着手满足第二个需求:利用 z 值控制对象的前后层级。
可将每个 z 值视为原始图像的一个层面。元素按 z 值从小到大顺序绘制。例如,绘制一个 z 值为 100 的红圆和一个 z 值为 200 的黑框,红圆将位于黑框之后,如图 3 所示:
图3. 不同 z 值的面
调整 z 值可使红圆置于黑框之上。为此,每个 GraphicsObject 需具备返回 z 值的 z() 方法。由于需支持 Line、Oval、Rectangle 等不同对象,需创建一个基类 BoxObject,其他类继承它以管理起点终点坐标、z 值及颜色(见图 4)。
图 4. 给系统添加另外一维:z 值
更新后的图形库代码见清单 3:
清单 3. 可以处理 z 信息的图形库
<?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 ) );
}
}
?>
测试代码亦需相应更新,见清单 4。
清单 4. 更新后的测试代码
<?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" );
?>
需注意两点:一是创建了 Oval 和 Rectangle 对象,首参数为 z 值;二是调用了 usort 函数,配合 zsort 回调依据 z 值对图形对象进行排序。
123下一页阅读全文