diff --git a/README.md b/README.md index cdcb466..ed93d36 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,15 @@ # go-captcha -golang 验证码 +golang 实现普通验证码,同时支持中文汉字点击行为验证码 +- 英文数字验证 +- 中文点选验证 +- 干扰线 +- 干扰点 +- 自定义背景颜色 +- 自定义字体颜色 +- 自定义倾斜角度 +- 自定义字体大小 +- 自定义字体 ## Preview @@ -8,4 +17,99 @@ golang 验证码 ![](./previews/test_1.png) -![](./previews/test_2.png) \ No newline at end of file +![](./previews/test_2.png) + +## Quick Start 使用 + +*普通验证码* +``` +import ( + "github.com/feiin/go-captcha" +) + +cpt := captcha.NewCaptcha(260, 100) + +//cpt.SetFontColors(...) +//cpt.SetFont(...) +//cpt.SetFontSize(...) +//cpt.SetSytle(...) +//cpt.SetBackgroundColor(...) + + +//获取4位随机验证码 +result,err := cpt.GenRandomNormalCaptcha(4) + +//result 属性 +// - DrawRects []DrawRect 随机验证码位置 +// - Text string 随机验证码字符 +// - ImageBase64 string 随机验证码base64编码 + +``` + +*中文点击行为验证码* + +> 验证码输出展示 -> 上传用户行为 -> 验证点击行为数据 + +``` + +import ( + "github.com/feiin/go-captcha" +) + +//........................................ + + + +cpt := captcha.NewCaptcha(260, 100) +cpt.SetBackgroundColor(color.RGBA{R: uint8(20), G: uint8(8), B: uint8(100), A: uint8(255)}) +cpt.Config.Style = captcha.CaptchaStyle_Behavior +cpt.Config.MaxRotate = 20 +result, err := cpt.GenBehaviorCNCaptcha() + +//可以将result或result.DrawRects信息者结果存入redis或程序内存中等,当上传用户点击行为时,做位置信息校验 + +//当用户点击验证后,上传点击[]Point位置信息,后端校验 +if captcha.ValidBehaviorCaptcha(result,points) { + //验证通过 +} else { + //验证未过 +} + +//rects = result.DrawRects +//if captcha.ValidBehaviorRects(rects,points) { + ////验证通过 +//} else { + ////验证未过 +//} + +``` + + +## Config属性 + +``` +type Config struct { + FontColors []color.Color + BackgroupColor color.Color //default white + FontSize int //default 56 + MaxRotate int //default 30 + Style CaptchaStyle + Font *truetype.Font //字体 +} +``` +- FontColors 字体颜色,多个时随机字体颜色 +- BackgroupColor 背景颜色 +- FontSize 字体大小 +- MaxRotate 最大旋转角度 +- Style CaptchaStyle_Normal普通验证码,CaptchaStyle_Behavior点击行为验证码 + + +Config设置方法 +``` + +cpt := NewCaptcha(260, 100) +cpt.Config.FontSize = 24 //等同cpt.SetFontSize(24) +//... + + +``` diff --git a/captcha.go b/captcha.go index 263792d..2b9cf31 100644 --- a/captcha.go +++ b/captcha.go @@ -147,7 +147,7 @@ func (cp *Captcha) GenRandomNormalCaptcha(length int) (CaptchaResult, error) { } //GenBehaviorCaptcha 生成中文点击验证码 - 点击行为验证码 -func (cp *Captcha) GenBehaviorCaptcha() (CaptchaResult, error) { +func (cp *Captcha) GenBehaviorCNCaptcha() (CaptchaResult, error) { ix := rand.Intn(len(CNChars)) return cp.GenCaptchaImage(CNChars[ix]) diff --git a/captcha_test.go b/captcha_test.go index 279bbe2..c9641e1 100644 --- a/captcha_test.go +++ b/captcha_test.go @@ -29,7 +29,7 @@ func TestGenGenBehaviorCaptcha(t *testing.T) { cpt.SetBackgroundColor(color.RGBA{R: uint8(20), G: uint8(8), B: uint8(100), A: uint8(255)}) // cpt.Config.Style = CaptchaStyle_Behavior cpt.Config.MaxRotate = 20 - result, err := cpt.GenBehaviorCaptcha() + result, err := cpt.GenBehaviorCNCaptcha() t.Logf("GenCaptchaImage: %+v %+v", result, err) }