English | 中文
Golang Package go-captcha implements generation and position verification of behavioral CAPTCHAs.
⭐️ If it helps you, please give a star.
Installation of proxy go module in China
Set Proxy of go module
$ set GO111MODULE=on
$ set GOPROXY=https://goproxy.io,direct
#
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.io,direct
$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.io,direct
#
$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn,direct" >> ~/.profile
$ source ~/.profile
Dependency Library
$ go get -u github.com/golang/freetype
$ go get -u golang.org/x/crypto
$ go get -u golang.org/x/image
Install Captcha Module
$ go get -u github.com/wenlng/go-captcha/captcha
Import Captcha Module
package main
import "github.com/wenlng/go-captcha/captcha"
func main(){
}
Quick Use
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
dots, b64, tb64, key, err := capt.Generate()
if err != nil {
panic(err)
return
}
fmt.Println(len(b64))
fmt.Println(len(tb64))
fmt.Println(key)
fmt.Println(dots)
}
Captcha Instances
- New Instances or Get Single Instances
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
fmt.Println(capt)
}
Set Configuration
After version v1.2.3, the default size of large drawing is 300×240px, the default size of the small drawing is 150×40px.
Set Chars
Some fonts are attached by default. If other Chinese strings are set, you may need to import a font file.
package main
import (
"fmt"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
chars := []string{"你","好","呀","这","是","点","击","验","证","码","哟"}
_ = capt.SetRangChars(chars)
fmt.Println(capt)
}
Set Font File Configuration
You can copy the resource files under the "https://github.com/wenlng/go-captcha-example/tree/main/resources" to the directory of your project.
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
capt.SetFont([]string{
path + "/__example/resources/fonts/fzshengsksjw_cu.ttf",
})
}
Set Big Image Configuration
Tip: Some images are attached by default.
package main
import (
"fmt"
"golang.org/x/image/font"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
capt.SetBackground([]string{
path + "/__example/resources/images/1.jpg",
path + "/__example/resources/images/2.jpg",
})
capt.SetImageSize(captcha.Size{300, 300})
capt.SetImageQuality(captcha.QualityCompressNone)
capt.SetFontHinting(font.HintingFull)
capt.SetTextRangLen(captcha.RangeVal{6, 7})
capt.SetRangFontSize(captcha.RangeVal{32, 42})
capt.SetTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})
capt.SetImageFontAlpha(0.5)
capt.SetTextShadow(true)
capt.SetTextShadowColor("#101010")
capt.SetTextShadowPoint(captcha.Point{1, 1})
capt.SetTextRangAnglePos([]captcha.RangeVal{
{1, 15},
{15, 30},
{30, 45},
{315, 330},
{330, 345},
{345, 359},
})
capt.SetImageFontDistort(captcha.DistortLevel2)
}
Set Thumb Image Configuration
Tip: Some images are attached by default.
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
path, _ := os.Getwd()
capt.SetRangCheckTextLen(captcha.RangeVal{2, 4})
capt.SetRangCheckFontSize(captcha.RangeVal{24, 30})
capt.SetThumbTextRangFontColors([]string{
"#1d3f84",
"#3a6a1e",
})
capt.SetThumbBgColors([]string{
"#1d3f84",
"#3a6a1e",
})
capt.SetThumbBackground([]string{
path + "/__example/resources/images/r1.jpg",
path + "/__example/resources/images/r2.jpg",
})
capt.SetThumbBgDistort(captcha.DistortLevel2)
capt.SetThumbFontDistort(captcha.DistortLevel2)
capt.SetThumbBgCirclesNum(20)
capt.SetThumbBgSlimLineNum(3)
}
Other
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
path, _ := os.Getwd()
captcha.ClearAssetCacheWithPaths([]string{
path + "/__example/resources/images/1.jpg",
path + "/__example/resources/fonts/fonts.ttf",
})
captcha.CheckPointDist(0, 30, 0, 30, 30, 30)
captcha.CheckPointDistWithPadding(0, 30, 0, 30, 30, 30, 5)
}
Generate Captcha Data
package main
import (
"fmt"
"os"
"github.com/wenlng/go-captcha/captcha"
)
func main(){
capt := captcha.GetCaptcha()
dots, imageBase64, thumbImageBase64, key, err := capt.Generate()
if err != nil {
panic(err)
return
}
fmt.Println(len(imageBase64))
fmt.Println(len(thumbImageBase64))
fmt.Println(key)
fmt.Println(dots)
}
Api Params
// Example: Get captcha data
Respose Data = {
"code": 0,
"image_base64": "...",
"thumb_base64": "...",
"captcha_key": "...",
}
// Example: Post check data
Request Data = {
dots: "x1,y1,x2,y2,...."
key: "......"
}
Buy the author coffee: http://witkeycode.com/sponsor
LICENSE
MIT