您好!欢迎访问家园网-www.jy.wang!

家园网

php中GD库有函数?请全部列出,逐一列出,并举例讲解每一个语法结构。最后用表格进行总结

网络 作者:本站 点击:

以下是PHP中GD库核心函数的详细解析及语法说明:


一、图像创建与销毁函数

  1. imagecreatetruecolor()

$img = imagecreatetruecolor(int $width, int $height);
    • 创建真彩色画布(返回图像资源标识符)

    • 示例:$img = imagecreatetruecolor(500, 300);

  1. imagecreate()

$img = imagecreate(int $width, int $height);
    • 创建基于调色板的图像(兼容旧版本)

  1. imagedestroy()

imagedestroy(resource $image);
    • 释放图像内存资源


二、颜色管理函数

  1. imagecolorallocate()

$color = imagecolorallocate(resource $image, int $red, int $green, int $blue);
    • 为图像分配颜色(RGB值范围0-255)

    • 示例:$red = imagecolorallocate($img, 255, 0, 0);

  1. imagecolorallocatealpha()

$color = imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha);
    • 分配带透明度的颜色(alpha值0-127)


三、绘图与文本函数

  1. imageline()

imageline(resource $image, int $x1, int $y1, int $x2, int $y2, int $color);
    • 绘制直线(起点和终点坐标)

  1. imagestring()

imagestring(resource $image, int $font, int $x, int $y, string $text, int $color);
    • 绘制水平文本(内置字体1-5)

  1. imagettftext()

imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text);
    • 使用TrueType字体绘制文本


四、图像输出函数

  1. imagepng()

imagepng(resource $image [, string $filename]);
    • 输出PNG格式图像(直接显示或保存)

  1. imagejpeg()

imagejpeg(resource $image [, string $filename [, int $quality]]);
    • 输出JPEG图像(可设置质量1-100)

  1. imagegif()

imagegif(resource $image [, string $filename]);
    • 输出GIF格式图像


五、辅助函数

  1. gd_info()

array gd_info();
    • 获取GD库版本及支持格式

  1. imagesx() / imagesy()

int imagesx(resource $image);  // 获取图像宽度
int imagesy(resource $image);  // 获取图像高度
    • 读取图像尺寸


六、函数总结表

函数分类函数名作用描述示例参数格式
图像创建imagecreatetruecolor创建真彩色画布(500, 300)
颜色管理imagecolorallocate分配颜色($img, 255, 0, 0)
文本绘制imagettftext使用TTF字体绘制文本($img, 20, 0, 10, 30, $color, 'arial.ttf', 'Hello')
图像输出imagepng输出PNG图像($img, 'output.png')
信息获取gd_info获取GD库信息无参数

以上函数覆盖了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_FILTER_GRAYSCALE灰度化无参数
    IMG_FILTER_COLORIZE着色RGB+alpha值
    IMG_FILTER_GAUSSIAN_BLUR高斯模糊无参数
  • 示例‌:实现老照片效果

$img = imagecreatefromjpeg('portrait.jpg');
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagefilter($img, IMG_FILTER_COLORIZE, 100, 50, 0);

函数对比表

函数核心用途是否支持透明典型场景
imagecopyresampled高质量缩放缩略图生成
imagefilledpolygon矢量图形绘制图表绘制
imagettftext抗锯齿文字验证码/水印
imagefilter实时特效图片滤镜



标签: