在PHP开发中,GD库是常用的图像处理扩展,支持生成各类统计图表,无需依赖第三方绘图服务,适合服务端动态生成图表场景。使用前先确认环境已开启GD扩展,可通过phpinfo()查看是否存在GD相关配置项。

GD库基础使用流程
使用GD库绘图需要遵循固定的步骤,按顺序执行即可完成基础图形输出:
- 创建画布:指定图表的宽高,生成空白图像资源
- 分配颜色:定义背景色、边框色、数据色等所需颜色
- 绘制元素:根据需求画矩形、线条、文字等图表组件
- 输出图像:指定输出格式,将图像发送到浏览器或保存为文件
- 释放资源:销毁图像资源,避免内存占用
绘制简单柱状图示例
以下代码实现了一个简单的单数据柱状图,支持自定义数据、颜色、尺寸:
<?php
// 检查GD库是否开启
if (!extension_loaded('gd')) {
die('GD库未开启,请先配置PHP环境');
}
// 图表基础配置
$width = 600; // 画布宽度
$height = 400; // 画布高度
$data = [120, 180, 90, 210, 150]; // 统计数据
$labels = ['一月', '二月', '三月', '四月', '五月']; // 对应标签
// 创建画布
$image = imagecreatetruecolor($width, $height);
// 分配颜色
$bgColor = imagecolorallocate($image, 240, 240, 240); // 背景色浅灰
$barColor = imagecolorallocate($image, 66, 133, 244); // 柱状图颜色蓝色
$textColor = imagecolorallocate($image, 51, 51, 51); // 文字颜色深灰
$borderColor = imagecolorallocate($image, 200, 200, 200); // 边框色
// 填充背景
imagefill($image, 0, 0, $bgColor);
// 绘制边框
imagerectangle($image, 0, 0, $width-1, $height-1, $borderColor);
// 柱状图参数计算
$barWidth = 50; // 单个柱子宽度
$barGap = ($width - 20 - count($data) * $barWidth) / (count($data) + 1); // 柱子间距
$maxValue = max($data);
$chartHeight = $height - 60; // 图表区域高度,预留底部文字空间
$baseY = $height - 30; // 柱子底部Y坐标
// 绘制柱状图
foreach ($data as $index => $value) {
$x1 = 10 + $barGap + $index * ($barWidth + $barGap);
$x2 = $x1 + $barWidth;
$barHeight = ($value / $maxValue) * $chartHeight;
$y1 = $baseY - $barHeight;
$y2 = $baseY;
// 画柱子
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $barColor);
// 画柱子顶部数值
imagestring($image, 5, $x1 + 15, $y1 - 20, $value, $textColor);
// 画底部标签
imagestring($image, 5, $x1 + 10, $baseY + 5, $labels[$index], $textColor);
}
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);
// 释放资源
imagedestroy($image);
?>绘制折线图示例
折线图适合展示数据变化趋势,核心逻辑是计算数据点坐标后连接线条,同时可添加数据点标记:
<?php
$width = 600;
$height = 400;
$data = [80, 120, 90, 150, 200, 180];
$labels = ['周一', '周二', '周三', '周四', '周五', '周六'];
$image = imagecreatetruecolor($width, $height);
// 颜色分配
$bgColor = imagecolorallocate($image, 255, 255, 255);
$lineColor = imagecolorallocate($image, 219, 68, 55);
$pointColor = imagecolorallocate($image, 219, 68, 55);
$textColor = imagecolorallocate($image, 0, 0, 0);
$gridColor = imagecolorallocate($image, 230, 230, 230);
imagefill($image, 0, 0, $bgColor);
// 绘制网格线
for ($i = 0; $i <= 5; $i++) {
$y = 30 + $i * ($height - 60) / 5;
imageline($image, 30, $y, $width - 30, $y, $gridColor);
// 网格线对应数值
$val = (5 - $i) * (max($data) / 5);
imagestring($image, 3, 5, $y - 8, round($val), $textColor);
}
// 计算数据点坐标
$pointGap = ($width - 60) / (count($data) - 1);
$points = [];
foreach ($data as $index => $value) {
$x = 30 + $index * $pointGap;
$y = 30 + (1 - $value / max($data)) * ($height - 60);
$points[] = [$x, $y];
// 画数据点
imagefilledellipse($image, $x, $y, 8, 8, $pointColor);
// 画底部标签
imagestring($image, 3, $x - 10, $height - 20, $labels[$index], $textColor);
}
// 连接数据点成折线
for ($i = 0; $i < count($points) - 1; $i++) {
imageline($image, $points[$i][0], $points[$i][1], $points[$i+1][0], $points[$i+1][1], $lineColor);
}
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>注意事项
使用GD库绘图时需要注意几个常见问题:
- 输出图像前不要输出任何HTML内容或空格,否则会导致图像损坏
- 如果需要保存图像到文件,将
imagepng($image)改为imagepng($image, 'chart.png')即可,不同格式对应函数如imagejpeg、imagegif - 中文文字需要使用
imagettftext函数配合字体文件渲染,避免中文乱码,示例:imagettftext($image, 12, 0, $x, $y, $textColor, 'simhei.ttf', '中文内容') - 复杂图表建议提前计算好所有坐标参数,避免绘制时出现位置偏移