Skip to content

Commit 0ba7114

Browse files
committed
Optimize code and resources
1 parent 252c95a commit 0ba7114

25 files changed

Lines changed: 300 additions & 126 deletions

File tree

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ package main
178178
import (
179179
"fmt"
180180
"golang.org/x/image/font"
181-
"os"
181+
"os"
182182
"github.com/wenlng/go-captcha/captcha"
183183
)
184184

@@ -204,16 +204,16 @@ func main(){
204204
})
205205

206206
// ====================================================
207-
// Method: SetImageSize(size *Size);
207+
// Method: SetImageSize(size Size);
208208
// Desc: Set size of captcha
209209
// ====================================================
210-
capt.SetImageSize(&captcha.Size{300, 300})
210+
capt.SetImageSize(captcha.Size{300, 300})
211211

212212
// ====================================================
213-
// Method: SetThumbSize(size *Size);
213+
// Method: SetThumbSize(size Size);
214214
// Desc: Set size of captcha thumb
215215
// ====================================================
216-
capt.SetThumbSize(&captcha.Size{150, 40})
216+
capt.SetThumbSize(captcha.Size{150, 40})
217217

218218
// ====================================================
219219
// Method: SetFontDPI(val int);
@@ -228,28 +228,28 @@ func main(){
228228
capt.SetFontHinting(font.HintingFull)
229229

230230
// ====================================================
231-
// Method: SetTextRangLen(val *captcha.RangeVal);
231+
// Method: SetTextRangLen(val captcha.RangeVal);
232232
// Desc: Set random length of captcha text
233233
// ====================================================
234-
capt.SetTextRangLen(&captcha.RangeVal{6, 7})
234+
capt.SetTextRangLen(captcha.RangeVal{6, 7})
235235

236236
// ====================================================
237-
// Method: SetRangFontSize(val *captcha.RangeVal);
237+
// Method: SetRangFontSize(val captcha.RangeVal);
238238
// Desc: Set random size of captcha text
239239
// ====================================================
240-
capt.SetRangFontSize(&captcha.RangeVal{32, 42})
240+
capt.SetRangFontSize(captcha.RangeVal{32, 42})
241241

242242
// ====================================================
243-
// Method: SetRangCheckTextLen(val *captcha.RangeVal);
243+
// Method: SetRangCheckTextLen(val captcha.RangeVal);
244244
// Desc:Set random length of check text
245245
// ====================================================
246-
capt.SetRangCheckTextLen(&captcha.RangeVal{2, 4})
246+
capt.SetRangCheckTextLen(captcha.RangeVal{2, 4})
247247

248248
// ====================================================
249-
// Method: SetRangCheckFontSize(val *captcha.RangeVal);
249+
// Method: SetRangCheckFontSize(val captcha.RangeVal);
250250
// Desc:Set random size of check text
251251
// ====================================================
252-
capt.SetRangCheckFontSize(&captcha.RangeVal{24, 30})
252+
capt.SetRangCheckFontSize(captcha.RangeVal{24, 30})
253253

254254
// ====================================================
255255
// Method: SetTextRangFontColors(colors []string);
@@ -259,6 +259,15 @@ func main(){
259259
"#1d3f84",
260260
"#3a6a1e",
261261
})
262+
263+
// ====================================================
264+
// Method: SetThumbTextRangFontColors(colors []string);
265+
// Desc: Set random hex color of captcha text
266+
// ====================================================
267+
capt.SetThumbTextRangFontColors([]string{
268+
"#1d3f84",
269+
"#3a6a1e",
270+
})
262271

