Skip to content

Commit

Permalink
Optimize code and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlng committed Dec 20, 2021
1 parent 252c95a commit 0ba7114
Show file tree
Hide file tree
Showing 25 changed files with 300 additions and 126 deletions.
40 changes: 24 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ package main
import (
"fmt"
"golang.org/x/image/font"
"os"
"os"
"github.com/wenlng/go-captcha/captcha"
)

Expand All @@ -204,16 +204,16 @@ func main(){
})

// ====================================================
// Method: SetImageSize(size *Size);
// Method: SetImageSize(size Size);
// Desc: Set size of captcha
// ====================================================
capt.SetImageSize(&captcha.Size{300, 300})
capt.SetImageSize(captcha.Size{300, 300})

// ====================================================
// Method: SetThumbSize(size *Size);
// Method: SetThumbSize(size Size);
// Desc: Set size of captcha thumb
// ====================================================
capt.SetThumbSize(&captcha.Size{150, 40})
capt.SetThumbSize(captcha.Size{150, 40})

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

// ====================================================
// Method: SetTextRangLen(val *captcha.RangeVal);
// Method: SetTextRangLen(val captcha.RangeVal);
// Desc: Set random length of captcha text
// ====================================================
capt.SetTextRangLen(&captcha.RangeVal{6, 7})
capt.SetTextRangLen(captcha.RangeVal{6, 7})

// ====================================================
// Method: SetRangFontSize(val *captcha.RangeVal);
// Method: SetRangFontSize(val captcha.RangeVal);
// Desc: Set random size of captcha text
// ====================================================
capt.SetRangFontSize(&captcha.RangeVal{32, 42})
capt.SetRangFontSize(captcha.RangeVal{32, 42})

// ====================================================
// Method: SetRangCheckTextLen(val *captcha.RangeVal);
// Method: SetRangCheckTextLen(val captcha.RangeVal);
// Desc:Set random length of check text
// ====================================================
capt.SetRangCheckTextLen(&captcha.RangeVal{2, 4})
capt.SetRangCheckTextLen(captcha.RangeVal{2, 4})

// ====================================================
// Method: SetRangCheckFontSize(val *captcha.RangeVal);
// Method: SetRangCheckFontSize(val captcha.RangeVal);
// Desc:Set random size of check text
// ====================================================
capt.SetRangCheckFontSize(&captcha.RangeVal{24, 30})
capt.SetRangCheckFontSize(captcha.RangeVal{24, 30})

// ====================================================
// Method: SetTextRangFontColors(colors []string);
Expand All @@ -259,6 +259,15 @@ func main(){
"#1d3f84",
"#3a6a1e",
})

// ====================================================
// Method: SetThumbTextRangFontColors(colors []string);
// Desc: Set random hex color of captcha text
// ====================================================
capt.SetThumbTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})

