captcha

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 15 Imported by: 0

README

go-captcha

golang 实现普通验证码,同时支持中文汉字点击行为验证码

  • 英文数字验证
  • 中文点选验证
  • 干扰线
  • 干扰点
  • 自定义背景颜色
  • 自定义字体颜色
  • 自定义倾斜角度
  • 自定义字体大小
  • 自定义字体

Preview

Quick Start 使用

普通验证码

import (
   "github.com/feiin/go-captcha"
)

cpt := captcha.NewCaptcha(260, 100)

//cpt.SetFontColors(...)
//cpt.SetFont(...)
//cpt.SetFontSize(...)
//cpt.SetSytle(...)
//cpt.SetBackgroundColor(...)


//获取4位随机验证码
result,err := cpt.GenRandomNormalCaptcha(4)

//result 属性
//    - DrawRects   []DrawRect 随机验证码位置
//	  - Text        string     随机验证码字符
//	  - ImageBase64 string     随机验证码base64编码

中文点击行为验证码

验证码输出展示 -> 上传用户行为 -> 验证点击行为数据


import (
   "github.com/feiin/go-captcha"
)

//........................................



cpt := captcha.NewCaptcha(260, 100)
cpt.SetBackgroundColor(color.RGBA{R: uint8(20), G: uint8(8), B: uint8(100), A: uint8(255)})
cpt.Config.Style = captcha.CaptchaStyle_Behavior
cpt.Config.MaxRotate = 20
result, err := cpt.GenBehaviorCNCaptcha()

//可以将result或result.DrawRects信息者结果存入redis或程序内存中等,当上传用户点击行为时,做位置信息校验

//当用户点击验证后,上传点击[]Point位置信息,后端校验
if captcha.ValidBehaviorCaptcha(result,points) {
    //验证通过
} else {
    //验证未过
}

//rects = result.DrawRects
//if captcha.ValidBehaviorRects(rects,points) {
    ////验证通过
//} else {
    ////验证未过
//}

Config属性

type Config struct {
	FontColors     []color.Color
	BackgroupColor color.Color //default white
	FontSize       int         //default 56
	MaxRotate      int         //default 30
	Style          CaptchaStyle
	Font           *truetype.Font //字体
}
  • FontColors 字体颜色,多个时随机字体颜色
  • BackgroupColor 背景颜色
  • FontSize 字体大小
  • MaxRotate 最大旋转角度
  • Style CaptchaStyle_Normal普通验证码,CaptchaStyle_Behavior点击行为验证码

Config设置方法


cpt := NewCaptcha(260, 100)
cpt.Config.FontSize = 24 //等同cpt.SetFontSize(24)
//... 


LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Alphabets = "abcdefghijklmnopqrstuvwxyxABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
View Source
var CNChars = []string{
	"热情",
	"帮助",
	"聪明",
	"活泼",
	"笑容",
	"风景",
	"眼睛",
	"太阳",
	"棉花",
	"落叶",
	"翅膀",
	"松果",
	"表情",
	"船只",
	"灯笼",
	"天气",
	"时光",
	"鲜花",
	"火车",
	"街道",
	"问题",
	"奇怪",
	"欢乐",
	"端正",
	"大方",
	"清楚",
	"欢喜",
	"完整",
	"高兴",
	"漂亮",
	"干净",
	"仔细",
	"完全",
	"前后",
	"马虎",
	"冷清",
	"普通",
	"高贵",
	"优雅",
	"热闹",
	"云彩",
	"同学",
	"黑板",
	"篮球",
	"足球",
	"城市",
	"组合",
	"玩具",
	"表格",
	"希望",
	"和平",
	"同意",
	"目标",
	"工作",
	"学习",
	"顺利",
	"谈话",
	"风格",
	"如果",
	"天气",
	"严格",
	"教育",
	"讨论",
	"明亮",
	"相信",
	"犹豫",
	"坚定",
	"缓慢",
	"勇敢",
	"提前",
	"杰出",
	"安全",
	"宽容",
}

Functions

func PointInRect

func PointInRect(p Point, rect DrawRect) bool

PointInRect 验证点是否在矩形中

func ValidBehaviorCaptcha

func ValidBehaviorCaptcha(cr CaptchaResult, userPoints []Point) bool

