Click Captcha
Randomly generate text or graphics, and click the corresponding contents in order to verify them.
Install Module
$ go get -u github.com/wenlng/go-captcha/v2@latest
$ go get -u github.com/wenlng/go-captcha/v2@latest
Install assets
$ go get -u github.com/wenlng/go-captcha-assets@latest
$ go get -u github.com/wenlng/go-captcha-assets@latest
Example
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/bindata/chars"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
var textCapt click.Captcha
func init() {
builder := click.NewBuilder()
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
log.Fatalln(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
textCapt = builder.Make()
}
func main() {
captData, err := textCapt.Generate()
if err != nil {
log.Fatalln(err)
}
dotData := captData.GetData()
if dotData == nil {
log.Fatalln(">>>>> generate err")
}
dots, _ := json.Marshal(dotData)
fmt.Println(">>>>> ", string(dots))
err = captData.GetMasterImage().SaveToFile("./.caches/master.jpg", option.QualityNone)
if err != nil {
fmt.Println(err)
}
err = captData.GetThumbImage().SaveToFile("./.caches/thumb.png")
if err != nil {
fmt.Println(err)
}
}
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/golang/freetype/truetype"
"github.com/wenlng/go-captcha-assets/bindata/chars"
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
"github.com/wenlng/go-captcha-assets/resources/images"
"github.com/wenlng/go-captcha/v2/base/option"
"github.com/wenlng/go-captcha/v2/click"
)
var textCapt click.Captcha
func init() {
builder := click.NewBuilder()
// fonts
fonts, err := fzshengsksjw.GetFont()
if err != nil {
log.Fatalln(err)
}
// background images
imgs, err := images.GetImages()
if err != nil {
log.Fatalln(err)
}
builder.SetResources(
click.WithChars(chars.GetChineseChars()),
click.WithFonts([]*truetype.Font{fonts}),
click.WithBackgrounds(imgs),
)
textCapt = builder.Make()
}
func main() {
captData, err := textCapt.Generate()
if err != nil {
log.Fatalln(err)
}
dotData := captData.GetData()
if dotData == nil {
log.Fatalln(">>>>> generate err")
}
dots, _ := json.Marshal(dotData)
fmt.Println(">>>>> ", string(dots))
err = captData.GetMasterImage().SaveToFile("./.caches/master.jpg", option.QualityNone)
if err != nil {
fmt.Println(err)
}
err = captData.GetThumbImage().SaveToFile("./.caches/thumb.png")
if err != nil {
fmt.Println(err)
}
}
Reference
Create an instance
- builder.Make()
- builder.MakeWithShape()
Configure
TIP
click.NewBuilder(click.WithXxxx(), click.WithXxxx(), ....) OR builder.SetOptions(click.WithXxxx(), click.WithXxxx(), ....)
click.WithImageSize(option.Size)
click.WithRangeLen(option.RangeVal)
click.WithRangeAnglePos([]option.RangeVal)
click.WithRangeSize(option.RangeVal)
click.WithRangeColors([]string)
click.WithDisplayShadow(bool)
click.WithShadowColor(string)
click.WithShadowPoint(option.Point)
click.WithImageAlpha(float32)
click.WithUseShapeOriginalColor(bool)
click.WithThumbImageSize(option.Size)
click.WithRangeVerifyLen(option.RangeVal)
click.WithDisabledRangeVerifyLen(bool)
click.WithRangeThumbSize(option.RangeVal)
click.WithRangeThumbColors([]string)
click.WithRangeThumbBgColors([]string)
click.WithIsThumbNonDeformAbility(bool)
click.WithThumbBgDistort(int) option.DistortLevel1 (1-5)
click.WithThumbBgCirclesNum(int)
click.WithThumbBgSlimLineNum(int)
Resources
TIP
builder.SetResources(click.WithXxxx(), click.WithXxxx(), ....)
- click.WithChars([]string)
- click.WithShapes(map[string]image.Image)
- click.WithFonts([]*truetype.Font)
- click.WithBackgrounds([]image.Image)
- click.WithThumbBackgrounds([]image.Image)
Captcha Data
- GetData() map[int]*Dot
- GetMasterImage() imagedata.JPEGImageData
- GetThumbImage() imagedata.PNGImageData
Refer to the image data API Captcha Image.