-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_static_image.go
53 lines (43 loc) · 1.43 KB
/
draw_static_image.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
package coolCaptcha
import (
"image"
"math/rand"
"github.com/fogleman/gg"
)
// drawStaticImage
// @Description: The set parameters are drawn into an image, then the base64 data and code are returned
// @receiver c
// @return imageBase64Data: The base64 data of the graphic captcha can generate an image on the front end
// @return code: Randomly generated characters that are compared to the verification code entered by the user. When custom code is used, uppercase code is output.
// @return err
func (c *Config) drawStaticImage(codeItems []string) (imageOriginData image.Image, err error) {
// create a new image
dc := gg.NewContext(c.Width, c.Height)
dc.SetHexColor(c.BackgroundHexColor)
dc.Clear()
// load font
face, err := loadFontFace()
dc.SetFontFace(face)
// write random code and set lines
randomColorIndex := rand.Perm(len(c.LineHexColors))
for index, character := range codeItems {
textConfig := fontConfig{
Character: character,
X: float64((c.Width / 6) + (index * 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,
}
c.writeText(dc, textConfig)
// set 3 lines with random color
if index < charactersLength-1 {
c.setStaticLine(dc, c.LineHexColors[randomColorIndex[index]])
}
}
imageOriginData = dc.Image()
if c.DevMode {
c.devModeHandler(dc)
}
return
}