Home > Backend Development > PHP Tutorial > Summary of common functions of PHP GD image processing components

Summary of common functions of PHP GD image processing components

高洛峰
Release: 2023-03-04 09:36:01
Original
1736 people have browsed it

Summary of common functions of PHP image processing component GD - Overview
PHP has a series of very powerful graphics processing functions, which are all included in the GD library. These functions have basically satisfied the conventional image processing of a network application. requirements and is very simple to use.
Many of our PHP friends (including me) think that these functions are not very commonly used anyway, and are too lazy to study or understand these functions. When faced with image processing, they are at a loss. , the time spent in calligraphy is too short!
This series of articles is to summarize PHP's image processing functions for you. It does not require mastery. I just hope that you can have a general impression of these functions. At the very least, when you have discussions or questions about image processing , you can think of these functions in your mind, so that everyone can be confident when thinking of solutions! There's a lot of nonsense!
This article is the beginning, so let's first talk about the GD library related to these functions, as well as the classification of the functions. The following articles will be detailed according to the classification.

PHP functions are all in the GD library. If you want to use the GD library, PHP must enable GD library support. Since this series of articles is not for novices, I will not talk about how to enable GD library support. La.

PHP’s image processing functions are roughly divided into several categories:
1. Basic information functions
Mainly the most basic functions such as image type, image width and height, and library version.
2. Image conversion function
Contains functions for converting between image formats
3. Image creation and destruction functions
Contains functions for various ways to create images and destroys image processing related resources Functions
4. Drawing operation functions
Include drawing-related functions, such as drawing lines, circles, squares, etc.
5. Image operation functions
Functions that perform some effect processing on images
6. Image setting function
Set some parameters of the image, such as: the width of the drawn line, whether the image is transparent, whether it is true color, etc.
7. Image text function
Write on the image Some functions
8. Image output function
After the image is done, it must be output. These functions are used for output. Where should it be output? Browsers, files, etc.

I’ll talk about these at the beginning, and the next few articles will talk about these functions by category.

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

Let’s talk about it in detail below!

gd_info
Get the information of the currently installed GD library and return the array
Array key meaning:
GD Version
string value. Describes the version of libgd installed.
Freetype Support
boolean value. TRUE if Freetype support is installed.
Freetype Linkage
string value. Describes Freetype connection methods. Possible values ​​are: 'with freetype', 'with TTF library' and 'with unknown library'. This unit is only defined when 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.
GIF Create 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.

For example:

<?php 
var_dump(gd_info()); 
?>
Copy after login

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) 
}
Copy after login

imagetypes
Returns the image types supported by the current PHP version

Prototype: int imagetypes ( void )

This function returns the image format supported by the GD library associated with the current PHP version in a bit field. The following results will be 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"; 
} 
?>
Copy after login

getimagesize
取得图像大小
原型:array getimagesize ( string filename [, array &imageinfo] )

测定任何GD库支持的图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 标记中的 height/width 文本字符串。

如果不能访问 filename 指定的图像或者其不是有效的图像,getimagesize() 将返回 FALSE 并产生一条 E_WARNING 级的错误。

返回一个具有四个单元的数组。

索引 0 包含图像宽度的像素值
索引 1 包含图像高度的像素值
索引 2 是图像类型的标记
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。
这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。
索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。


imagecolorat
取得某像素的颜色索引值

原型:int imagecolorat ( resource image, int x, int y )

返回 image 所指定的图形中指定位置像素的颜色索引值。

如果 PHP 编译时加上了 GD 库 2.0 或更高的版本并且图像是真彩色图像,则本函数以整数返回该点的 RGB 值。

如,用移位加掩码来取得红,绿,蓝各自成分的值:

<?php 
$im = ImageCreateFromPng("rockym.png"); 
$rgb = ImageColorAt($im, 100, 100); 
$r = ($rgb >> 16) & 0xFF; 
$g = ($rgb >> 8) & 0xFF; 
$b = $rgb & 0xFF; 
?>
Copy after login

这两个函数比较简单,取得图像宽度/高度 
原型如下: 
int imagesx ( resource image ) 
int imagesy ( resource image ) 

返回 image 所代表的图像的宽度/高度。 

更多PHP GD 图像处理组件的常用函数总结相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template