search
HomeBackend DevelopmentPHP TutorialPHP GD 图像处理组件的常用函数总结_php技巧

PHP图像处理组件GD的常用函数总结——概述
PHP有一系列很强大的图形处理函数,他们都统一包含在GD库中,这些函数已经基本满足了一个网络应用的常规图像处理要求,而且使用十分简单。
而我们有好多搞PHP的朋友(包括我)都认为这些函数反正不是很常用,都懒的去研究或者了解这些函数了,而当要面临图像处理方面的事情的时候,又很茫然,书道用时方恨少啊!
本系列这几篇文章就是给大家归纳一下PHP的图像处理函数,不要求掌握,只希望能让各位对这些函数有个大体印象,最起码,有图像处理方面的讨论或者问题的时候,心里能想起这些函数,这样大家在想解决方案的时候就能做到胸有成竹啦!废话有点多!
本篇是开篇,所以先将一下和这些函数相关的GD库,还有函数的分类,之后的文章将根据分类,来具体说。

PHP函数都是在GD库中的,要想使用GD库,PHP要开启GD库支持,由于考虑本系列文章面对的不是菜鸟所以,就不讲如何开启GD库的支持啦。

PHP的图像处理函数大概分为几类:
1.基本信息函数
主要是图像类型,图像宽高,库版本等最基本的函数。
2.图像转换函数
包含图像格式之间的相互转换函数
3.图像创建和销毁函数
包含图像各种创建图像的方式的函数还有销毁图像处理相关资源的函数
4.画图操作函数
包含画图相关函数,如画线,画圆,画方形等函数
5.图像操作函数
对图像进行一些效果处理的函数
6.图像设定函数
设置图像的一些参数,比如:画线的宽度啊、图像是否透明啊、是否真彩啊等等
7.图像文字函数
在图像上写字的一些函数
8.图像输出函数
图像弄好了,总得输出吧,这些函数就是用来输出的,输出到哪里?浏览器、文件等

开篇就讲这些啦,接下来的几篇就分类讲这些函数啦。

PHP图像处理组件GD的常用函数总结——基本信息函数
基本信息函数主要有以下几个:
gd_info
当前PHP环境GD库的基本信息
imagetypes
支持的图像类型
getimagesize
获取一个图像的大小
imagecolorat
取得图像的某个像素的颜色索引值
imagesx
取得图像宽度
imagesy
取得图像高度

下面就具体来讲述啦!

gd_info
取得当前安装的GD库的信息,返回数组
数组键含义:
GD Version
string 值。描述了安装的 libgd 的版本。
Freetype Support
boolean 值。如果安装了 Freetype 支持则为 TRUE。
Freetype Linkage
string 值。描述了 Freetype 连接的方法。取值可能为:'with freetype', 'with TTF library' 和 'with unknown library'。本单元仅在 Freetype Support 的值为 TRUE 时有定义。
T1Lib Support
boolean 值。如果包含有 T1Lib 支持则为 TRUE。
GIF Read Support
boolean 值。如果包含有读取 GIF 图像的支持则为 TRUE。
GIF Create Support
boolean 值。如果包含有创建 GIF 图像的支持则为 TRUE。
JPG Support
boolean 值。如果包含有 JPG 支持则为 TRUE。
PNG Support
boolean 值。如果包含有 PNG 支持则为 TRUE。
WBMP Support
boolean 值。如果包含有 WBMP 支持则为 TRUE。
XBM Support
boolean 值。如果包含有 XBM 支持则为 TRUE。

如:
复制代码 代码如下:

var_dump(gd_info());
?>



输出为:
复制代码 代码如下:

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
返回当前 PHP 版本所支持的图像类型

原型:int imagetypes ( void )

本函数以比特字段方式返回与当前 PHP 版本关联的 GD 库所支持的图像格式。将返回以下结果,IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP| IMG_XPM。

如:检查是否支持 PNG
复制代码 代码如下:

if (imagetypes() & IMG_PNG) {
echo "PNG Support is enabled";
}
?>

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

测定任何GD库支持的图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 PHP GD 图像处理组件的常用函数总结_php技巧 标记中的 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 值。

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

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


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

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

转载自 http://www.sourcejoy.com/
Statement
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
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.