以下是PHP中GD库核心函数的详细解析及语法说明:
一、图像创建与销毁函数
imagecreatetruecolor()
$img = imagecreatetruecolor(int $width, int $height);
创建真彩色画布(返回图像资源标识符)
示例:
$img = imagecreatetruecolor(500, 300);
imagecreate()
$img = imagecreate(int $width, int $height);
创建基于调色板的图像(兼容旧版本)
imagedestroy()
imagedestroy(resource $image);
释放图像内存资源
二、颜色管理函数
imagecolorallocate()
$color = imagecolorallocate(resource $image, int $red, int $green, int $blue);
为图像分配颜色(RGB值范围0-255)
示例:
$red = imagecolorallocate($img, 255, 0, 0);
imagecolorallocatealpha()
$color = imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha);
分配带透明度的颜色(alpha值0-127)
三、绘图与文本函数
imageline()
imageline(resource $image, int $x1, int $y1, int $x2, int $y2, int $color);
绘制直线(起点和终点坐标)
imagestring()
imagestring(resource $image, int $font, int $x, int $y, string $text, int $color);
绘制水平文本(内置字体1-5)
imagettftext()
imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text);
使用TrueType字体绘制文本
四、图像输出函数
imagepng()
imagepng(resource $image [, string $filename]);
输出PNG格式图像(直接显示或保存)
imagejpeg()
imagejpeg(resource $image [, string $filename [, int $quality]]);
输出JPEG图像(可设置质量1-100)
imagegif()
imagegif(resource $image [, string $filename]);
输出GIF格式图像
五、辅助函数
gd_info()
array gd_info();
获取GD库版本及支持格式
imagesx()/imagesy()
int imagesx(resource $image); // 获取图像宽度 int imagesy(resource $image); // 获取图像高度
读取图像尺寸
六、函数总结表
以上函数覆盖了GD库的核心功能,通过组合使用可实现验证码生成、图像水印等复杂操作。
能否再详细讲解几个GD库函数?
以下是PHP GD库中几个关键函数的详细解析(基于最新GD 2.3.3版本):
1. imagecopyresampled() 图像高质量缩放
bool imagecopyresampled( resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h );
功能:将源图像矩形区域重新采样后复制到目标图像,支持平滑缩放
参数说明:
$dst_image:目标图像资源$src_image:源图像资源$dst_x/$dst_y:目标起始坐标$src_x/$src_y:源起始坐标$dst_w/$dst_h:目标区域宽高$src_w/$src_h:源区域宽高示例:生成缩略图时保持平滑度
$src = imagecreatefromjpeg('photo.jpg');
$dst = imagecreatetruecolor(100, 100);
imagecopyresampled($dst, $src, 0, 0, 0, 0, 100, 100, imagesx($src), imagesy($src));2. imagefilledpolygon() 复杂多边形绘制
bool imagefilledpolygon( resource $image, array $points, int $num_points, int $color );
功能:绘制实心多边形,支持任意边数
参数说明:
$points:顶点坐标数组(格式:[x1,y1, x2,y2,...])$num_points:顶点总数(≥3)示例:绘制红色五角星
$img = imagecreatetruecolor(200, 200); $red = imagecolorallocate($img, 255, 0, 0); $points = [100,10, 120,90, 200,90, 130,140, 160,220, 100,170, 40,220, 70,140, 0,90, 80,90]; imagefilledpolygon($img, $points, 10, $red);
3. imagettftext() TrueType字体渲染
array imagettftext( resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text );
功能:用矢量字体绘制抗锯齿文本
关键参数:
$fontfile:字体文件路径(如arial.ttf)$angle:旋转角度(0-360度)返回值:文本边界框坐标数组(8个元素)
示例:倾斜文字水印
$img = imagecreatefrompng('background.png');
$black = imagecolorallocate($img, 0, 0, 0);
imagettftext($img, 20, 45, 50, 100, $black, 'arial.ttf', 'Confidential');4. imagefilter() 图像特效处理
bool imagefilter( resource $image, int $filtertype, [int $arg1], [int $arg2], [int $arg3], [int $arg4] );
常用滤镜类型:
示例:实现老照片效果
$img = imagecreatefromjpeg('portrait.jpg');
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_COLORIZE, 100, 50, 0);