Home Backend Development PHP Tutorial Complete Practical Methods for Creating Graphics in PHP 50 Page 1/3

Complete Practical Methods for Creating Graphics in PHP 50 Page 1/3

Jul 29, 2016 am 08:37 AM
function gt height public this

本文将展示如何使用 PHP 构建面向对象的图形层。使用面向对象的系统可以用来构建复杂的图形,这比使用标准 PHP 库中所提供的基本功能来构建图形简单很多。
  我将图形编辑程序分为两类:一类是绘图程序,利用这种程序可以一个像素一个像素地绘制图像;另外一类是制图程序,这种程序提供了一组对象,例如线、椭圆和矩形,您可以使用这些对象来组合成一幅大图像,例如 JPEG。绘图程序非常适合进行像素级的控制。但是对于业务图形来说,制图程序是比较好的方式,因为大部分图形都是由矩形、线和椭圆组成的。
  PHP 内置的制图基本操作与绘图程序非常类似。它们对于绘制图像来说功能非常强大;但是如果您希望自己的图像是一组对象集合时,这就不太适合了。本文将向您展示如何在 PHP 图形库的基础上构建一个面向对象的图形库。您将使用 PHP V5 中提供的面向对象的扩展。
  具有面向对象的图形支持之后,您的图形代码就非常容易理解和维护了。您可能还需要从一种单一的图形源将图形合成为多种类型的媒介:Flash 电影、SVG 等等。
目标
  创建一个图形对象库包括 3 个主要的目标:
从基本操作切换到对象上
  它不使用 imageline、imagefilledrectangle 以及其他图形函数,这个库应该提供一些对象,例如 Line、Rectangle 和 Oval,它们可以用来制作图像。它应该还可以支持构建更大的复杂对象或对对象进行分组的功能。
  可以进行 z 值排序
  制图程序让画家可以在画面表面上上下移动图形对象。这个库应该可以支持将一个对象放到其他对象前后的功能:它使用了一个 z 值,用来定义对象从制图平面开始的高度。z 值越大的对象被画得越晚,也就出现在那些 z 值较小的对象之上。
  提供 viewport 的转换
  通常,数据的坐标空间与图像的坐标空间是不同的。PHP 中的图形基本操作是对图像的坐标平面进行操作的。这个图形库应该支持 viewport 的规范,这样您就可以在一个程序员熟悉的坐标系统中指定图形了,并且可以自动进行伸缩来适应任何图像的大小。
  由于这里有很多特性,您将一步步地编写代码来展示这些代码如何不断增加功能。
基础知识
  让我们首先来看一个图形环境对象和一个名为 GraphicsObject 的接口,它是使用一个 Line 类实现的,功能就是用来画线。UML 如图 1 所示。
图 1. 图形环境和图形对象接口
 PHP 50创建图形的实用方法完整篇第1/3页
  GraphicsEnvironment 类中保存了图形对象和一组颜色,还包括宽度和高度。saveAsPng 方法负责将当前的图像输出到指定的文件中。
  GraphicsObject 是任何图形对象都必须使用的接口。要开始使用这个接口,您所需要做的就是使用 render 方法来画这个对象。它是由一个 Line 类实现的,它利用 4 个坐标:开始和结束的 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 ) ); 
 } 
} 
?> 
Copy after login

  测试代码如清单 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" ); 
?> 
Copy after login

  这个测试程序创建了一个图形环境。然后创建几条线,它们指向不同的方向,具有不同的颜色。然后,render 方法可以将它们画到图形平面上。最后,这段代码将这个图像保存为 test.png。
  在本文中,都是使用下面的命令行解释程序来运行这段代码,如下所示:
% php test.php 
% 
Copy after login


  图 2 显示了所生成的 test.png 文件在 Firefox 中的样子。
  图2. 简单的图形对象测试
 PHP 50创建图形的实用方法完整篇第1/3页
  这当然不如蒙娜丽莎漂亮,但是可以满足目前的工作需要。

[NextPage]

添加维数
  我们的第一个需求 —— 提供图形对象的能力 —— 已经满足了,现在应该开始满足第二个需求了:可以使用一个 z 值将一个对象放到其他对象的上面或下面。
  我们可以将每个 z 值当作是原始图像的一个面。所画的元素是按照 z 值从最小到最大的顺序来画的。例如,让我们画两个图形元素:一个红色的圆和一个黑色的方框。圆的 z 值是 100,而黑方框的 z 值是 200。这样会将圆放到方框之后,如图 3 所示:
  图3. 不同 z 值的面
 PHP 50创建图形的实用方法完整篇第1/3页
  我们只需要修改一下 z 值就可以将这个红圆放到黑方框之上。要实现这种功能,我们需要让每个 GraphicsObject 都具有一个 z() 方法,它返回一个数字,就是 z 值。由于您需要创建不同的图形对象(Line、Oval 和 Rectangle),您还需要创建一个基本的类 BoxObject,其他 3 个类都使用它来维护起点和终点的坐标、z 值和这个对象的颜色(请参看图 4)。
  图 4. 给系统添加另外一维:z 值
 PHP 50创建图形的实用方法完整篇第1/3页
  这个图形库的新代码如清单 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 ) ); 
 } 
} 
?> 
Copy after login

  测试代码也需要进行更新,如清单 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" ); 
?> 
Copy after login

  此处需要注意两件事情。首先是我们添加了创建 Oval 和 Rectangle 对象的过程,其中第一个参数是 z 值。其次是调用了 usort,它使用了 zsort 函数来对图形对象根据 z 值进行排序。

当前1/3页 123下一页

以上就介绍了 PHP 50创建图形的实用方法完整篇第1/3页,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles