|
|
@@ -6,6 +6,57 @@ use app\common\model\setting\PostageRegion;
|
|
|
use app\common\service\FileService;
|
|
|
use think\helper\Str;
|
|
|
|
|
|
+/**
|
|
|
+ * 生成图形验证码
|
|
|
+ */
|
|
|
+function generateCaptcha() {
|
|
|
+ // 配置参数
|
|
|
+ $width = 120; // 图像宽度
|
|
|
+ $height = 40; // 图像高度
|
|
|
+ $characters = 4; // 验证码字符数量
|
|
|
+ $font_size = 20; // 字体大小
|
|
|
+
|
|
|
+ // 允许的字符(去除易混淆的字符如0、O、1、l等)
|
|
|
+ $allowed_chars = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ';
|
|
|
+
|
|
|
+ // 生成随机验证码
|
|
|
+ $code = '';
|
|
|
+ for ($i = 0; $i < $characters; $i++) {
|
|
|
+ $code .= $allowed_chars[rand(0, strlen($allowed_chars) - 1)];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建图像资源
|
|
|
+ $image = imagecreatetruecolor($width, $height);
|
|
|
+
|
|
|
+ // 设置背景色为白色
|
|
|
+ $bg_color = imagecolorallocate($image, 255, 255, 255);
|
|
|
+ imagefill($image, 0, 0, $bg_color);
|
|
|
+
|
|
|
+ // 添加干扰线
|
|
|
+ for ($i = 0; $i < 5; $i++) {
|
|
|
+ $line_color = imagecolorallocate($image, rand(100, 200), rand(100, 200), rand(100, 200));
|
|
|
+ imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加噪点
|
|
|
+ for ($i = 0; $i < 100; $i++) {
|
|
|
+ $pixel_color = imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
|
|
|
+ imagesetpixel($image, rand(0, $width), rand(0, $height), $pixel_color);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 绘制验证码字符
|
|
|
+ for ($i = 0; $i < $characters; $i++) {
|
|
|
+ $text_color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
|
|
|
+ $x = 15 + ($i * ($width - 30) / $characters);
|
|
|
+ $y = rand($font_size + 5, $height - 5);
|
|
|
+ $angle = rand(-20, 20); // 随机旋转角度
|
|
|
+ imagettftext($image, $font_size, $angle, (int)$x, (int)$y, $text_color, './static/fonts/arial.ttf', $code[$i]);
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ 'captcha' => $code,
|
|
|
+ 'image' => $image,
|
|
|
+ ];
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* 根据经纬度获取详细地址(高德地图逆地理编码)
|