-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
92 lines (81 loc) · 1.8 KB
/
base.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
package captcha
import (
_ "embed"
"image"
"image/color"
"time"
"github.com/virzz/logger"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
type Captcha interface {
Draw() (image.Image, *CaptchaData) // 验证码生成逻辑并绘制图片
}
type CaptchaBase struct {
fontFace font.Face
width, height int
background, front color.Color
fontSize float64
expire int64
line, point int
}
//go:embed monaco_ascii.ttf
var fontData []byte
var _ Captcha = (*CaptchaBase)(nil)
func New(opts ...Option) *CaptchaBase {
// Default
c := &CaptchaBase{
fontSize: 14,
width: 80,
height: 30,
expire: 300,
background: color.White,
front: color.Black,
}
// Option
for _, opt := range opts {
opt(c)
}
// Default Font
if c.fontFace == nil {
obj, _ := sfnt.Parse(fontData)
fontFace, err := opentype.NewFace(obj, &opentype.FaceOptions{
Size: c.fontSize, DPI: 72, Hinting: font.HintingNone,
})
if err != nil {
logger.Error(err)
} else {
c.fontFace = fontFace
}
}
return c
}
type CaptchaData struct {
Content string
Result string
Expire int64
}
func (c *CaptchaBase) Options(opts ...Option) *CaptchaBase {
for _, opt := range opts {
opt(c)
}
return c
}
func (c *CaptchaBase) Draw() (image.Image, *CaptchaData) {
panic("No implementation found")
}
func (c *CaptchaBase) draw(data string) (image.Image, *CaptchaData) {
img := newImage(c.width, c.height)
img.fillBkg(c.background) // 背景
img.drawText(data, c.fontFace, c.front) // 内容
// 干扰点
img.drawPoints(c.point)
// 干扰线
img.drawLines(c.line)
// TODO: 旋转
// img.rotate()
// TODO: 扭曲
// img.distort()
return img, &CaptchaData{Content: data, Expire: c.expire + time.Now().Unix()}
}