generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdigit.go
85 lines (71 loc) · 1.65 KB
/
digit.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
// 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 (
"clevergo.tech/captchas"
"github.com/mojocn/base64Captcha"
)
// DigitOption is a function that receives a pointer of digit driver.
type DigitOption func(*Digit)
// DigitHeight sets height.
func DigitHeight(height int) DigitOption {
return func(d *Digit) {
d.height = height
}
}
// DigitWidth sets width.
func DigitWidth(width int) DigitOption {
return func(d *Digit) {
d.width = width
}
}
// DigitLength sets length.
func DigitLength(length int) DigitOption {
return func(d *Digit) {
d.length = length
}
}
// DigitMaxSkew sets max skew.
func DigitMaxSkew(maxSkew float64) DigitOption {
return func(d *Digit) {
d.maxSkew = maxSkew
}
}
// DigitDotCount sets dot count.
func DigitDotCount(count int) DigitOption {
return func(d *Digit) {
d.dotCount = count
}
}
// Digit is a digit driver.
type Digit struct {
*driver
// captcha png height in pixel.
height int
// captcha png width in pixel.
width int
// number of digits in captcha solution.
length int
// max absolute skew factor of a single digit.
maxSkew float64
// number of background circles.
dotCount int
}
var _ captchas.Driver = NewDigit()
// NewDigit return a digit driver.
func NewDigit(opts ...DigitOption) *Digit {
d := &Digit{
driver: &driver{htmlTag: htmlTagIMG},
height: 80,
width: 220,
length: 6,
maxSkew: 0.7,
dotCount: 80,
}
for _, f := range opts {
f(d)
}
d.driver.driver = base64Captcha.NewDriverDigit(d.height, d.width, d.length, d.maxSkew, d.dotCount)
return d
}