// ====================================================
// Method: SetImageFontAlpha(val float64);
Expand All @@ -267,10 +276,10 @@ func main(){
capt.SetImageFontAlpha(0.5)

// ====================================================
// Method: SetTextRangAnglePos(pos []*RangeVal);
// Method: SetTextRangAnglePos(pos []RangeVal);
// Desc:Set angle of captcha text
// ====================================================
capt.SetTextRangAnglePos([]*captcha.RangeVal{
capt.SetTextRangAnglePos([]captcha.RangeVal{
{1, 15},
{15, 30},
{30, 45},
Expand Down Expand Up @@ -315,7 +324,6 @@ func main(){
// ====================================================
capt.SetThumbFontDistort(captcha.DistortLevel2)


// ====================================================
// Method: SetThumbBgCirclesNum(val int);
// Desc:Set circles number of captcha background
Expand Down
43 changes: 26 additions & 17 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ package main
import (
"fmt"
"os"
"golang.org/x/image/font"
"github.com/wenlng/go-captcha/captcha"
)

Expand All @@ -206,20 +207,20 @@ func main(){
})

// ====================================================
// Method: SetImageSize(size *Size);
// Method: SetImageSize(size Size);
// Desc: 设置验证码主图的尺寸
// ====================================================
capt.SetImageSize(&captcha.Size{300, 300})
capt.SetImageSize(captcha.Size{300, 300})

// ====================================================
// Method: SetThumbSize(size *Size);
// Method: SetThumbSize(size Size);
// Desc: 设置验证码缩略图的尺寸
// ====================================================
capt.SetThumbSize(&captcha.Size{150, 40})
capt.SetThumbSize(captcha.Size{150, 40})

// ====================================================
// Method: SetFontDPI(val int);
// Desc: 设置验证码字体DPI,最好是72
// Desc: 设置验证码字体DPI,最优72
// ====================================================
capt.SetFontDPI(72)

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


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

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

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

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

// ====================================================
// Method: SetTextRangFontColors(colors []string);
// Desc: 设置验证码校验文本的随机十六进制颜色
// Desc: 设置验证码文本的随机十六进制颜色
// ====================================================
capt.SetTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})

// ====================================================
// Method: SetThumbTextRangFontColors(colors []string);
// Desc: 设置验证码缩略图校验文本的随机十六进制颜色
// ====================================================
capt.SetThumbTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})

// ====================================================
// Method: SetImageFontAlpha(val float64);
Expand All @@ -270,10 +279,10 @@ func main(){
capt.SetImageFontAlpha(0.5)

// ====================================================
// Method: SetTextRangAnglePos(pos []*RangeVal);
// Method: SetTextRangAnglePos(pos []RangeVal);
// Desc:设置验证码文本的旋转角度
// ====================================================
capt.SetTextRangAnglePos([]*captcha.RangeVal{
capt.SetTextRangAnglePos([]captcha.RangeVal{
{1, 15},
{15, 30},
{30, 45},
Expand Down
47 changes: 47 additions & 0 deletions __example/.air.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "./.cache"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./.cache/main.exe ."
# Binary file yields from `cmd`.
bin = "./.cache\\main.exe"
# Customize binary.
full_bin = ""
# Watch these filename extensions.
include_ext = ["go", "yml"]
# Ignore these filename extensions or directories.
exclude_dir = ["./.cache"]
# Watch these directories if you specified.
include_dir = ["./../captcha/*"]
# Exclude files.
exclude_file = []
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms

[log]
# Show log time
time = true

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = false
31 changes: 29 additions & 2 deletions __example/demo.html

Large diffs are not rendered by default.

55 changes: 32 additions & 23 deletions __example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func main() {
// Example: demo
http.HandleFunc("/demo", demo)

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

capt.SetTextRangFontColors([]string{
"#006600",
"#005db9",
"#aa002a",
"#875400",
"#6e3700",
"#333333",
"#660033",
})
//capt.SetTextRangFontColors([]string{
// "#fdefac",
// "#8abcff",
// "#ffa37a",
// "#fcb3ff",
// "#b4fed4",
// "#cbfaa9",
//})
//
//capt.SetThumbTextRangFontColors([]string{
// "#006600",
// "#005db9",
// "#aa002a",
// "#875400",
// "#6e3700",
// "#660033",
//})

// capt.SetFont([]string{
// getPWD() + "/resources/fonts/fzshengsksjw_cu.ttf",
// getPWD() + "/resources/fonts/hyrunyuan.ttf",
// })
//
// capt.SetBackground([]string{
// getPWD() + "/resources/images/1.jpg",
// getPWD() + "/resources/images/2.jpg",
// getPWD() + "/resources/images/3.jpg",
// getPWD() + "/resources/images/4.jpg",
// getPWD() + "/resources/images/5.jpg",
// })

//capt.SetBackground([]string{
// getPWD() + "/resources/images/1.jpg",
// getPWD() + "/resources/images/2.jpg",
// getPWD() + "/resources/images/3.jpg",
// getPWD() + "/resources/images/4.jpg",
// getPWD() + "/resources/images/5.jpg",
//})

//capt.SetThumbBackground([]string{
// getPWD() + "/resources/images/thumb/r1.jpg",
Expand Down Expand Up @@ -163,9 +171,10 @@ func checkCaptcha(w http.ResponseWriter, r *http.Request) {
for i, dot := range dct {
j := i * 2
k := i*2 + 1
a, _ := strconv.Atoi(src[j])
b, _ := strconv.Atoi(src[k])
chkRet = checkDist(a, b, dot.Dx, dot.Dy, dot.Width, dot.Height)
sx, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[j]), 64)
sy, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[k]), 64)
// 检测点位置
chkRet = captcha.CheckPointDist(int64(sx), int64(sy), int64(dot.Dx), int64(dot.Dy), int64(dot.Width), int64(dot.Height))
if !chkRet {
break
}
Expand Down Expand Up @@ -231,7 +240,7 @@ func readCache(file string) string {
* @param height
* @return bool
*/
func checkDist(sx, sy, dx, dy, width int, height int) bool {
func checkDist(sx, sy, dx, dy, width, height int64) bool {
return sx >= dx &&
sx <= dx+width &&
sy <= dy &&
Expand Down
Binary file modified __example/resources/images/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __example/resources/images/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __example/resources/images/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __example/resources/images/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __example/resources/images/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __example/resources/images/6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __example/resources/images/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 11 additions & 11 deletions captcha/assets/bin.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion captcha/assets/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func findAsset(path string) ([]byte, error) {
*/
func DefaultBinFontList() []string {
return []string{
"assets/fonts/hyrunyuan.ttf",
"assets/fonts/fzshengsksjw_cu.ttf",
}
}

Expand Down
Loading

0 comments on commit 0ba7114

Please sign in to comment.