
本文介绍使用 PHP GD 库对文本进行水平/垂直拉伸的实用方法,重点解决验证码中常见文字变形需求,通过 imageaffine()(PHP 7.2+)或手动逐像素映射实现可控拉伸,并结合抗锯齿、倾斜与随机干扰提升安全性。
本文介绍使用 php gd 库对文本进行水平/垂直拉伸的实用方法,重点解决验证码中常见文字变形需求,通过 `imageaffine()`(php 7.2+)或手动逐像素映射实现可控拉伸,并结合抗锯齿、倾斜与随机干扰提升安全性。
在 PHP 中,GD 扩展本身不提供直接的“文字拉伸”参数(如 CSS 的 transform: scaleX(1.5)),但可通过两种主流方式模拟拉伸效果:一是利用 imageaffine() 对已渲染文字区域做仿射变换(推荐,简洁高效);二是先将单个字符渲染到独立小图,再用 imagecopyresampled() 按比例缩放后合成——后者兼容性更广,适用于旧版 PHP。
✅ 推荐方案:imageaffine() 实现精准拉伸(PHP ≥ 7.2)
GD 的 imageaffine() 函数支持对任意图像区域应用仿射变换矩阵,可轻松实现水平/垂直缩放、旋转、倾斜组合。以下为关键代码片段(集成至原 CAPTCHA 脚本):
// 渲染单个字符到临时图像(带透明背景)
$char_img = imagecreatetruecolor($xw, $height);
imagealphablending($char_img, false);
imagesavealpha($char_img, true);
$transparent = imagecolorallocatealpha($char_img, 0, 0, 0, 127);
imagefilledrectangle($char_img, 0, 0, $xw, $height, $transparent);
// 在临时图上绘制字符(居中、抗锯齿)
$y_offset = ($height + $font_size) / 2; // 垂直居中基线
imagettftext($char_img, $font_size, $angle, $xw/2 - $font_size/3, $y_offset, $font_color, $font, $letter);
// 定义拉伸矩阵:[scale_x, 0, 0, scale_y, 0, 0] → 水平拉伸1.8倍,垂直保持1.0
$scale_x = 1.8;
$scale_y = 1.0;
$matrix = [
$scale_x, 0, 0,
0, $scale_y, 0
];
// 对字符图像执行仿射拉伸
$stretched = imageaffine($char_img, $matrix);
if ($stretched) {
// 将拉伸后的图像合成到主画布(位置校准)
imagecopy($image, $stretched, $x, 5, 0, 0, imagesx($stretched), imagesy($stretched));
}
imagedestroy($char_img);
imagedestroy($stretched);⚠️ 注意事项:
Build MCP servers and clients for PHP applications with the official PHP SDK下载使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。
imageaffine()要求 GD 启用--with-webp或--with-png支持,且 PHP ≥ 7.2;- 拉伸后字符边缘可能模糊,建议配合
imageantialias($image, true)开启抗锯齿;- 若需同时倾斜+拉伸,可合并变换矩阵(如
[sx·cosθ, sx·sinθ, -sy·sinθ, sy·cosθ, tx, ty])。
? 兼容方案:imagecopyresampled() 分步缩放(全版本支持)
对于 PHP
立即学习“PHP免费学习笔记(深入)”;
// 步骤1:创建高精度字符图(2x尺寸防缩放失真) $src_w = (int)($xw * 2); $src_h = (int)($height * 2); $src_img = imagecreatetruecolor($src_w, $src_h); imagealphablending($src_img, false); imagesavealpha($src_img, true); $transparent = imagecolorallocatealpha($src_img, 0, 0, 0, 127); imagefilledrectangle($src_img, 0, 0, $src_w, $src_h, $transparent); // 步骤2:居中绘制(坐标按2x缩放) $large_font_size = $font_size * 2; $large_y = ($src_h + $large_font_size) / 2; imagettftext($src_img, $large_font_size, $angle, $src_w/2 - $large_font_size/3, $large_y, $font_color, $font, $letter); // 步骤3:按目标尺寸重采样(水平拉伸:dst_w = src_w * 1.8, dst_h = src_h) $dst_w = (int)($src_w * 1.8); $dst_h = $src_h; $dst_img = imagecreatetruecolor($dst_w, $dst_h); imagealphablending($dst_img, false); imagesavealpha($dst_img, true); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h); // 步骤4:合成到主图(y偏移微调以对齐基线) imagecopy($image, $dst_img, $x, 5, 0, 0, $dst_w, $dst_h); imagedestroy($src_img); imagedestroy($dst_img);
?️ 安全增强建议(CAPTCHA 场景)
-
动态干扰:在拉伸后叠加轻微噪声点(
imagesetpixel()随机绘制)或低透明度斜线; -
字符间距扰动:避免固定
$xw,改为rand($xw*0.8, $xw*1.2)增加识别难度; -
字体轮换:从数组中随机选取不同 TTF 字体(如
arial.ttf,tahoma.ttf),破坏纹理规律; -
Session 校验强化:生成 token 时加入时间戳哈希(
hash_hmac('sha256', $digit, $_SERVER['REQUEST_TIME'])),防止重放。
通过上述方法,你不仅能复现示例中右图的拉伸效果,还能灵活控制形变强度、方向与组合策略,显著提升验证码的机器识别门槛,同时保持人类可读性。



















