base64Captcha 使用

951 阅读1分钟

使用

base64Captcha的使用非常简单只需要几行代码就可配置使用

依赖引入

go get -u github.com/mojocn/base64Captcha

简单用法:

创建一个生成器

// 创建一个全局缓存
var store = base64Captcha.DefaultMemStore
​
func captcha(){
    driverString := base64Captcha.NewDriverString(60, 100, 200, 2, 6,"123456abcdefghijklmnopqrstuvwxyz", nil, nil, nil)
    // id 为缓存id,str为生成的base64码
    // 除DriverString 还有DriverMath,DriverChinese,DriverAudio,DriverDigit等实现
    id,str,err :=base64Captcha.NewCaptcha(driverString, store).Generate()
    if err!=nil{
        ...
    }
    return id,str
}

结构体注释

DriverString{
    Height:          60, // 高
    Width:           100, // 宽
    NoiseCount:      200, // 背景噪点数量
    ShowLineOptions: base64Captcha.OptionShowHollowLine, // 干扰线数量 默认为2,4,8
    Length:          6, // 验证码长度
    Source:          "1234567890qwertyuioplkjhgfdsazxcvbnm", // 验证码随机文本
    BgColor:         nil, // 背景色
    Fonts:           nil, // 字体
}
DriverMath{
        Height:          60,// 同上
        Width:           100,// 同上
        NoiseCount:      200,// 同上
        ShowLineOptions: base64Captcha.OptionShowHollowLine,// 同上
        BgColor:         nil,// 同上
        Fonts:           nil,// 同上
}
...

Driver接口重写

此外该工具包还提供了Driver接口,可通过重写其方法来自定义自己的验证码

github地址

github地址mojocn/base64Captcha: captcha of base64 image string (github.com)