-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.go
142 lines (115 loc) · 2.93 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
package coolCaptcha
import (
_ "embed"
"image"
"image/color/palette"
"image/draw"
"image/gif"
"math/rand"
)
// GenerateImage
// @Description: Generate a static image
// @receiver c
// @return imageBase64Data
// @return code
// @return err
func (c *Config) GenerateImage() (imageBase64Data string, code string, err error) {
err = c.checkConfig()
if err != nil {
return
}
code, codeItems, err := c.getLastCode()
if err != nil {
return
}
originImage, err := c.drawStaticImage(codeItems)
if err != nil {
return
}
imageBase64Data, err = convertImageToBase64(originImage)
if err != nil {
return
}
return
}
// GenerateGif
// @Description: Generate gif
// @receiver c
// @return gifBase64Data
// @return code
// @return err
func (c *Config) GenerateGif() (gifBase64Data string, code string, err error) {
err = c.checkConfig()
if err != nil {
return
}
code, codeItems, err := c.getLastCode()
if err != nil {
return
}
// gen random line config
var lineConfigs []lineConfig
for i := 0; i < 4; i++ {
line := c.genLineCoordinates()
line.Width = c.lineWidth()
lineConfigs = append(lineConfigs, line)
}
var textConfigs []fontConfig
for i := 0; i < charactersLength; i++ {
textConfig := fontConfig{
Character: codeItems[i],
X: float64((c.Width / 6) + (i * c.Width / 4)),
Y: randomFloat64(float64(c.Height/4), float64(c.Height/3)),
AX: randomFloat64(0.3, 0.7),
AY: randomFloat64(0.3, 0.7),
Color: c.FontHexColor,
}
textConfigs = append(textConfigs, textConfig)
}
imageCount := 12
randomLineIndex := rand.Perm(len(c.LineHexColors))
var images []image.Image
for i := 0; i < imageCount; i++ {
for lineIndex, line := range lineConfigs {
// start
lineConfigs[lineIndex].Start.Y = genRandomPoint(line.Start.Y, 15)
// end
lineConfigs[lineIndex].End.Y = genRandomPoint(line.End.Y, 15)
// zigzag
lineConfigs[lineIndex].Zigzag.X = genRandomPoint(line.Zigzag.X, 20)
lineConfigs[lineIndex].Zigzag.Y = genRandomPoint(line.Zigzag.Y, 20)
lineConfigs[lineIndex].Color = c.LineHexColors[randomLineIndex[lineIndex]]
}
for textIndex, text := range textConfigs {
textConfigs[textIndex].X = genRandomPoint(text.X, 6)
textConfigs[textIndex].Y = genRandomPoint(text.Y, 6)
}
originImage, err := c.drawGifImage(c.BackgroundHexColor, textConfigs, lineConfigs)
if err != nil {
break
}
images = append(images, originImage)
}
if err != nil {
return
}
outGif := &gif.GIF{
LoopCount: 0,
}
delay := 16
for _, imageItem := range images {
bounds := imageItem.Bounds()
paletteImage := image.NewPaletted(bounds, palette.Plan9)
draw.Draw(paletteImage, paletteImage.Rect, imageItem, bounds.Min, draw.Over)
outGif.Image = append(outGif.Image, paletteImage)
outGif.Delay = append(outGif.Delay, delay)
}
gifBase64Data, err = convertGifToBase64(outGif)
if err != nil {
return
}
if c.DevMode {
c.devModelHandlerForGif(outGif)
}
return
}