search
HomeBackend DevelopmentPHP TutorialExample 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:

Copy code The code is as follows:

//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:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914042.htmlTechArticleAn example of using the GD library to create a circular pie chart in PHP, gd circle in PHP, there are some simple The image function can be used directly, but most images to be processed need to be compiled when PHP...
Statement
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

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 Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),