PHP Tutorials - Herong's Tutorial Examples - Version 5.00, by Dr. Herong Yang
GD Library - Draw Graphical Elements
This section provides a tutorial example on how to draw graphical elements using GD Library functions.
You can use GD Library functions to create a new image and draw some graphical elements.
1. Write a PHP script to create an image with some graphical elements added and save it to GD-Graphics.png:
<?php # GD-Create-Image.php #- Copyright (c) HerongYang.com. All Rights Reserved. $image = imagecreatetruecolor(400,400); $blue = imagecolorallocate($image, 0, 0, 255); $green = imagecolorallocate($image, 0, 255, 0); $red = imagecolorallocate($image, 255, 0, 0); print("Drawing a box...\n"); imagefilledrectangle($image, 50, 50, 350, 350, $blue); print("Drawing a circle...\n"); imagefilledellipse($image, 200, 200, 200, 200, $green); print("Drawing 2 lines...\n"); imageline($image, 0,0, 400, 400, $red); imageline($image, 400,0, 0, 400, $red); $file = "GD-Graphics.png"; imagepng($image, $file); print("Image saved:\n"); print(" File: $file\n"); ?>
2. Run the above script:
herong$ php GD-Create-Image.php Drawing a box... Drawing a circle... Drawing 2 lines... Image saved: File: GD-Graphics.png
3. View GD-Graphics.png. You see a nice image.
Table of Contents
Introduction and Installation of PHP 7.3
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Configuring and Sending out Emails
►GD Library - Draw Graphical Elements
GD Library - Print 2 Pictures on 1 Page
GD Library - Create Transparent Image
Transparent Image Over Text Web Page
GD Library - Pad Transparent Image
Managing PHP Engine and Modules on macOS