go-captcha行為驗證碼 Go 庫
相關(guān)連接
Github:https://github.com/wenlng/go-captcha
Gitee:https://gitee.com/wenlng/go-captcha
Vue實例代碼:https://github.com/wenlng/go-captcha-vue
React實例代碼:https://github.com/wenlng/go-captcha-react
在線演示:http://47.104.180.148:8081/go_captcha_demo
作者網(wǎng)站:http://witkeycode.com
安裝模塊
go get -u github.com/wenlng/go-captcha
引入模塊
package main
import "github.com/wenlng/go-captcha/captcha"
快速使用
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
// Captcha Single Instances
capt := captcha.GetCaptcha()
// Generate Captcha
dots, b64, tb64, key, err := capt.Generate()
if err != nil {
panic(err)
return
}
// Main image base64 code
fmt.Println(len(b64))
// Thumb image base64 code
fmt.Println(len(tb64))
// Only key
fmt.Println(key)
// Dot data For verification
fmt.Println(dots)
}
創(chuàng)建實例或獲取單例實例
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
// Captcha Instances
// capt := captcha.NewCaptcha()
// Captcha Single Instances
capt := captcha.GetCaptcha()
// ====================================================
fmt.Println(capt)
}
驗證碼配置項
v1.2.3版本后大圖默認尺寸為:300×240px,小圖默認尺寸為:150×40px。
文本相關(guān)配置
默認情況下內(nèi)置了定制的字體。如果設(shè)置了其他中文的文字,則可能需要設(shè)置字體文件。
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
// ====================================================
// Method: SetRangChars (chars []string) error;
// Desc: 設(shè)置驗證碼文本隨機種子
// ====================================================
// 字符
//chars := "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
//_ = capt.SetRangChars(strings.Split(chars, ""))
// 雙字母
//chars := []string{"HE","CA","WO","NE","HT","IE","PG","GI","CH","CO","DA"}
//_ = capt.SetRangChars(chars)
// 漢字
chars := []string{"你","好","呀","這","是","點","擊","驗","證","碼","喲"}
_ = capt.SetRangChars(chars)
// ====================================================
fmt.Println(capt)
}
大圖相關(guān)配置
package main
import (
"fmt"
"os"
"golang.org/x/image/font"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
// ====================================================
// Method: SetBackground(color []string);
// Desc: 設(shè)置驗證碼背景圖
// ====================================================
capt.SetBackground([]string{
path + "/__example/resources/images/1.jpg",
path + "/__example/resources/images/2.jpg",
})
// ====================================================
// Method: SetImageSize(size Size);
// Desc: 設(shè)置驗證碼主圖的尺寸
// ====================================================
capt.SetImageSize(captcha.Size{300, 300})
// ====================================================
// Method: SetImageQuality(val int);
// Desc: 設(shè)置驗證碼主圖清晰度,壓縮級別范圍1-5,QualityCompressNone無壓縮,默認為最底壓縮級別
// ====================================================
capt.SetImageQuality(captcha.QualityCompressNone)
// ====================================================
// Method: SetFontHinting(val font.Hinting);
// Desc: 設(shè)置字體Hinting值 (HintingNone,HintingVertical,HintingFull)
// ====================================================
capt.SetFontHinting(font.HintingFull)
// ====================================================
// Method: SetTextRangLen(val captcha.RangeVal);
// Desc: 設(shè)置驗證碼文本顯示的總數(shù)隨機范圍
// ====================================================
capt.SetTextRangLen(captcha.RangeVal{6, 7})
// ====================================================
// Method: SetRangFontSize(val captcha.RangeVal);
// Desc: 設(shè)置驗證碼文本的隨機大小
// ====================================================
capt.SetRangFontSize(captcha.RangeVal{32, 42})
// ====================================================
// Method: SetTextRangFontColors(colors []string);
// Desc: 設(shè)置驗證碼文本的隨機十六進制顏色
// ====================================================
capt.SetTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})
// ====================================================
// Method: SetImageFontAlpha(val float64);
// Desc:設(shè)置驗證碼字體的透明度
// ====================================================
capt.SetImageFontAlpha(0.5)
// ====================================================
// Method: SetTextShadow(val bool);
// Desc:設(shè)置字體陰影
// ====================================================
capt.SetTextShadow(true)
// ====================================================
// Method: SetTextShadowColor(val string);
// Desc:設(shè)置字體陰影顏色
// ====================================================
capt.SetTextShadowColor("#101010")
// ====================================================
// Method: SetTextShadowPoint(val captcha.Point);
// Desc:設(shè)置字體陰影偏移位置
// ====================================================
capt.SetTextShadowPoint(captcha.Point{1, 1})
// ====================================================
// Method: SetTextRangAnglePos(pos []captcha.RangeVal);
// Desc:設(shè)置驗證碼文本的旋轉(zhuǎn)角度
// ====================================================
capt.SetTextRangAnglePos([]captcha.RangeVal{
{1, 15},
{15, 30},
{30, 45},
{315, 330},
{330, 345},
{345, 359},
})
// ====================================================
// Method: SetImageFontDistort(val int);
// Desc:設(shè)置驗證碼字體的扭曲程度
// ====================================================
capt.SetImageFontDistort(captcha.DistortLevel2)
}
小圖相關(guān)配置
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
// ====================================================
// Method: SetThumbSize(size Size);
// Desc: 設(shè)置縮略圖的尺寸
// ====================================================
capt.SetThumbSize(captcha.Size{150, 40})
// ====================================================
// Method: SetRangCheckTextLen(val captcha.RangeVal);
// Desc:設(shè)置縮略圖校驗文本的隨機長度范圍
// ====================================================
capt.SetRangCheckTextLen(captcha.RangeVal{2, 4})
// ====================================================
// Method: SetRangCheckFontSize(val captcha.RangeVal);
// Desc:設(shè)置縮略圖校驗文本的隨機大小
// ====================================================
capt.SetRangCheckFontSize(captcha.RangeVal{24, 30})
// ====================================================
// Method: SetThumbTextRangFontColors(colors []string);
// Desc: 設(shè)置縮略圖文本的隨機十六進制顏色
// ====================================================
capt.SetThumbTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})
// ====================================================
// Method: SetThumbBgColors(colors []string);
// Desc: 設(shè)置縮略圖的背景隨機十六進制顏色
// ====================================================
capt.SetThumbBgColors([]string{
"#1d3f84",
"#3a6a1e",
})
// ====================================================
// Method: SetThumbBackground(colors []string);
// Desc:設(shè)置縮略圖的隨機圖像背景
// ====================================================
capt.SetThumbBackground([]string{
path + "/__example/resources/images/r1.jpg",
path + "/__example/resources/images/r2.jpg",
})
// ====================================================
// Method: SetThumbBgDistort(val int);
// Desc:設(shè)置縮略圖背景的扭曲程度
// ====================================================
capt.SetThumbBgDistort(captcha.DistortLevel2)
// ====================================================
// Method: SetThumbFontDistort(val int);
// Desc:設(shè)置縮略圖字體的扭曲程度
// ====================================================
capt.SetThumbFontDistort(captcha.DistortLevel2)
// ====================================================
// Method: SetThumbBgCirclesNum(val int);
// Desc:設(shè)置縮略圖背景的圈點數(shù)
// ====================================================
capt.SetThumbBgCirclesNum(20)
// ====================================================
// Method: SetThumbBgSlimLineNum(val int);
// Desc:設(shè)置縮略圖背景的線條數(shù)
// ====================================================
capt.SetThumbBgSlimLineNum(3)
}
其它
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
// ====================================================
// Method: ClearAssetCacheWithPath(paths []string) bool;
// Desc: 根據(jù)路徑消除對應(yīng)的資源緩存
// ====================================================
capt.ClearAssetCacheWithPaths([]string{
path + "/__example/resources/images/1.jpg",
path + "/__example/resources/images/2.jpg",
})
// ====================================================
// Method: captcha.CheckPointDist(sx, sy, dx, dy, width, height int64) bool;
// Desc: 校驗點的位置
// ====================================================
captcha.CheckPointDist(0, 30, 0, 30, 30, 30)
// ====================================================
// Method: captcha.CheckPointDistWithPadding(sx, sy, dx, dy, width, height, padding int64) bool;
// Desc: 校驗點的位置
// ====================================================
captcha.CheckPointDistWithPadding(0, 30, 0, 30, 30, 30, 5)
}評論
圖片
表情
