webman:生成图形验证码(v1.5.7)
一,官方文档地址:
1,文档
https://www.workerman.net/doc/webman/components/captcha.html
2,用到的库项目代码地址:
https://github.com/webman-php/captcha
二, 安装库:
1,用composer安装
liuhongdi@lhdpc:/data/webman/imageadmin$ composer require webman/captcha
2,查看所安装库的版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show webman/captcha
name : webman/captcha
descrip. : Captcha generator
keywords : bot, captcha, spam
versions : * v1.0.2
…
三,php代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?php namespace app\controller; use support\Request; use app\result\Result; use support\Log; use think\facade\Db; use think\facade\Cache; use Webman\Captcha\CaptchaBuilder; use Webman\Captcha\PhraseBuilder; use app\model\Comment as CommentModel; class ImageController { /** * 输出验证码图像 */ public function captcha(Request $request ){ // 验证码长度 $length = 4; // 包含哪些字符 $chars = '0123456789abcefghijklmnopqrstuvwxyz' ; $builder = new PhraseBuilder( $length , $chars ); $captcha = new CaptchaBuilder(null, $builder ); // 生成验证码 $captcha ->build( $width = 200, $height = 80, $font = null); // 将验证码的值存储到session中 $request ->session()->set( 'captcha' , strtolower ( $captcha ->getPhrase())); /* // base64 image $image = $captcha->inline(); //json return json(['code' => 0, 'image'=>$image,'uniqid'=>'123']); */ // 获得验证码图片二进制数据 $img_content = $captcha ->get(); return response( $img_content , 200, [ 'Content-Type' => 'image/jpeg' ]); } /** * 检查验证码 */ public function checkCaptcha(Request $request ) { // 获取post请求中的captcha字段 $captcha = $request ->post( 'captcha' ); // 对比session中的captcha值 if ( strtolower ( $captcha ) !== $request ->session()->get( 'captcha' )) { return json([ 'code' => 400, 'msg' => '输入的验证码不正确' ]); } return json([ 'code' => 0, 'msg' => 'ok' ]); } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/09/19/webman-sheng-cheng-tu-xing-yan-zheng-ma/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,查看效果:
五,查看webman的版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...