text($captcha, $width / 2, $height / 2, function ($font) { $font->file(public_path('COOPBL.TTF')); $font->size(40); // GD 内置字体只支持 1-5 $font->color('#000000'); $font->align('center'); $font->valign('middle'); }); // 添加干扰点(杂色) for ($i = 0; $i < 150; $i++) { $x = rand(0, $width); $y = rand(0, $height); $color = '#' . str_pad(dechex(rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); $image->pixel($color, $x, $y); } for ($i = 0; $i < 3; $i++) { $image->line( rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), function ($draw) { $draw->color('#' . str_pad(dechex(rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT)); // 移除了设置宽度的部分 } ); } // 转为 Base64 $base64 = 'data:image/png;base64,' . base64_encode($image->encode('png')); $id=DB::table('yanzhengma_image')->insertGetId([ 'code' => $captcha, 'end_time' => date('Y-m-d H:i:s', strtotime('+5 minutes')), 'created_at' => date('Y-m-d H:i:s'), ]); if($id){ return \Yz::Return(true,'获取成功',[ 'image' => $base64, 'code_id' => $id, // 注意:生产环境不要返回明文验证码!仅用于调试 //'debug_captcha' => $captcha, ]); }else{ return \Yz::echoError1('验证码获取失败'); } } public function CheckCode(){ $code_id = request('code_id'); $code = request('code'); $check=DB::table('yanzhengma_image') ->where('id',$code_id) ->where('code',$code) ->where('end_time','>',date('Y-m-d H:i:s')) ->where('status','1') ->update([ 'status'=>'2' ]); if($check){ return \Yz::Return(true,'验证成功'); }else{ return \Yz::echoError1('验证码无效'); } } }