PHP中使用GD库创建圆形饼图的例子_PHP
在PHP中,有一些简单的图像函数是可以直接使用的,但大多数要处理的图像,都需要在编译PHP时加上GD库。除了安装GD库之外,在PHP中还可能需要其他的库,这可以根据需要支持哪些图像格式而定。GD库可以在http://www.boutell.com/gd/免费下载,不同的GD版本支持的图像格式不完全一样,最新的GD库版本支持GIF、JPEG、PNG、WBMP、XBM等格式的图像文件,此外还支持一些如FreeType、Type 1等字体库。通过GD库中的函数,可以完成各种点、线、几何图形、文本及颜色的操作处理,也可以创建或读取多种格式的图像文件。
在PHP中,通过GD库处理图像的操作都是先在内存中处理,操作完成以后再以文件流的方式输出到浏览器或保存在服务器的磁盘中。创建一个图像应该完成如下所示4个基本步骤。
①创建画布:所有的绘图设计都需要在一个背景图片上完成,而画布实际上就是在内存中开辟的一块临时区域,用于存储图像的信息。以后的图像操作都将基于这个背景画布,该画布的管理就类似于我们在画画时使用的画布。
②绘制图像:画布创建完成以后,就可以通过这个画布资源,使用各种画像函数设置图像的颜色、填充画布、画点、线段、各种几何图形,以及向图像中添加文本等。
③输出图像:完成整个图像的绘制以后,需要将图像以某种格式保存到服务器指定的文件中,或将图像直接输出到浏览器上显示给客户。但在图像输出之前,一定要使用header()函数发送Content-type通知浏览器,这次发送的是图片不是文本。
④释放资源:图像被输出以后,画布中的内容也不再有用。出于节约系统资源的考虑,需要及时清楚画布占用的所有内存资源。
我们先来了解一下一个非常简单的创建图像脚本。在下面的脚本文件image.php中,按前面介绍的绘制图像的四个步骤,使用GD库动态输出一个扇形统计图。代码如下所示:
代码如下:
//创建画布,返回一个资源类型的变量$image,并在内存中开辟一个临时区域
$image = imagecreatetruecolor(100, 100); //创建画布大小为100x100
//设置图像中所需的颜色,相当于在画画时准备的染料盒
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); //为图像分配颜色为白色
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); //为图像分配颜色为灰色
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); //为图像分配颜色为暗灰色
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80); //为图像分配颜色为深蓝色
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); //为图像分配颜色为暗深蓝色
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00); //为图像分配颜色为红色
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); //为图像分配颜色为暗红色
imagefill($image, 0, 0, $white); //为画布背景填充背景颜色
//动态制作3D效果
for ($i = 60; $i >50; $i--){ //循环10次画出立体效果
imagefilledarc($image, 50, $i, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, 50, 50, 100, 50, -160, 40, $navy, IMG_ARC_PIE); //画一椭圆弧且填充
imagefilledarc($image, 50, 50, 100, 50, 40 , 75, $gray, IMG_ARC_PIE); //画一椭圆弧且填充
imagefilledarc($image, 50, 50, 100, 50, 75, 200, $red, IMG_ARC_PIE); //画一椭圆弧且填充
imagestring($image, 1, 15, 55, '34.7%', $white); //水平地画一行字符串
imagestring($image, 1, 45, 35, '55.5%', $white); //水平地画一行字符串
//向浏览器中输出一个GIF格式的图片
header('Content-type:image/png'); //使用头函数告诉浏览器以图像方式处理以下输出
imagepng($image); //向浏览器输出
imagedestroy($image); //销毁图像释放资源
?>
直接通过浏览器请求该脚本,或是将该脚本所在的URL,赋给HTML中的IMG标记的src属性,都可以获取动态输出的图像结果,如下图所示:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
