generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring.go
105 lines (87 loc) · 2.07 KB
/
string.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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package drivers
import (
"image/color"
"clevergo.tech/captchas"
"github.com/mojocn/base64Captcha"
)
// StringOption is a function that receives a pointer of string driver.
type StringOption func(*Str)
// StringHeight sets height.
func StringHeight(height int) StringOption {
return func(s *Str) {
s.height = height
}
}
// StringWidth sets width.
func StringWidth(width int) StringOption {
return func(s *Str) {
s.width = width
}
}
// StringLength sets length.
func StringLength(length int) StringOption {
return func(s *Str) {
s.length = length
}
}
// StringSource sets source.
func StringSource(source string) StringOption {
return func(s *Str) {
s.source = source
}
}
// StringNoiseCount sets noise count.
func StringNoiseCount(count int) StringOption {
return func(s *Str) {
s.noiseCount = count
}
}
// StringBGColor sets background color.
func StringBGColor(color *color.RGBA) StringOption {
return func(s *Str) {
s.bgColor = color
}
}
// StringFonts sets fonts.
func StringFonts(fonts []string) StringOption {
return func(s *Str) {
s.fonts = fonts
}
}
// Str is a string driver.
type Str struct {
*driver
// captcha png height in pixel.
height int
// captcha png width in pixel.
width int
length int
source string
// text noise count.
noiseCount int
showLineOptions int
// background color.
bgColor *color.RGBA
fonts []string
}
const defaultStringSource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var _ captchas.Driver = NewString()
// NewString returns a string driver.
func NewString(opts ...StringOption) *Str {
d := &Str{
driver: &driver{htmlTag: htmlTagIMG},
height: 80,
width: 220,
noiseCount: 0,
length: 4,
source: defaultStringSource,
}
for _, f := range opts {
f(d)
}
d.driver.driver = base64Captcha.NewDriverString(d.height, d.width, d.noiseCount, d.showLineOptions, d.length, d.source, d.bgColor, d.fonts)
return d
}