修改验证码图片生成问题

This commit is contained in:
wanglei 2025-11-21 14:46:26 +08:00
parent 4869731a99
commit 80a4e9d478

View File

@ -18,9 +18,9 @@ local function generate_captcha()
local uid = uuid.generateUuid()
--picgid
local filename = uid
--图片78x26
local width = 78
local height = 26
--图片120x40
local width = 120
local height = 40
-- 创建画布
local im = gd.createTrueColor(width, height)
@ -41,20 +41,20 @@ local function generate_captcha()
gd.useFontConfig(true)
for i = 1, 4 do
local char = string.sub(captcha_text, i, i)
local x = 10 + (i-1)*25 + math.random(-5,5)
local x = 10 + (i - 1) * 25 + math.random(-5,5)
local y = 10 + math.random(-5,5)
local angle = math.random(-20, 20)
im:stringFT(black, "Arial:bold", 18, angle, x, 30, char)
im:stringFT(black, "Arial:bold", 18, angle, x, y, char)
end
-- 添加干扰线
for i = 1, 5 do
local line_color = im:colorAllocate(math.random(0,255), math.random(0,255), math.random(0,255))
im:line(math.random(0, height), math.random(0, width), math.random(0,height), math.random(0, width), line_color)
im:line(math.random(0, width), math.random(0, height), math.random(0,width), math.random(0, height), line_color)
end
-- 添加噪点
for i = 1, 100 do
local dot_color = im:colorAllocate(math.random(0,255), math.random(0,255), math.random(0,255))
im:setPixel(math.random(0, height), math.random(0, width), dot_color)
im:setPixel(math.random(0, width), math.random(0, height), dot_color)
end
-- 将图片转换为PNG格式的二进制数据
local png_data = im:pngStr(75)