Skip to content

旋转式验证码

随机生成不同角度的图像,并要求拖动滑块让图像重合验证。

安装

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

安装预置内嵌素材资源

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

实例

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)
	}
}

使用

创建实例

  • builder.Make() 旋转式

配置

TIP

配置举例:rotate.NewBuilder(rotate.WithXxxx(), rotate.WithXxxx(), ....) 或 builder.SetOptions(rotate.WithXxxx(), rotate.WithXxxx(), ....)

主图配置

  • rotate.WithImageSquareSize(val int) 设置主图大小
  • rotate.WithRangeAnglePos(vals []option.RangeVal) 设置校验随机角度范围

缩略图

  • rotate.WithRangeThumbImageSquareSize(val []int) 设置缩略图大小
  • rotate.WithThumbImageAlpha(val float32) 设置缩略图透明度

设置资源

TIP

配置举例:builder.SetResources(rotate.WithXxxx(), ....)

  • rotate.WithBackgrounds([]image.Image) 设置主图图片

验证码数据

  • GetData() *Block 获取当前校验的信息
  • GetMasterImage() imagedata.PNGImageData 获取主图
  • GetThumbImage() imagedata.PNGImageData 获取缩略图

关于验证码 "imagedata" API可参考 验证码图像