Summary of common functions for the PHP GD image processing component

  • 2020-03-31 20:43:00
  • OfStack

Overview of common functions of GD, a PHP image processing component
PHP has a series of powerful graphics functions, all of which are included in the GD library. These functions basically meet the general image processing requirements of a web application, and they are easy to use.
And we have a lot of friends who do PHP (including me) think that these functions are not very commonly used anyway, are lazy to study or understand these functions, and when it comes to face with the image processing aspects of things, and very confused, the book used to hate!
This series is to give you summary of these passages PHP image processing function, does not require the master, only hope can let everybody had a general impression for these functions, at the very least, there are image processing aspects of the discussion or problem, can the in the mind think of these functions, so that everyone in the want to solution can do it well! A little too much nonsense!
This is the beginning of the article, so I will first discuss the GD library related to these functions, and the classification of functions, and then I will talk about the classification.

PHP functions are all in the GD library, and to use the GD library, PHP needs to turn on the GD library support, so I won't talk about how to turn on the GD library support since we are not dealing with newbies in this series.

PHP image processing functions can be roughly divided into several categories:
1. Basic information function
Mainly is the image type, image width and height, library version and other basic functions.
2. Image conversion function
Contains conversion functions between image formats
3. Image creation and destruction functions
Functions that contain various ways to create images and functions that destroy resources related to image processing
4. Draw operation functions
It contains drawing function, drawing line, drawing circle, drawing square and so on
5. Image manipulation function
A function that performs some effects on an image
6. Image setting function
Set some parameters of the image, such as the width of the line, whether the image is transparent, true color, etc
7. Image text function
Some function that writes on a graph
8. Image output function
I'm done with the graph, so I have to output it, and that's what these functions are for. Where is the output? Browser, file, etc

So that's what we're going to talk about in the beginning, and we're going to talk about these functions in the next few chapters.

Summary of common functions for PHP image processing component GD - basic information functions
The basic information functions mainly include the following:
gd_info
Basic information about the GD library in the current PHP environment
imagetypes
Supported image types
getimagesize
Gets the size of an image
imagecolorat
Gets the color index value of a pixel in the image
imagesx
Get image width
imagesy
Get image height

The following is specific to tell about!

gd_info
Gets information about the currently installed GD library and returns an array
Array key meaning:
GD Version
A string value. Describes the version of libgd installed.
Freetype Support
Boolean value. TRUE if Freetype support is installed.
Freetype Linkage
A string value. Describes the method of Freetype connection. Values can be 'with freetype', 'with TTF library', and 'with unknown library'. This unit is defined only if the value of Freetype Support is TRUE.
T1Lib Support
Boolean value. TRUE if T1Lib support is included.
GIF Read Support
Boolean value. TRUE if support for reading GIF images is included.
The Create GIF Support
Boolean value. TRUE if support for creating GIF images is included.
JPG Support
Boolean value. TRUE if JPG support is included.
PNG Support
Boolean value. TRUE if PNG support is included.
WBMP Support
Boolean value. TRUE if WBMP support is included.
XBM Support
Boolean value. TRUE if XBM support is included.

Such as:
 
<?php 
var_dump(gd_info()); 
?> 



The output is:
 
array(9) { 
["GD Version"]=> 
string(24) "bundled (2.0 compatible)" 
["FreeType Support"]=> 
bool(false) 
["T1Lib Support"]=> 
bool(false) 
["GIF Read Support"]=> 
bool(true) 
["GIF Create Support"]=> 
bool(false) 
["JPG Support"]=> 
bool(false) 
["PNG Support"]=> 
bool(true) 
["WBMP Support"]=> 
bool(true) 
["XBM Support"]=> 
bool(false) 
} 

imagetypes
Returns the image type supported by the current PHP version

Int imagetypes (void)

This function returns the image format supported by the GD library associated with the current PHP version as a bit field. The following results are returned, IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP| IMG_XPM.

For example, check whether PNG is supported
 
<?php 
if (imagetypes() & IMG_PNG) { 
echo "PNG Support is enabled"; 
} 
?> 

getimagesize
Get image size
Prototype: array getimagesize (string filename [, array &imageinfo])

Determines the size of any image file supported by the GD library and returns the size of the image as well as the file type and one that can be used in plain HTML files. IMG> Height /width text string in the tag.

If the image specified by filename cannot be accessed or is not a valid image, getimagesize() returns FALSE and generates an E_WARNING error.

Returns an array with four cells.

Index 0 contains the pixel value of the image width
Index 1 contains pixel values for the height of the image
Index 2 is a marker for the image type
1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(Intel byte order), 8 = TIFF(MOTOROLA byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.
These tags correspond to the new IMAGETYPE constant in PHP 4.3.0.
The index 3 is a text string with the content "height="yyy" width=" XXX" ", which can be used directly on the IMG tag.


imagecolorat
Gets the color index value of a pixel

Prototype: int imagecolorat (resource image, int x, int y)

Returns the color index value of the pixel at the specified position in the graph specified by the image.

If PHP is compiled with the GD library 2.0 or later and the image is a true-color image, this function returns the RGB value of the point as an integer.

For example, shift and mask are used to obtain the values of red, green and blue components:
 
<?php 
$im = ImageCreateFromPng("rockym.png"); 
$rgb = ImageColorAt($im, 100, 100); 
$r = ($rgb >> 16) & 0xFF; 
$g = ($rgb >> 8) & 0xFF; 
$b = $rgb & 0xFF; 
?> 


Imagesx/imagesy
These two functions are relatively simple and get the width/height of the image
The prototype is as follows:
Int imagesx (resource image)
Int imagesy (resource image)

Returns the width/height of the image represented by the image.

Reprinted from (link: http://www.sourcejoy.com/)

Related articles: