请讲解下面的GD库函数:绘图与文本13个
imageline() - 绘制直线
imagerectangle() - 绘制矩形
imagefilledrectangle() - 绘制实心矩形
imagepolygon() - 绘制多边形
imagefilledpolygon() - 绘制实心多边形
imagearc() - 绘制椭圆弧
imageellipse() - 绘制椭圆
imagefilledellipse() - 绘制实心椭圆
imagestring() - 绘制水平文本(内置字体)
imagestringup() - 绘制垂直文本(内置字体)
imagettftext() - 用TrueType字体绘制文本
imagechar() - 绘制单个水平字符
imagecharup() - 绘制单个垂直字符
GD库绘图与文本函数详解
根据您的要求,我将继续按照之前的形式,详细讲解GD库中用于绘图与文本处理的相关函数。以下是12个核心函数的详细解析,每个函数都包含语法结构、参数说明、示例代码及逐行解析。
一、imageline() - 绘制直线
语法结构
bool imageline( resource $image, int $x1, int $y1, int $x2, int $y2, int $color )
参数说明
$image: 图像资源$x1: 起点x坐标$y1: 起点y坐标$x2: 终点x坐标$y2: 终点y坐标$color: 线条颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x400图像
$image = imagecreatetruecolor(400, 400);
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 255, 0, 0);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 绘制十字线
imageline($image, 0, 200, 400, 200, $red); // 水平线
imageline($image, 200, 0, 200, 400, $red); // 垂直线
// 绘制对角线
imageline($image, 0, 0, 400, 400, $red); // 主对角线
imageline($image, 400, 0, 0, 400, $red); // 副对角线
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建400x400真彩色图像
分配白色和红色
填充白色背景
绘制红色十字线(水平和垂直)
绘制两条红色对角线
输出PNG图像
释放资源
注意事项
坐标超出图像范围会被自动裁剪
抗锯齿效果需要额外处理
线宽固定为1像素
二、imagerectangle() - 绘制矩形
语法结构
bool imagerectangle( resource $image, int $x1, int $y1, int $x2, int $y2, int $color )
参数说明
$image: 图像资源$x1: 左上角x坐标$y1: 左上角y坐标$x2: 右下角x坐标$y2: 右下角y坐标$color: 边框颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建300x300图像
$image = imagecreatetruecolor(300, 300);
$white = imagecolorallocate($image, 255, 255, 255);
$blue = imagecolorallocate($image, 0, 0, 255);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 绘制嵌套矩形
for ($i = 0; $i < 10; $i++) {
$offset = $i * 15;
imagerectangle($image, $offset, $offset, 300-$offset, 300-$offset, $blue);
}
// 输出
header('Content-Type: image/jpeg');
imagejpeg($image, null, 90);
imagedestroy($image);
?>代码解析:
创建300x300图像
分配白色和蓝色
填充白色背景
使用循环绘制10个嵌套蓝色矩形
输出JPEG图像(质量90)
释放资源
注意事项
矩形边框宽度固定为1像素
(x1,y1)和(x2,y2)可以任意顺序指定
填充矩形应使用
imagefilledrectangle()
三、imagefilledrectangle() - 绘制实心矩形
语法结构
bool imagefilledrectangle( resource $image, int $x1, int $y1, int $x2, int $y2, int $color )
参数说明
参数同imagerectangle()
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建256x256图像(用于颜色渐变)
$image = imagecreatetruecolor(256, 256);
// 创建颜色渐变(红到绿)
for ($x = 0; $x < 256; $x++) {
$color = imagecolorallocate($image, $x, 255-$x, 0);
imagefilledrectangle($image, $x, 0, $x, 255, $color);
}
// 添加文字
$text_color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 5, 80, 120, "Color Gradient", $text_color);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建256x256图像
使用循环创建从左到右的红绿渐变
每个垂直条1像素宽
添加白色文字
输出PNG图像
释放资源
注意事项
比绘制多个像素点效率更高
适合创建色块和背景
可以用于快速填充大面积区域
四、imagepolygon() - 绘制多边形
语法结构
bool imagepolygon( resource $image, array $points, int $num_points, int $color )
参数说明
$image: 图像资源$points: 顶点坐标数组,格式为[x1,y1,x2,y2,...]$num_points: 顶点总数(必须≥3)$color: 边框颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x400图像
$image = imagecreatetruecolor(400, 400);
$white = imagecolorallocate($image, 255, 255, 255);
$purple = imagecolorallocate($image, 128, 0, 128);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 定义五边形顶点
$points = [
200, 50, // 顶点1
350, 150, // 顶点2
300, 350, // 顶点3
100, 350, // 顶点4
50, 150 // 顶点5
];
// 绘制紫色五边形
imagepolygon($image, $points, 5, $purple);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建400x400图像
分配白色和紫色
填充白色背景
定义五边形的5个顶点坐标
绘制紫色五边形边框
输出PNG图像
释放资源
注意事项
顶点必须按顺序排列(顺时针或逆时针)
会自动闭合图形(连接首尾点)
填充多边形应使用
imagefilledpolygon()
五、imagefilledpolygon() - 绘制实心多边形
语法结构
bool imagefilledpolygon( resource $image, array $points, int $num_points, int $color )
参数说明
参数同imagepolygon()
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建500x500图像
$image = imagecreatetruecolor(500, 500);
$black = imagecolorallocate($image, 0, 0, 0);
$yellow = imagecolorallocate($image, 255, 255, 0);
// 填充黑色背景
imagefill($image, 0, 0, $black);
// 定义星形顶点(五角星)
$points = [
250, 50, // 顶点1
294, 182, // 顶点2
430, 182, // 顶点3
318, 268, // 顶点4
360, 400, // 顶点5
250, 330, // 顶点6
140, 400, // 顶点7
182, 268, // 顶点8
70, 182, // 顶点9
206, 182 // 顶点10
];
// 绘制黄色实心五角星
imagefilledpolygon($image, $points, 10, $yellow);
// 输出
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>代码解析:
创建500x500图像
分配黑色和黄色
填充黑色背景
定义五角星的10个顶点坐标
绘制黄色实心五角星
输出JPEG图像
释放资源
注意事项
适合绘制复杂形状
奇偶填充规则决定重叠区域
顶点顺序影响填充效果
六、imagearc() - 绘制椭圆弧
语法结构
bool imagearc( resource $image, int $cx, int $cy, int $width, int $height, int $start, int $end, int $color )
参数说明
$image: 图像资源$cx: 中心点x坐标$cy: 中心点y坐标$width: 椭圆宽度$height: 椭圆高度$start: 起始角度(0-360)$end: 结束角度(0-360)$color: 弧线颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x400图像
$image = imagecreatetruecolor(400, 400);
$white = imagecolorallocate($image, 255, 255, 255);
$colors = [
imagecolorallocate($image, 255, 0, 0),
imagecolorallocate($image, 0, 255, 0),
imagecolorallocate($image, 0, 0, 255)
];
// 填充白色背景
imagefill($image, 0, 0, $white);
// 绘制彩色圆弧(类似饼图)
$start_angle = 0;
foreach ([60, 120, 180] as $i => $angle) {
imagearc($image, 200, 200, 300, 300, $start_angle, $start_angle+$angle, $colors[$i]);
$start_angle += $angle;
}
// 添加中心点
imagefilledellipse($image, 200, 200, 20, 20, $white);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建400x400图像
分配白色和红绿蓝三色
填充白色背景
绘制三个相连的彩色圆弧(60°,120°,180°)
在中心添加白色小圆点
输出PNG图像
释放资源
注意事项
角度0表示3点钟方向
顺时针方向绘制
要画完整椭圆可设start=0,end=360
七、imageellipse() - 绘制椭圆
语法结构
bool imageellipse( resource $image, int $cx, int $cy, int $width, int $height, int $color )
参数说明
$image: 图像资源$cx: 中心点x坐标$cy: 中心点y坐标$width: 椭圆宽度$height: 椭圆高度$color: 边框颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建500x300图像
$image = imagecreatetruecolor(500, 300);
$white = imagecolorallocate($image, 255, 255, 255);
$gray = imagecolorallocate($image, 128, 128, 128);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 绘制同心椭圆
for ($i = 1; $i <= 5; $i++) {
$size = $i * 50;
imageellipse($image, 250, 150, $size+200, $size, $gray);
}
// 添加文字
imagestring($image, 5, 180, 280, "Ellipse Examples", $gray);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建500x300图像
分配白色和灰色
填充白色背景
绘制5个同心椭圆(逐渐增大)
添加灰色文字
输出PNG图像
释放资源
注意事项
width=height时为圆形
边框宽度固定为1像素
填充椭圆应使用
imagefilledellipse()
八、imagefilledellipse() - 绘制实心椭圆
语法结构
bool imagefilledellipse( resource $image, int $cx, int $cy, int $width, int $height, int $color )
参数说明
参数同imageellipse()
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x400图像
$image = imagecreatetruecolor(400, 400);
$black = imagecolorallocate($image, 0, 0, 0);
$colors = [
imagecolorallocate($image, 255, 0, 0),
imagecolorallocate($image, 0, 255, 0),
imagecolorallocate($image, 0, 0, 255)
];
// 填充黑色背景
imagefill($image, 0, 0, $black);
// 绘制奥运五环
$positions = [
[100, 100, 80], // 蓝环
[200, 100, 80], // 黄环
[300, 100, 80], // 黑环
[150, 170, 80], // 绿环
[250, 170, 80] // 红环
];
foreach ($positions as $i => $pos) {
imagefilledellipse($image, $pos[0], $pos[1], $pos[2], $pos[2], $colors[$i % 3]);
}
// 输出
header('Content-Type: image/jpeg');
imagejpeg($image, null, 95);
imagedestroy($image);
?>代码解析:
创建400x400图像
分配黑色和红绿蓝三色
填充黑色背景
定义五环位置和大小
绘制彩色实心圆环(奥运五环样式)
输出高质量JPEG
释放资源
注意事项
适合绘制气泡、圆点等元素
比绘制多个像素点效率更高
抗锯齿效果需要额外处理
九、imagestring() - 绘制水平文本(内置字体)
语法结构
bool imagestring( resource $image, int $font, int $x, int $y, string $text, int $color )
参数说明
$image: 图像资源$font: 字体编号(1-5对应不同大小)$x: 文本左上角x坐标$y: 文本左上角y坐标$text: 要绘制的文本$color: 文本颜色索引
内置字体大小
1: 8x13像素
2: 6x13像素
3: 7x13像素(粗体)
4: 8x13像素(更大)
5: 9x15像素
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x200图像
$image = imagecreatetruecolor(400, 200);
$white = imagecolorallocate($image, 255, 255, 255);
$blue = imagecolorallocate($image, 0, 0, 255);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 显示所有内置字体
$text = "Built-in Fonts";
$y = 20; // 起始Y坐标
for ($font = 1; $font <= 5; $font++) {
// 使用5种不同的内置字体绘制文本
imagestring($image, $font, 50, $y, "$font: $text", $blue);
// 计算行高:当前字体高度 + 10像素间距
$font_height = imagefontheight($font);
$y += $font_height + 10;
}
// 绘制一段多行文本
$lines = [
"This is the first line.",
"Second line with font 3",
"Third line using font 4"
];
$y = 120; // 新的起始Y坐标
foreach ($lines as $index => $line) {
// 交替使用字体3、4、5
$font = ($index % 3) + 3;
// 绘制文本行
imagestring($image, $font, 50, $y, $line, $blue);
// 计算行高:当前字体高度 + 5像素间距
$font_height = imagefontheight($font);
$y += $font_height + 5;
}
// 添加版权信息到右下角
$copyright = "© " . date("Y") . " PHP GD Library";
$font_width = imagefontwidth(2);
$str_width = strlen($copyright) * $font_width;
$x_position = 400 - $str_width - 10; // 右边距10像素
imagestring($image, 2, $x_position, 180, $copyright, $blue);
// 输出PNG图像
header('Content-Type: image/png');
imagepng($image);
// 释放资源
imagedestroy($image);
?>这个完整示例展示了如何:
使用所有内置字体
创建多行文本
计算文本位置
添加自动版权信息
合理处理行间距
完整代码解析:
图像初始化:
创建400×200像素的真彩色图像资源
分配白色和蓝色颜色资源
填充整个图像背景为白色
字体展示部分:
定义文本内容"Built-in Fonts"
使用循环展示5种内置字体(1-5)
$y变量控制每行的垂直位置imagefontheight($font)获取每种字体的高度每行增加字体高度+10像素的间距
多行文本绘制:
定义三行文本内容
交替使用字体3、4、5
foreach循环逐行绘制每行增加字体高度+5像素的间距
版权信息:
创建包含当前年份的版权文本
imagefontwidth(2)获取2号字体字符宽度计算文本总宽度:字符数×字符宽度
计算右下角位置:图像宽度-文本宽度-右边距
使用2号字体绘制版权信息
输出与清理:
设置Content-Type为image/png
输出PNG格式图像
销毁图像资源释放内存
注意事项:
内置字体尺寸:
1号:8×13像素
2号:6×13像素
3号:7×13像素(粗体)
4号:8×13像素(更大)
5号:9×15像素
字体尺寸函数:
imagefontheight($font):获取字体高度imagefontwidth($font):获取字符宽度文本位置计算:
左上角定位:给定坐标是文本左上角位置
垂直间距:建议使用字体高度+额外间距
水平居中:(图像宽度 - 文本总宽度)/2
局限性:
不支持Unicode字符
字体样式固定
无法旋转文本
性能考虑:
比TrueType字体渲染更快
适合简单标签和低资源环境
多次调用效率高于单次长文本
十、imagestringup() - 绘制垂直文本(内置字体)
语法结构
bool imagestringup( resource $image, int $font, int $x, int $y, string $text, int $color )
参数说明
$image: 图像资源$font: 字体编号(1-5对应不同大小)$x: 文本左上角x坐标$y: 文本左上角y坐标$text: 要绘制的文本$color: 文本颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建300x300图像
$image = imagecreatetruecolor(300, 300);
$white = imagecolorallocate($image, 255, 255, 255);
$purple = imagecolorallocate($image, 128, 0, 128);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 绘制垂直文本标签
$labels = ['North', 'East', 'South', 'West'];
$positions = [
[150, 50], // 北
[270, 150], // 东
[150, 270], // 南
[30, 150] // 西
];
foreach ($positions as $i => $pos) {
imagestringup($image, 4, $pos[0], $pos[1], $labels[$i], $purple);
}
// 添加中心点
imagefilledellipse($image, 150, 150, 20, 20, $purple);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建300x300图像
分配白色和紫色
填充白色背景
定义四个方向的标签位置
使用4号字体绘制垂直文本
在中心添加紫色圆点
输出PNG图像
释放资源
注意事项
文本从上往下垂直排列
适合创建图表标签和特殊效果
字体选择有限,不支持中文
十一、imagettftext() - 用TrueType字体绘制文本
语法结构
array imagettftext( resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text )
参数说明
$image: 图像资源$size: 字体大小(磅值)$angle: 文本角度(0-360度,0为水平)$x: 文本基线的x坐标$y: 文本基线的y坐标$color: 文本颜色索引$fontfile: TrueType字体文件路径$text: 要绘制的文本
返回值
返回包含8个元素的数组,表示文本边界框的四个角点坐标。
示例代码及解析
<?php
// 创建600x400图像
$image = imagecreatetruecolor(600, 400);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// 填充白色背景
imagefill($image, 0, 0, $white);
// 设置字体文件路径
$font = 'arial.ttf'; // 确保该字体文件存在
// 绘制各种角度的文本
$texts = [
[30, "Welcome to PHP", 100, 100, 0],
[24, "旋转的文字", 300, 150, 45],
[20, "GD库功能强大", 200, 300, -15],
[18, "图像处理示例", 500, 350, 90]
];
foreach ($texts as $item) {
list($size, $text, $x, $y, $angle) = $item;
imagettftext($image, $size, $angle, $x, $y, $black, $font, $text);
}
// 添加水印
$watermark = imagecolorallocatealpha($image, 128, 128, 128, 60);
imagettftext($image, 14, 0, 20, 390, $watermark, $font, "© 2023 GD Library Demo");
// 输出
header('Content-Type: image/jpeg');
imagejpeg($image, null, 95);
imagedestroy($image);
?>代码解析:
创建600x400图像
分配白色和黑色
填充白色背景
设置TrueType字体文件
绘制不同大小、位置和角度的文本
添加半透明灰色水印
输出高质量JPEG
释放资源
注意事项
字体文件路径可以是相对或绝对路径
$x和$y是基线起点,不是左上角支持UTF-8编码的多语言文本
需要FreeType库支持
十二、imagechar() - 绘制单个水平字符
语法结构
bool imagechar( resource $image, int $font, int $x, int $y, string $char, int $color )
参数说明
$image: 图像资源$font: 字体编号(1-5)$x: 字符左上角x坐标$y: 字符左上角y坐标$char: 要绘制的单个字符$color: 字符颜色索引
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建400x200图像
$image = imagecreatetruecolor(400, 200);
$gray = imagecolorallocate($image, 200, 200, 200);
$dark_red = imagecolorallocate($image, 128, 0, 0);
// 填充灰色背景
imagefill($image, 0, 0, $gray);
// 绘制ASCII艺术字
$text = "PHP GD";
$char_width = imagefontwidth(5); // 9像素
$char_height = imagefontheight(5); // 15像素
$x_start = (400 - strlen($text) * $char_width) / 2;
$y_start = (200 - $char_height) / 2;
for ($i = 0; $i < strlen($text); $i++) {
$x = $x_start + $i * $char_width;
imagechar($image, 5, $x, $y_start, $text[$i], $dark_red);
}
// 添加装饰线
imageline($image, $x_start - 10, $y_start + 7, $x_start + strlen($text)*$char_width + 10, $y_start + 7, $dark_red);
// 输出
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>代码解析:
创建400x200图像
分配灰色和深红色
填充灰色背景
计算文本居中位置
使用5号字体逐个字符绘制文本
在文本下方添加装饰线
输出PNG图像
释放资源
注意事项
适合创建ASCII艺术效果
只能绘制单个字符
比绘制整个字符串更灵活
十三、imagecharup() - 绘制单个垂直字符
语法结构
bool imagecharup( resource $image, int $font, int $x, int $y, string $char, int $color )
参数说明
参数同imagechar()
返回值
成功返回true,失败返回false。
示例代码及解析
<?php
// 创建300x300图像
$image = imagecreatetruecolor(300, 300);
$light_blue = imagecolorallocate($image, 200, 230, 255);
$navy = imagecolorallocate($image, 0, 0, 128);
// 填充浅蓝色背景
imagefill($image, 0, 0, $light_blue);
// 绘制垂直条形码效果
$chars = '0123456789ABCDEF';
$x = 50;
for ($i = 0; $i < 15; $i++) {
$char = $chars[rand(0, strlen($chars)-1)];
$y = 50;
for ($j = 0; $j < 8; $j++) {
imagecharup($image, 3, $x, $y, $char, $navy);
$y += imagefontheight(3) + 2;
}
$x += imagefontwidth(3) + 5;
}
// 添加标题
imagestring($image, 5, 100, 270, "Vertical Barcode", $navy);
// 输出
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>代码解析:
创建300x300图像
分配浅蓝和深蓝色
填充浅蓝色背景
生成随机字符序列
绘制8个垂直排列的字符组(模拟条形码)
添加水平标题文本
输出JPEG图像
释放资源
注意事项
字符从上往下垂直排列
适合创建特殊视觉效果
比水平字符更节省水平空间
综合比较表:文本绘制函数
综合比较表:图形绘制函数
通用注意事项
坐标系统:GD库使用左上角(0,0)坐标系
颜色管理:确保颜色已分配后再使用
资源释放:使用后及时调用
imagedestroy()字体路径:使用TrueType字体时确保路径正确
内存限制:大图像可能超出内存限制
输出格式:输出前设置正确的Content-Type头
错误处理:检查函数返回值避免静默失败
以上详细讲解了GD库中的13个绘图与文本函数,包括直线、矩形、多边形、椭圆等图形绘制,以及水平和垂直文本绘制功能。每个函数都包含语法结构、参数说明、示例代码和注意事项。掌握这些函数可以实现从简单的几何图形到复杂的文本排版等各种图像处理需求。