Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
feiin committed Jun 18, 2022
1 parent cdd68e0 commit e768768
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
108 changes: 106 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,115 @@
# go-captcha
golang 验证码
golang 实现普通验证码,同时支持中文汉字点击行为验证码

- 英文数字验证
- 中文点选验证
- 干扰线
- 干扰点
- 自定义背景颜色
- 自定义字体颜色
- 自定义倾斜角度
- 自定义字体大小
- 自定义字体

## Preview

![](./previews/test_0.png)

![](./previews/test_1.png)

![](./previews/test_2.png)
![](./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)
//...
```
2 changes: 1 addition & 1 deletion captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion captcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit e768768

Please sign in to comment.