PHP Modules Tutorials - Herong's Tutorial Examples
∟GD Module - Manipulating Images and Pictures
∟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.
What Is GD Module? -
GD (Graphic Draw) Module, also called GD Extension or GD Library,
allows you to create and manipulate image files in different formats,
including GIF, PNG, JPEG, and more.
Zip Module was introduced in PHP 4.3.
GD module is included in most PHP distribution packages.
On some versions, you may need to enable it explicitly in the php.ini configuration file
using one of the following settings:
extension=gd2
extension=gd2.so
extension=php_gd2.dll
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 module documentation at
php.net/manual/en/book.image.php.
Table of Contents
About This Book
Introduction and Installation of PHP
Managing PHP Engine and Modules on macOS
Managing PHP Engine and Modules on CentOS
cURL Module - Client for URL
DOM Module - Parsing HTML Documents
►GD Module - Manipulating Images and Pictures
►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
MySQLi Module - Accessing MySQL Server
OpenSSL Module - Cryptography and SSL/TLS Toolkit
PCRE Module - Perl Compatible Regular Expressions
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples
Zip Module - Managing ZIP Archive Files
References
Full Version in PDF/EPUB