


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

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

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
