依赖
"github.com/mojocn/base64Captcha"
实现
import (
cp "github.com/mojocn/base64Captcha"
"image/color"
)
type captcha struct {
c *cp.Captcha
}
//创建字符串验证码实例
func NewCaptcha(height, width, length int, source string) *captcha {
driver := cp.NewDriverString(height, width, 0, cp.OptionShowHollowLine,
length, source, &color.RGBA{0, 0, 0, 0}, []string{"Flim-Flam.ttf"})
cape := cp.NewCaptcha(driver, cp.DefaultMemStore)
return &captcha{cape}
}
//验证是否有效
func (r *captcha) Verify(id, answer string) bool {
get := cp.DefaultMemStore.Get(id, false)
if get == "" {
return false
}
return r.c.Verify(id, answer, true)
}
//生成base64
func (r *captcha) Generate() (id, b64s string, err error) {
return r.c.Generate()
}