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/slide"
)

var slideCapt slide.Captcha

func init() {
  builder := slide.NewBuilder(
		//slide.WithGenGraphNumber(2),
		slide.WithEnableGraphVerticalRandom(true),
	)

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

	graphs, err := tiles.GetTiles()
	if err != nil {
		log.Fatalln(err)
	}

	var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
	for i := 0; i < len(graphs); i++ {
		graph := graphs[i]
		newGraphs = append(newGraphs, &slide.GraphImage{
			OverlayImage: graph.OverlayImage,
			MaskImage:    graph.MaskImage,
			ShadowImage:  graph.ShadowImage,
		})
	}

	// set resources
	builder.SetResources(
		slide.WithGraphImages(newGraphs),
		slide.WithBackgrounds(imgs),
	)
	
	slideCapt = builder.Make()
}

func main() {
	captData, err := slideCapt.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/slide"
)

var slideCapt slide.Captcha

func init() {
  builder := slide.NewBuilder(
		//slide.WithGenGraphNumber(2),
		slide.WithEnableGraphVerticalRandom(true),
	)

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

	graphs, err := tiles.GetTiles()
	if err != nil {
		log.Fatalln(err)
	}

	var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
	for i := 0; i < len(graphs); i++ {
		graph := graphs[i]
		newGraphs = append(newGraphs, &slide.GraphImage{
			OverlayImage: graph.OverlayImage,
			MaskImage:    graph.MaskImage,
			ShadowImage:  graph.ShadowImage,
		})
	}

	// set resources
	builder.SetResources(
		slide.WithGraphImages(newGraphs),
		slide.WithBackgrounds(imgs),
	)
	
	slideCapt = builder.Make()
}

func main() {
	captData, err := slideCapt.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() 滑动式
  • builder.MakeWithRegion() 区域内拖拽滑动式

配置

TIP

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

主图配置

  • slide.WithImageSize(*option.Size) 设置主图尺寸,默认 300x220
  • slide.WithImageAlpha(float32) 设置主图透明度
  • slide.WithRangeGraphSize(val option.RangeVal) 设置图形随机尺寸范围
  • slide.WithEnableGraphVerticalRandom(val bool) 设置图形横向排序,"滑动式"使用

贴图

  • slide.WithGenGraphNumber(val int) 设置图形个数
  • slide.WithRangeDeadZoneDirections(val []DeadZoneDirectionType) 设置贴图盲区
  • slide.WithRangeGraphAnglePos([]option.RangeVal) 设置图形随机角度范围

设置资源

TIP

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

  • slide.WithBackgrounds([]image.Image) 设置主图背景
  • slide.WithGraphImages(images []*GraphImage) 设置贴图的图形

验证码数据

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

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

拖拽式预览