Docker PHP中安装gd扩展并生成图形验证码

在容器中执行:

1
2
3
4
5
apt install libjpeg62-turbo-dev libfreetype6-dev -y
 
docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg
 
docker-php-ext-install gd

可运行:php --ri gd 查看安装结果,重启docker容器。

图形验证码示例代码:

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
58
59
60
61
62
63
64
<?php
session_start();
function random($len) {
    $srcstr = "123456789ABCDEFGHJKLNPQRSTUVXYZ";
    $strs = "";
    for ($i = 0; $i < $len; $i++) {
        $strs .= $srcstr[mt_rand(0,30)];
    }
    return $strs;
}
  
//随机生成的字符串
$str = random(5);
  
//验证码图片的宽度
$width  = 80;     
  
//验证码图片的高度
$height = 40;    
  
//声明需要创建的图层的图片格式
@ header("Content-Type:image/png");
  
//创建一个图层
$im = imagecreate($width, $height);
  
//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
  
//模糊点颜色
$pix  = imagecolorallocate($im, 100, 207, 95);
  
//字体色
$color = imagecolorallocate($im, 100, 207, 2);
 
//边框颜色
$col = imagecolorallocate($im, 100, 164, 26);
 
//字体
putenv('GDFONTPATH=' . realpath('.'));//解决linux下GD库版本低于2.0.18不能显示的问题
$font = 'arial.ttf';
  
//绘模糊作用的点
mt_srand();
for ($i = 0; $i < 800; $i++) {
    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
  
//输出字符
imagettftext($im, 16, 0, 5, 28, $color, $font, $str);
 
//输出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $col);
  
//输出图片
imagepng($im);
  
imagedestroy($im);
  
$str = md5(strtolower($str));
  
//选择 Session
$_SESSION["verification"] = $str;
?>

  

posted @   Don  阅读(294)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示