captcha

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: MIT Imports: 24 Imported by: 0

README

Captcha

Mode 模式

  • String 字符串
  • Equation 算式 +,-,*

Disturb 干扰项

  • Point 点
  • Line 直线
  • Arc 弧线
  • Rotate 旋转
  • Distort 扭曲

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCodeExpired = CaptchaErr{ErrCode: 301001, /* contains filtered or unexported fields */}
	ErrCodeInvalid = CaptchaErr{ErrCode: 301002, /* contains filtered or unexported fields */}
)

Functions

func BoundString added in v0.1.1

func BoundString(content string, fontFace font.Face) (width, height int)

BoundString 根据内容、字体,返回图像大小

func Check

func Check(id, code string) (bool, error)

Check 验证验证码是否正确,返回错误类型

func CheckOk

func CheckOk(id, code string) bool

Check 验证验证码是否正确

func Create

func Create() (id string, result string, img image.Image)

Create 创建验证码,返回图片对象image.Image

func CreateB64

func CreateB64() (id string, result string, data string, err error)

CreateB64 创建验证码,返回图片base64

Example
package main

import (
	"fmt"

	"github.com/virzz/captcha"
)

func main() {
	id, result, data, err := captcha.CreateB64()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(id)
	fmt.Println(data)
	if captcha.CheckOk(id, "xxxx") {
		fmt.Println("test check")
	} else if captcha.CheckOk(id, result) {
		fmt.Println("ok")
	} else {
		fmt.Println("fail")
	}
}
Output:

func CreateBytes

func CreateBytes() (id string, result string, buf []byte, err error)

CreateBytes 创建验证码,返回图片数据[]byte

func Debug

func Debug()

func RandomBytes added in v0.1.1

func RandomBytes(n int) []byte

func RandomBytesHex added in v0.1.1

func RandomBytesHex(n int) string

func RandomCode added in v0.1.1

func RandomCode() string

func RandomString added in v0.1.1

func RandomString(n int) string

func SetCaptcha added in v0.1.1

func SetCaptcha(m Captcha)

Types

type Captcha added in v0.1.1

type Captcha interface {
	Draw() (image.Image, *CaptchaData) // 验证码生成逻辑并绘制图片
}

func DefaultCaptchaEquation added in v0.1.1

func DefaultCaptchaEquation() Captcha

func NewCaptchaEquation added in v0.1.1

func NewCaptchaEquation(bit int, opts ...Option) Captcha

type CaptchaBase added in v0.1.1

type CaptchaBase struct {
	// contains filtered or unexported fields
}

func New added in v0.1.1

func New(opts ...Option) *CaptchaBase

func (*CaptchaBase) Draw added in v0.1.1

func (c *CaptchaBase) Draw() (image.Image, *CaptchaData)

func (*CaptchaBase) Options added in v0.1.1

func (c *CaptchaBase) Options(opts ...Option) *CaptchaBase

type CaptchaData added in v0.1.1

type CaptchaData struct {
	Content string
	Result  string
	Expire  int64
}

type CaptchaEquation added in v0.1.1

type CaptchaEquation struct {
	*CaptchaBase
	// contains filtered or unexported fields
}

func (*CaptchaEquation) Draw added in v0.1.1

func (c *CaptchaEquation) Draw() (image.Image, *CaptchaData)

type CaptchaErr added in v0.1.1

type CaptchaErr struct {
	ErrCode int
	// contains filtered or unexported fields
}

func (CaptchaErr) Error added in v0.1.1

func (e CaptchaErr) Error() string

type Option added in v0.1.1

type Option func(cb *CaptchaBase)

func WithBackground added in v0.1.1

func WithBackground(color color.Color) Option

func WithExpire added in v0.1.1

func WithExpire(t int64) Option

func WithFont added in v0.1.1

func WithFont(fontFace font.Face, size float64) Option

func WithFontByte added in v0.1.1

func WithFontByte(buf []byte, fontSize float64, dpi ...float64) Option

func WithFontName added in v0.1.1

func WithFontName(fontName string, fontSize float64, dpi ...float64) Option

func WithFontSize added in v0.1.1

func WithFontSize(size float64) Option

func WithFront added in v0.1.1

func WithFront(color color.Color) Option

func WithLine added in v0.1.1

func WithLine(n int) Option

func WithPoint added in v0.1.1

func WithPoint(n int) Option

func WithSize added in v0.1.1

func WithSize(width, hight int) Option

Jump to

Keyboard shortcuts

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