ValidBehaviorCaptcha 验证行为点击验证码是否正确

func ValidBehaviorRects

func ValidBehaviorRects(rects []DrawRect, userPoints []Point) bool

ValidBehaviorRects 验证行为点是否在验证码矩形中

Types

type AreaPoint

type AreaPoint struct {
	MinX, MaxX, MinY, MaxY int
}

type Canvas

type Canvas struct {
	*image.NRGBA
	Width  int
	Height int
	Config Config
}

func (*Canvas) DrawBackgroud

func (c *Canvas) DrawBackgroud()

DrawBackgroud 设置画板背景

func (*Canvas) DrawBezierLines

func (c *Canvas) DrawBezierLines(lineCount int)

DrawBezierLines 画贝塞尔干扰线

func (*Canvas) DrawCircle

func (c *Canvas) DrawCircle(xc, yc, r int, fill bool, cc color.Color)

DrawCircle 画圆

func (*Canvas) DrawCircles

func (c *Canvas) DrawCircles(circleCount int)

DrawCircles 随机产生circleCount干扰点

func (*Canvas) DrawFont

func (c *Canvas) DrawFont(fontText string, fontColors []color.Color) (*Palette, *AreaPoint)

DrawFont 绘制验证码字

func (*Canvas) DrawLine

func (c *Canvas) DrawLine(p1, p2 Point, color color.Color)

DrawLine 画干扰线

func (*Canvas) DrawLines

func (c *Canvas) DrawLines(lineCount int)

func (*Canvas) DrawString

func (c *Canvas) DrawString(text string) []DrawRect

DrawString 绘制文字验证码

type Captcha

type Captcha struct {
	Width  int
	Height int
	Config Config
}

func NewCaptcha

func NewCaptcha(width, height int) *Captcha

func (*Captcha) GenBehaviorCNCaptcha

func (cp *Captcha) GenBehaviorCNCaptcha() (CaptchaResult, error)

GenBehaviorCaptcha 生成中文点击验证码 - 点击行为验证码

func (*Captcha) GenCaptchaImage

func (cp *Captcha) GenCaptchaImage(text string) (CaptchaResult, error)

GenCaptchaImage 生成文字的验证码图片

func (*Captcha) GenRandomNormalCaptcha

func (cp *Captcha) GenRandomNormalCaptcha(length int) (CaptchaResult, error)

GenNormalRandomCaptcha 随机验证码 - 普通验证码

func (*Captcha) SetBackgroundColor

func (cp *Captcha) SetBackgroundColor(color color.Color)

SetBackgroundColor 设置背景颜色

func (*Captcha) SetFont

func (cp *Captcha) SetFont(font *truetype.Font)

SetFont 设置字体颜色

func (*Captcha) SetFontColors

func (cp *Captcha) SetFontColors(colors ...color.Color)

SetFontColors 设置字体颜色

func (*Captcha) SetFontSize

func (cp *Captcha) SetFontSize(fontSize int)

SetFontSize 字体大小

func (*Captcha) SetSytle

func (cp *Captcha) SetSytle(style CaptchaStyle)

SetFontSize 字体大小

type CaptchaResult

type CaptchaResult struct {
	DrawRects   []DrawRect `json:"draw_rect"`
	Text        string     `json:"text"`
	ImageBase64 string     `json:"image_base64"`
}

type CaptchaStyle

type CaptchaStyle int32
const (
	CaptchaStyle_Normal   CaptchaStyle = 0
	CaptchaStyle_Behavior CaptchaStyle = 1
)

type Config

type Config struct {
	FontColors     []color.Color
	BackgroupColor color.Color //default white
	FontSize       int         //default 56
	MaxRotate      int         //default 30
	Style          CaptchaStyle
	Font           *truetype.Font //字体
}

type DrawRect

type DrawRect struct {
	X      int `json:"x"`
	Y      int `json:"y"`
	Width  int `json:"width"`
	Height int `json:"height"`
}

type Palette

type Palette struct {
	*image.Paletted
}

func NewPalette

func NewPalette(r image.Rectangle, p color.Palette) *Palette

func (*Palette) Rotate

func (p *Palette) Rotate(angle int)

Rotate 旋转角度

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

func NewPoint

func NewPoint(x int, y int) *Point

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL