-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.go
213 lines (162 loc) · 4.49 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package captcha
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/golang/freetype/truetype"
"image"
"image/color"
"image/png"
"math/rand"
"os"
)
type CaptchaStyle int32
const (
CaptchaStyle_Normal CaptchaStyle = 0
CaptchaStyle_Behavior CaptchaStyle = 1
)
type Config struct {
FontColors []color.Color
BackgroupColor color.Color //default white
FontSize int //default 56
MaxRotate int //default 30
Style CaptchaStyle
Font *truetype.Font //字体
}
type Captcha struct {
Width int
Height int
Config Config
}
type CaptchaResult struct {
DrawRects []DrawRect `json:"draw_rect"`
Text string `json:"text"`
ImageBase64 string `json:"image_base64"`
}
func NewCaptcha(width, height int) *Captcha {
cp := Captcha{Width: width, Height: height}
config := Config{}
config.FontColors = []color.Color{
color.RGBA{R: uint8(66), G: uint8(153), B: uint8(244), A: uint8(255)},
color.RGBA{R: uint8(234), G: uint8(67), B: uint8(53), A: uint8(255)},
color.RGBA{R: uint8(251), G: uint8(188), B: uint8(5), A: uint8(255)},
color.RGBA{R: uint8(52), G: uint8(168), B: uint8(83), A: uint8(255)},
}
config.BackgroupColor = color.White
config.FontSize = 56
config.MaxRotate = 30
cp.Config = config
return &cp
}
//SetFontColors 设置字体颜色
func (cp *Captcha) SetFontColors(colors ...color.Color) {
if len(colors) == 0 {
return
}
cp.Config.FontColors = cp.Config.FontColors[:0]
for _, cr := range colors {
cp.Config.FontColors = append(cp.Config.FontColors, cr)
}
}
//SetFont 设置字体颜色
func (cp *Captcha) SetFont(font *truetype.Font) {
if font == nil {
return
}
cp.Config.Font = font
}
//SetFontSize 字体大小
func (cp *Captcha) SetFontSize(fontSize int) {
cp.Config.FontSize = fontSize
}
//SetFontSize 字体大小
func (cp *Captcha) SetSytle(style CaptchaStyle) {
cp.Config.Style = style
}
//SetBackgroundColor 设置背景颜色
func (cp *Captcha) SetBackgroundColor(color color.Color) {
cp.Config.BackgroupColor = color
}
//GenCaptchaImage 生成文字的验证码图片
func (cp *Captcha) GenCaptchaImage(text string) (CaptchaResult, error) {
result := CaptchaResult{}
c := Canvas{
Width: cp.Width,
Height: cp.Height,
NRGBA: image.NewNRGBA(image.Rect(0, 0, cp.Width, cp.Height)),
Config: cp.Config,
}
// text := "3567"
c.DrawBackgroud()
c.DrawLines(5)
drawRects := c.DrawString(text)
c.DrawCircles(120)
// c.DrawBezierLines(5)
imageBytes, err := encodingWithPng(c)
if err != nil {
return result, err
}
result.DrawRects = drawRects
result.ImageBase64 = fmt.Sprintf("data:%s;base64,%s", "image/png", base64.StdEncoding.EncodeToString(imageBytes))
result.Text = text
// writeImageFile("./previews/test.png", imageBytes)
return result, nil
}
//GenNormalRandomCaptcha 随机验证码 - 普通验证码
func (cp *Captcha) GenRandomNormalCaptcha(length int) (CaptchaResult, error) {
var buff bytes.Buffer
for i := 0; i < length; i++ {
ix := rand.Intn(length)
buff.WriteByte(Alphabets[ix])
}
return cp.GenCaptchaImage(buff.String())
}
//GenBehaviorCaptcha 生成中文点击验证码 - 点击行为验证码
func (cp *Captcha) GenBehaviorCNCaptcha() (CaptchaResult, error) {
ix := rand.Intn(len(CNChars))
return cp.GenCaptchaImage(CNChars[ix])
}
//ValidBehaviorCaptcha 验证行为点击验证码是否正确
func ValidBehaviorCaptcha(cr CaptchaResult, userPoints []Point) bool {
//字符数量不相等
return ValidBehaviorRects(cr.DrawRects, userPoints)
}
//ValidBehaviorRects 验证行为点是否在验证码矩形中
func ValidBehaviorRects(rects []DrawRect, userPoints []Point) bool {
if len(userPoints) != len(rects) {
return false
}
for i := 0; i < len(userPoints); i++ {
realRect := rects[i]
userPos := userPoints[i]
if !PointInRect(userPos, realRect) {
return false
}
}
return true
}
//PointInRect 验证点是否在矩形中
func PointInRect(p Point, rect DrawRect) bool {
if p.X >= rect.X && p.X <= rect.X+rect.Width && p.Y >= rect.Y && p.Y <= rect.Y+rect.Height {
return true
}
return false
}
func encodingWithPng(img image.Image) (encodeResult []byte, err error) {
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
return encodeResult, err
}
encodeResult = buf.Bytes()
buf.Reset()
return encodeResult, nil
}
//writeImageFile tmp
func writeImageFile(filepath string, imageBytes []byte) {
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer file.Close()
file.Write(imageBytes)
}