好的,我来给你整理一份 PHP GD 库使用技巧与实用图像处理案例,从基础概念、常用函数,到实际案例,方便直接上手。内容尽量条理化,适合参考与快速开发。
PHP GD 库使用技巧与图像处理案例
1️⃣ GD 库简介
GD 库是 PHP 内置的图像处理扩展,用于生成、修改或输出图片。支持多种图像格式,如 JPEG、PNG、GIF、WebP 等。
常用场景:
- 生成验证码
- 缩放或裁剪图片
- 添加文字水印
- 动态图表或海报生成
注意:使用前需确保 GD 库已安装,可通过
phpinfo()查看。
<?php
if (function_exists('gd_info')) {
echo "GD 已安装";
} else {
echo "GD 未安装";
}
?>
2️⃣ 常用 GD 函数分类
创建/读取图片
// 创建空白画布
$image = imagecreatetruecolor(200, 100);
// 从文件读取
$image = imagecreatefromjpeg('test.jpg');
$image = imagecreatefrompng('test.png');
$image = imagecreatefromgif('test.gif');
颜色相关
// 分配颜色
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 255, 0, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
绘图
// 绘制矩形
imagerectangle($image, 10, 10, 190, 90, $red);
// 绘制线条
imageline($image, 0, 0, 200, 100, $blue);
// 绘制圆
imageellipse($image, 100, 50, 80, 80, $white);
文字操作
// 系统字体
imagestring($image, 5, 50, 30, 'Hello GD', $white);
// TTF 字体
$font = __DIR__.'/msyh.ttf'; // 字体文件
imagettftext($image, 20, 0, 50, 50, $red, $font, 'GD 字体文字');
图像输出与保存
header('Content-Type: image/png');
imagepng($image); // 输出到浏览器
imagejpeg($image, 'out.jpg'); // 保存到文件
imagedestroy($image); // 销毁资源
3️⃣ 实用技巧
- 保持透明背景
imagealphablending($image, false);
imagesavealpha($image, true);
- 缩放图片
$new = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new, $original, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
- 裁剪图片
$crop = imagecrop($original, ['x'=>50,'y'=>50,'width'=>100,'height'=>100]);
- 添加水印
$stamp = imagecreatefrompng('watermark.png');
imagecopy($original, $stamp, 10, 10, 0, 0, imagesx($stamp), imagesy($stamp));
- 动态验证码生成
$image = imagecreatetruecolor(120, 40);
$bg = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg);
$code = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ23456789'),0,5);
for($i=0;$i<5;$i++){
$color = imagecolorallocate($image, rand(0,150), rand(0,150), rand(0,150));
imagestring($image, 5, 20*$i+5, 10, $code[$i], $color);
}
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
4️⃣ 图像处理案例
案例 1:生成缩略图
function createThumbnail($src, $dest, $width, $height){
$info = getimagesize($src);
switch($info[2]){
case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($src); break;
case IMAGETYPE_PNG: $img = imagecreatefrompng($src); break;
case IMAGETYPE_GIF: $img = imagecreatefromgif($src); break;
default: return false;
}
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $img, 0,0,0,0, $width, $height, $info[0], $info[1]);
imagejpeg($thumb, $dest);
imagedestroy($img);
imagedestroy($thumb);
}
createThumbnail('big.jpg', 'thumb.jpg', 150, 150);
案例 2:给图片添加文字水印
$image = imagecreatefromjpeg('photo.jpg');
$color = imagecolorallocatealpha($image, 255, 255, 255, 50); // 半透明
$font = __DIR__.'/msyh.ttf';
imagettftext($image, 20, 0, 10, 30, $color, $font, '©版权');
imagejpeg($image, 'watermarked.jpg');
imagedestroy($image);
案例 3:动态拼接海报
$bg = imagecreatefromjpeg('bg.jpg');
$avatar = imagecreatefromjpeg('user.jpg');
$avatar_new = imagecreatetruecolor(100,100);
imagecopyresampled($avatar_new, $avatar, 0,0,0,0,100,100,imagesx($avatar), imagesy($avatar));
imagecopy($bg, $avatar_new, 50, 50, 0,0, 100,100);
header('Content-Type: image/jpeg');
imagejpeg($bg);
imagedestroy($bg);
imagedestroy($avatar_new);
imagedestroy($avatar);
5️⃣ 总结与技巧
- 优先使用
imagecreatetruecolor保证 24 位色彩和抗锯齿。 - 使用
imagecopyresampled代替imagecopyresized,保证缩放质量。 - 注意内存管理:GD 会占用大量内存,处理大图时需要
ini_set('memory_limit', '512M');。 - PNG 和 GIF 透明处理:务必使用
imagealphablending和imagesavealpha。
发表回复