增加验证码相关函数和库 并编写测试程序

This commit is contained in:
wanglei 2025-11-20 23:05:33 +08:00
parent 614dfdfbf0
commit 836d908f4c
4 changed files with 122 additions and 61 deletions

View File

@ -13,7 +13,7 @@ http {
client_max_body_size 1024M; #允许最大100k的请求体
client_body_buffer_size 1024M; #设置缓冲区大小
lua_code_cache on; #代码缓存
#lua_code_cache on; #代码缓存
lua_package_path '$prefix/src/?/?.lua;$prefix/src/?.lua;/home/frankly/work/AuthPlatform/src/?/?.lua;/home/frankly/work/AuthPlatform/src/?.lua;;';
lua_package_cpath '$prefix/src/share/lib/?.so;/home/frankly/work/AuthPlatform/src/share/lib/?.so;;';

BIN
src/share/lib/gd.so Executable file

Binary file not shown.

View File

@ -31,6 +31,73 @@ local status = val or STATUS_CODE.UNKNOWN_ERROR
local msg = status.message
ngx.say("message:"..msg)
local ok, gd = pcall(require, "gd")
if not ok then
ngx.say("加载失败"..gd)
return
end
function enabled(res, desc)
local str = " " .. desc .. " "
str = str .. string.rep(".", 37 - string.len(str))
if res then
print(str .. " Enabled")
else
print(str .. " Disabled")
end
end
print("Lua-GD version: " .. gd.VERSION)
print("Lua-GD features:")
--[[
local gd = require("gd")
local iSeed = os.time() + os.clock() * 10000
math.randomseed(iSeed)
function CheckCode(nChars, iWidth, iHeight)
local sTab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
local iTabLen = string.len(sTab)
local idx, x, y, clr
local im = gd.create(iWidth, iHeight)
im:colorAllocate(255, 255, 255) -- background color
for i=1, nChars do
clr = im:colorAllocate(math.random(0, 100), math.random(0, 150), math.random(0, 200))
idx = math.random(1, iTabLen)
x = math.random(1, 8) + iWidth * (i-1) / 4
y = math.random(1, iHeight / 4)
im:string(gd.FONT_LARGE, x, y, string.sub(sTab, idx, idx), clr)
end
for i=1, 3 do
clr = im:colorAllocate(math.random(0, 255), math.random(0, 255), math.random(0, 255))
im:line(math.random(1, iWidth), math.random(1, iHeight), math.random(1, iWidth), math.random(1, iHeight), clr)
end
return im
end
local im = CheckCode(4, 60, 20)
print("Content-type: image/png")
print("Pragma: no-cache")
print("Expires: Thu Jan 01 00:00:00 UTC 1970")
print("")
io.write(im:pngStr())
--]]
--[[
local genpic = require("util.generatechaptcha")
local filename, fp = genpic.getChaptcha()
--redis中添加picgid为key,string为value的记录
--setRedis(filename, stringmark)
--response header中传参picgid
ngx.header.content_type = "text/plain"
ngx.header.picgid = filename
--页面返回pic
ngx.say(fp)
--nginx退出
ngx.exit(200)
--]]
do return end
--local openssl = require("openssl")
--

View File

@ -4,69 +4,63 @@
--- DateTime: 2025/11/19 20:18
--- 生成校验码图片
--设置随机种子
local resty_uuid = require("resty.uuid")
math.randomseed(tonumber(resty_uuid.gennum20()))
--在32个备选字符中随机筛选4个作为captcha字符串
local dict={'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'}
local stringmark = ""
for i = 1, 4 do
stringmark = stringmark..dict[math.random(1,32)]
end
--图片基本info
--picgid
local filename = "1"..resty_uuid.gen20()..".png"
--图片78x26
local xsize = 78
local ysize = 26
--字体大小
local wsize = 17.5
--干扰线(yes/no)
local line = "yes"
--加载模块
local gd = require('gd')
--创建面板
local im = gd.createTrueColor(xsize, ysize)
--定义颜色
local black = im:colorAllocate(0, 0, 0)
local grey = im:colorAllocate(202, 202, 202)
local color = {}
for c = 1, 100 do
color[c] = im:colorAllocate(math.random(100), math.random(100), math.random(100))
end
--画背景
x, y = im:sizeXY()
im:filledRectangle(0, 0, x, y, grey)
--画字符
gd.useFontConfig(true)
for i = 1, 4 do
k = (i - 1) * 16 + 3
im:stringFT(color[math.random(100)], "Arial:bold", wsize, math.rad(math.random(-10,10)), k, 22, string.sub(stringmark, i, i))
end
--干扰线点
if line == "yes" then
for j = 1, math.random(3) do
im:line(math.random(xsize), math.random(ysize), math.random(xsize), math.random(ysize), color[math.random(100)])
local uuid = require("util.uuid")
local _M = {}
--在32个备选字符中随机筛选4个作为captcha字符串
local dict = {'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'}
function _M.getChaptcha()
local stringmark = ""
for i = 1, 4 do
stringmark = stringmark..dict[math.random(1,32)]
end
for p = 1, 20 do
im:setPixel(math.random(xsize), math.random(ysize), color[math.random(100)])
--图片基本info
local uid = uuid.generateUuid()
--picgid
local filename = "1"..uuid..".png"
--图片78x26
local xsize = 78
local ysize = 26
--字体大小
local wsize = 17.5
--干扰线(yes/no)
local line = "yes"
--创建面板
local im = gd.createTrueColor(xsize, ysize)
--定义颜色
local black = im:colorAllocate(0, 0, 0)
local grey = im:colorAllocate(202, 202, 202)
local color = {}
for c = 1, 100 do
color[c] = im:colorAllocate(math.random(100), math.random(100), math.random(100))
end
--画背景
x, y = im:sizeXY()
im:filledRectangle(0, 0, x, y, grey)
--画字符
gd.useFontConfig(true)
for i = 1, 4 do
k = (i - 1) * 16 + 3
im:stringFT(color[math.random(100)], "Arial:bold", wsize, math.rad(math.random(-10,10)), k, 22, string.sub(stringmark, i, i))
end
--干扰线点
if line == "yes" then
for j = 1, math.random(3) do
im:line(math.random(xsize), math.random(ysize), math.random(xsize), math.random(ysize), color[math.random(100)])
end
for p = 1, 20 do
im:setPixel(math.random(xsize), math.random(ysize), color[math.random(100)])
end
end
--流输出
local fp = im:pngStr(75)
return filename, fp
end
--流输出
local fp = im:pngStr(75)
--redis中添加picgid为key,string为value的记录
--setRedis(filename, stringmark)
--response header中传参picgid
ngx.header.content_type = "text/plain"
ngx.header.picgid = filename
--页面返回pic
ngx.say(fp)
--nginx退出
ngx.exit(200)
return _M