Skip to content

Rotate Captcha

Randomly generate images from different angles, and ask to drag the slider to make the images coincide to verify.

Install Module

shell
$ go get -u github.com/wenlng/go-captcha/v2@latest
$ go get -u github.com/wenlng/go-captcha/v2@latest

Install assets

shell
$ go get -u github.com/wenlng/go-captcha-assets@latest
$ go get -u github.com/wenlng/go-captcha-assets@latest

Example

go
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/golang/freetype/truetype"
	"github.com/wenlng/go-captcha-assets/resources/images"
	"github.com/wenlng/go-captcha-assets/resources/tiles"
	"github.com/wenlng/go-captcha/v2/base/option"
	"github.com/wenlng/go-captcha/v2/rotate"
)

var rotateCapt slide.Captcha

func init() {
  builder := rotate.NewBuilder(rotate.WithRangeAnglePos([]option.RangeVal{
		{Min: 20, Max: 330},
	}))

	// background images
	imgs, err := images.GetImages()
	if err != nil {
		log.Fatalln(err)
	}

	// set resources
	builder.SetResources(
		rotate.WithImages(imgs),
	)
	
	rotateCapt = builder.Make()
}

func main() {
	captData, err := rotateCapt.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/resources/images"
	"github.com/wenlng/go-captcha-assets/resources/tiles"
	"github.com/wenlng/go-captcha/v2/base/option"
	"github.com/wenlng/go-captcha/v2/rotate"
)

var rotateCapt slide.Captcha

func init() {
  builder := rotate.NewBuilder(rotate.WithRangeAnglePos([]option.RangeVal{
		{Min: 20, Max: 330},
	}))

	// background images
	imgs, err := images.GetImages()
	if err != nil {
		log.Fatalln(err)
	}

	// set resources
	builder.SetResources(
		rotate.WithImages(imgs),
	)
	
	rotateCapt = builder.Make()
}

func main() {
	captData, err := rotateCapt.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()

Configure

TIP

rotate.NewBuilder(rotate.WithXxxx(), rotate.WithXxxx(), ....) OR builder.SetOptions(rotate.WithXxxx(), rotate.WithXxxx(), ....)

  • rotate.WithImageSquareSize(val int)

  • rotate.WithRangeAnglePos(vals []option.RangeVal)

  • rotate.WithRangeThumbImageSquareSize(val []int)

  • rotate.WithThumbImageAlpha(val float32)

Resources

TIP

builder.SetResources(rotate.WithXxxx(), ....)

  • rotate.WithBackgrounds([]image.Image)

Captcha Data

  • GetData() *Block
  • GetMasterImage() imagedata.PNGImageData
  • GetThumbImage() imagedata.PNGImageData

Refer to the image data API Captcha Image.