263272
// ====================================================
264273
// Method: SetImageFontAlpha(val float64);
@@ -267,10 +276,10 @@ func main(){
267276
capt.SetImageFontAlpha(0.5)
268277

269278
// ====================================================
270-
// Method: SetTextRangAnglePos(pos []*RangeVal);
279+
// Method: SetTextRangAnglePos(pos []RangeVal);
271280
// Desc:Set angle of captcha text
272281
// ====================================================
273-
capt.SetTextRangAnglePos([]*captcha.RangeVal{
282+
capt.SetTextRangAnglePos([]captcha.RangeVal{
274283
{1, 15},
275284
{15, 30},
276285
{30, 45},
@@ -315,7 +324,6 @@ func main(){
315324
// ====================================================
316325
capt.SetThumbFontDistort(captcha.DistortLevel2)
317326

318-
319327
// ====================================================
320328
// Method: SetThumbBgCirclesNum(val int);
321329
// Desc:Set circles number of captcha background

README_zh.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ package main
181181
import (
182182
"fmt"
183183
"os"
184+
"golang.org/x/image/font"
184185
"github.com/wenlng/go-captcha/captcha"
185186
)
186187

@@ -206,20 +207,20 @@ func main(){
206207
})
207208

208209
// ====================================================
209-
// Method: SetImageSize(size *Size);
210+
// Method: SetImageSize(size Size);
210211
// Desc: 设置验证码主图的尺寸
211212
// ====================================================
212-
capt.SetImageSize(&captcha.Size{300, 300})
213+
capt.SetImageSize(captcha.Size{300, 300})
213214

214215
// ====================================================
215-
// Method: SetThumbSize(size *Size);
216+
// Method: SetThumbSize(size Size);
216217
// Desc: 设置验证码缩略图的尺寸
217218
// ====================================================
218-
capt.SetThumbSize(&captcha.Size{150, 40})
219+
capt.SetThumbSize(captcha.Size{150, 40})
219220

220221
// ====================================================
221222
// Method: SetFontDPI(val int);
222-
// Desc: 设置验证码字体DPI,最好是72
223+
// Desc: 设置验证码字体DPI,最优72
223224
// ====================================================
224225
capt.SetFontDPI(72)
225226

@@ -229,39 +230,47 @@ func main(){
229230
// ====================================================
230231
capt.SetFontHinting(font.HintingFull)
231232

232-
233233
// ====================================================
234-
// Method: SetTextRangLen(val *captcha.RangeVal);
234+
// Method: SetTextRangLen(val captcha.RangeVal);
235235
// Desc: 设置验证码文本显示的总数随机范围
236236
// ====================================================
237-
capt.SetTextRangLen(&captcha.RangeVal{6, 7})
237+
capt.SetTextRangLen(captcha.RangeVal{6, 7})
238238

239239
// ====================================================
240-
// Method: SetRangFontSize(val *captcha.RangeVal);
240+
// Method: SetRangFontSize(val captcha.RangeVal);
241241
// Desc: 设置验证码文本的随机大小
242242
// ====================================================
243-
capt.SetRangFontSize(&captcha.RangeVal{32, 42})
243+
capt.SetRangFontSize(captcha.RangeVal{32, 42})
244244

245245
// ====================================================
246-
// Method: SetRangCheckTextLen(val *captcha.RangeVal);
246+
// Method: SetRangCheckTextLen(val captcha.RangeVal);
247247
// Desc:设置验证码校验文本的随机长度范围
248248
// ====================================================
249-
capt.SetRangCheckTextLen(&captcha.RangeVal{2, 4})
249+
capt.SetRangCheckTextLen(captcha.RangeVal{2, 4})
250250

251251
// ====================================================
252-
// Method: SetRangCheckFontSize(val *captcha.RangeVal);
252+
// Method: SetRangCheckFontSize(val captcha.RangeVal);
253253
// Desc:设置验证码校验文本的随机大小
254254
// ====================================================
255-
capt.SetRangCheckFontSize(&captcha.RangeVal{24, 30})
255+
capt.SetRangCheckFontSize(captcha.RangeVal{24, 30})
256256

257257
// ====================================================
258258
// Method: SetTextRangFontColors(colors []string);
259-
// Desc: 设置验证码校验文本的随机十六进制颜色
259+
// Desc: 设置验证码文本的随机十六进制颜色
260260
// ====================================================
261261
capt.SetTextRangFontColors([]string{
262262
"#1d3f84",
263263
"#3a6a1e",
264264
})
265+
266+
// ====================================================
267+
// Method: SetThumbTextRangFontColors(colors []string);
268+
// Desc: 设置验证码缩略图校验文本的随机十六进制颜色
269+
// ====================================================
270+
capt.SetThumbTextRangFontColors([]string{
271+
"#1d3f84",
272+
"#3a6a1e",
273+
})
265274

266275
// ====================================================
267276
// Method: SetImageFontAlpha(val float64);
@@ -270,10 +279,10 @@ func main(){
270279
capt.SetImageFontAlpha(0.5)
271280

272281
// ====================================================
273-
// Method: SetTextRangAnglePos(pos []*RangeVal);
282+
// Method: SetTextRangAnglePos(pos []RangeVal);
274283
// Desc:设置验证码文本的旋转角度
275284
// ====================================================
276-
capt.SetTextRangAnglePos([]*captcha.RangeVal{
285+
capt.SetTextRangAnglePos([]captcha.RangeVal{
277286
{1, 15},
278287
{15, 30},
279288
{30, 45},

__example/.air.conf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
2+
3+
# Working directory
4+
# . or absolute path, please note that the directories following must be under root.
5+
root = "."
6+
tmp_dir = "./.cache"
7+
8+
[build]
9+
# Just plain old shell command. You could use `make` as well.
10+
cmd = "go build -o ./.cache/main.exe ."
11+
# Binary file yields from `cmd`.
12+
bin = "./.cache\\main.exe"
13+
# Customize binary.
14+
full_bin = ""
15+
# Watch these filename extensions.
16+
include_ext = ["go", "yml"]
17+
# Ignore these filename extensions or directories.
18+
exclude_dir = ["./.cache"]
19+
# Watch these directories if you specified.
20+
include_dir = ["./../captcha/*"]
21+
# Exclude files.
22+
exclude_file = []
23+
# This log file places in your tmp_dir.
24+
log = "air.log"
25+
# It's not necessary to trigger build each time file changes if it's too frequent.
26+
delay = 1000 # ms
27+
# Stop running old binary when build errors occur.
28+
stop_on_error = true
29+
# Send Interrupt signal before killing process (windows does not support this feature)
30+
send_interrupt = false
31+
# Delay after sending Interrupt signal
32+
kill_delay = 500 # ms
33+
34+
[log]
35+
# Show log time
36+
time = true
37+
38+
[color]
39+
# Customize each part's color. If no color found, use the raw app log.
40+
main = "magenta"
41+
watcher = "cyan"
42+
build = "yellow"
43+
runner = "green"
44+
45+
[misc]
46+
# Delete tmp directory on exit
47+
clean_on_exit = false

__example/demo.html

Lines changed: 29 additions & 2 deletions
Large diffs are not rendered by default.

__example/main.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func main() {
2626
// Example: demo
2727
http.HandleFunc("/demo", demo)
2828

29-
log.Println("ListenAndServe 0.0.0.0:8082")
30-
err := http.ListenAndServe(":8082", nil)
29+
log.Println("ListenAndServe 0.0.0.0:8002")
30+
err := http.ListenAndServe(":8002", nil)
3131
if err != nil {
3232
log.Fatal("ListenAndServe: ", err)
3333
}
@@ -64,28 +64,36 @@ func getCaptchaData(w http.ResponseWriter, r *http.Request) {
6464
//chars := []string{"你","好","呀","这","是","点","击","验","证","码","哟"}
6565
//_ = capt.SetRangChars(chars)
6666

67-
capt.SetTextRangFontColors([]string{
68-
"#006600",
69-
"#005db9",
70-
"#aa002a",
71-
"#875400",
72-
"#6e3700",
73-
"#333333",
74-
"#660033",
75-
})
67+
//capt.SetTextRangFontColors([]string{
68+
// "#fdefac",
69+
// "#8abcff",
70+
// "#ffa37a",
71+
// "#fcb3ff",
72+
// "#b4fed4",
73+
// "#cbfaa9",
74+
//})
75+
//
76+
//capt.SetThumbTextRangFontColors([]string{
77+
// "#006600",
78+
// "#005db9",
79+
// "#aa002a",
80+
// "#875400",
81+
// "#6e3700",
82+
// "#660033",
83+
//})
7684

7785
// capt.SetFont([]string{
7886
// getPWD() + "/resources/fonts/fzshengsksjw_cu.ttf",
7987
// getPWD() + "/resources/fonts/hyrunyuan.ttf",
8088
// })
81-
//
82-
// capt.SetBackground([]string{
83-
// getPWD() + "/resources/images/1.jpg",
84-
// getPWD() + "/resources/images/2.jpg",
85-
// getPWD() + "/resources/images/3.jpg",
86-
// getPWD() + "/resources/images/4.jpg",
87-
// getPWD() + "/resources/images/5.jpg",
88-
// })
89+
90+
//capt.SetBackground([]string{
91+
// getPWD() + "/resources/images/1.jpg",
92+
// getPWD() + "/resources/images/2.jpg",
93+
// getPWD() + "/resources/images/3.jpg",
94+
// getPWD() + "/resources/images/4.jpg",
95+
// getPWD() + "/resources/images/5.jpg",
96+
//})
8997

9098
//capt.SetThumbBackground([]string{
9199
// getPWD() + "/resources/images/thumb/r1.jpg",
@@ -163,9 +171,10 @@ func checkCaptcha(w http.ResponseWriter, r *http.Request) {
163171
for i, dot := range dct {
164172
j := i * 2
165173
k := i*2 + 1
166-
a, _ := strconv.Atoi(src[j])
167-
b, _ := strconv.Atoi(src[k])
168-
chkRet = checkDist(a, b, dot.Dx, dot.Dy, dot.Width, dot.Height)
174+
sx, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[j]), 64)
175+
sy, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[k]), 64)
176+
// 检测点位置
177+
chkRet = captcha.CheckPointDist(int64(sx), int64(sy), int64(dot.Dx), int64(dot.Dy), int64(dot.Width), int64(dot.Height))
169178
if !chkRet {
170179
break
171180
}
@@ -231,7 +240,7 @@ func readCache(file string) string {
231240
* @param height
232241
* @return bool
233242
*/
234-
func checkDist(sx, sy, dx, dy, width int, height int) bool {
243+
func checkDist(sx, sy, dx, dy, width, height int64) bool {
235244
return sx >= dx &&
236245
sx <= dx+width &&
237246
sy <= dy &&

__example/resources/images/1.jpg

21 KB
Loading

__example/resources/images/2.jpg

-9.5 KB
Loading

__example/resources/images/3.jpg

-3.18 KB
Loading

__example/resources/images/4.jpg

36.7 KB
Loading

__example/resources/images/5.jpg

9.71 KB
Loading

0 commit comments

Comments
 (0)