


Example of using GD library to create a circular pie chart in PHP, gd circle_PHP tutorial
An example of using the GD library to create a circular pie chart in PHP, gd circle
In PHP, there are some simple image functions that can be used directly, but most of the images to be processed require the GD library when compiling PHP. In addition to installing the GD library, other libraries may be required in PHP, depending on which image formats need to be supported. The GD library can be downloaded for free at http://www.boutell.com/gd/. Different GD versions support different image formats. The latest GD library version supports images in GIF, JPEG, PNG, WBMP, XBM and other formats. files, and also supports some font libraries such as FreeType and Type 1. Through the functions in the GD library, you can complete the operation and processing of various points, lines, geometric figures, text and colors, and you can also create or read image files in various formats.
In PHP, the operation of processing images through the GD library is first processed in the memory. After the operation is completed, it is output to the browser in the form of a file stream or saved on the server's disk. Creating an image should be done in 4 basic steps as shown below.
①Create a canvas: All drawing designs need to be completed on a background image, and the canvas is actually a temporary area opened in memory to store image information. All future image operations will be based on this background canvas, and the management of this canvas is similar to the canvas we use when painting.
②Draw an image: After the canvas is created, you can use this canvas resource to set the color of the image, fill the canvas, draw points, line segments, various geometric figures, and add text to the image using various portrait functions.
③Output image: After completing the drawing of the entire image, the image needs to be saved in a certain format to a file specified by the server, or the image needs to be output directly to the browser to display to the customer. But before the image is output, you must use the header() function to send the Content-type to notify the browser. This time, the image is sent instead of text.
④Release resources: After the image is output, the content in the canvas is no longer useful. In order to save system resources, it is necessary to know all the memory resources occupied by the canvas in a timely manner.
Let’s first take a look at a very simple script to create an image. In the following script file image.php, follow the four steps of drawing an image introduced previously, and use the GD library to dynamically output a sector chart. The code looks like this:
//Create a canvas, return a resource type variable $image, and open a temporary area in memory
$image = imagecreatetruecolor(100, 100); //Create canvas size is 100x100
//Set the required color in the image, which is equivalent to the dye box prepared when painting
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); //Assign the color to the image to be white
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); //Assign the color to the image as gray
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); //Assign the color to the image as dark gray
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80); //Assign the color to the image as dark blue
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); //Assign the color to the image as dark blue
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00); //Assign the color to the image as red
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); //Assign the color to the image as dark red
Imagefill($image, 0, 0, $white); //Fill the canvas background with the background color
//Dynamic production of 3D effects
for ($i = 60; $i >50; $i--){ //Loop 10 times to draw a three-dimensional effect
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); //Draw an elliptical arc and fill it
Imagefilledarc($image, 50, 50, 100, 50, 40, 75, $gray, IMG_ARC_PIE); //Draw an elliptical arc and fill it
Imagefilledarc($image, 50, 50, 100, 50, 75, 200, $red, IMG_ARC_PIE); //Draw an elliptical arc and fill it
Imagestring($image, 1, 15, 55, '34.7%', $white); //Draw a line of string horizontally
Imagestring($image, 1, 45, 35, '55.5%', $white); //Draw a line of string horizontally
//Output a GIF format image to the browser
header('Content-type:image/png'); //Use the header function to tell the browser to process the following output as an image
imagepng($image); //Output
to the browser Imagedestroy($image); //Destroy the image to release resources
?>
You can obtain dynamically output image results by directly requesting the script through the browser, or by assigning the URL of the script to the src attribute of the IMG tag in HTML, as shown in the figure below:

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
