PHP Tutorials - Herong's Tutorial Examples
∟Image and Picture Processing
∟GD Library of Image Functions
This section provides a quick introduction on GD Library for creating images and managing image files in BMP, GIF, PNG, JPEG formats.
GD Library is a collection of image functions
that allows you manipulate image files in a variety of different image formats,
including GIF, PNG, JPEG, WBMP, and XPM.
GD library supports the following main features:
- Create new images and draw graphical elements.
- Read images from image files in BMP, GIF, PNG, JPEG formats.
- save images to image files in BMP, GIF, PNG, JPEG formats.
- Transform images by clipping, scaling, rotating, flipping, etc..
Examples of GD functions:
- gd_info() - This "GD information" function returns information about the GD Imaging library as an array.
- imagecreate($width,$height) - This "image create" function creates a blank image object.
- imagecreatetruecolor($width,$height) - This "image create true color" function creates a blank image object with in true color mode,
which uses RGB color format with 256 value for each color component.
- imagecreatefromgif($path) - This "image create from GIF" function creates an image object from a GIF image file.
- imagecreatefromjpeg($path) - This "image create from JPEG" function creates an image object from a JPEG (JPG) image file.
- imagecreatefromwbmp($path) - This "image create from BMP" function creates an image object from a BMP image file.
- imagegif($image,$path) - This "image GIF" function writes an image object to a GIF image file.
- imagejpeg($image,$path) - This "image JPEG" function writes an image object to a JPEG (JPG) image file.
- imagerotate($image,$angle,$color) - This "image rotate" function rotates an image object of $angle degrees
and returns the rotated image. $color is used to fill uncovered area after the rotation.
- imagesx($image) - This "image size x" function returns the width of $image.
- imagesy($image) - This "image size y" function returns the height of $image.
- imagedestroy($image) - This "image destroy" function descries an image object.
- imagecopyresized($dst_image,$src_image, $dst_x,$dst_y, $src_x,$src_y, $dst_w,$dst_h, $src_w,$src_h)
- This "image copy resized" function copies a part of an image from $src_image to $dst_image.
$src_x and $src_y specifies the copy-from location.
$dst_x and $dst_y specifies the copy-to location.
$src_w and $src_h specifies the copy-from size.
$dst_w and $dst_h specifies the copy-to size.
If the copy-to size is larger or smaller than the copy-from size, the copied part will be resized.
- imagecopyresampled($dst_image,$src_image, $dst_x,$dst_y, $src_x,$src_y, $dst_w,$dst_h, $src_w,$src_h)
- This "image copy resampled" function does the same job as imagecopyresized() with a better quality.
- imagecopy($dst_image,$src_image, $dst_x,$dst_y, $src_x,$src_y, $src_w,$src_h)
- This "image copy" function does the same job as imagecopyresized() except that the copied part will not be resized.
- imagecopymerge($dst_image,$src_image, $dst_x,$dst_y, $src_x,$src_y, $src_w,$src_h, $percentage)
- This "image copy merge" function does the same job as imagecopy() except that the copied part will be merged
to $dst_image with a $percentage factor.
- imagecolortransparent($image,$color) - This "image color transparent" function sets new color as the transparent color.
- imagecolorat($image, $x,$y) - This "image color at" function returns the color value from $image at $x and $y position.
In true color mode, the returned $rgb value can be decomposed as $r = ($rgb >> 16) & 0xFF, $g = ($rgb >> 8) & 0xFF,
and $b = $rgb & 0xFF.
- imagecolorsforindex(image, $color) - This "image color for index" function return RGB color values and alpha key
of the specified color index.
- imagesetpixel($image, $x,$y, $color) - This "image set pixel" function set one pixel at $x and $y with $color.
- imagefill($image, $x,$y, $color) - This "image fill" function performs a flood fill at $x and $y position with
a given $color.
- imagecolorallocate($image, $r,$g,$b) - This "image color allocate" function allocates a color index with $r, $g and $b
values. The allocated color index is returned.
- imagecolorallocatealpha($image, $r,$g,$b, $alpha) - This "image color allocate alpha" function does the same job
as imagecolorallocate() except that it applies a transparency parameter $alpha between
0, completely opaque, and 127, completely transparent.
- imagestring($image, $font, $x,$y, $string, $color) - This "image string" function draws a $string horizontally
at $x and $y with $font in $color.
- imagettftext($image, $size, $angle, $x,$y, $color, $font, $text) -
This "image TTF Text draws a $text at $x and $y with a $font of $size in a $color at an $angle.
- imageline($image, $x1,$y1, $x2,$y2, $color) - This "image line" function draws a line in a $color.
- imagerectangle($image, $x1,$y1, $x2,$y2, $color) - This "image rectangle" function draws a rectangle in a $color.
For more information, see GD Library Website at
https://www.php.net/manual/en/book.image.php.
If you want to use the GD library, you may need to turning on
the extension=gd2 (or extension=php_gd2.dll in PHP 5) setting in php.ini.
Table of Contents
About This Book
Introduction and Installation of PHP 7.3
PHP Script File Syntax
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
Arrays - Ordered Maps
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
►Image and Picture Processing
►GD Library of Image Functions
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
Managing PHP Engine and Modules on CentOS
Outdated Tutorials
References
Full Version in PDF/EPUB