captcha

package module
v0.0.0-...-3bb9e60 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 10 Imported by: 0

README

golang实现的验证码 golang captcha

优点

  1. 使用简单
  2. 不依赖第三方图形库 直接go get 就Ok
  3. 丰富自定义设置(字体,多颜色,验证码大小,文字模式,文字数量,干扰强度)

demo

colorsamplecolor2

使用 Start using it

Download and install it:

go get github.com/afocus/captcha

必须设置font

最简单的示例 sample use
cap = captcha.New()
// 设置字体
cap.SetFont("comic.ttf")
// 创建验证码 4个字符 captcha.NUM 字符模式数字类型
// 返回验证码图像对象以及验证码字符串 后期可以对字符串进行对比 判断验证
img,str := cap.Create(4,captcha.NUM)
设置 set options
cap = captcha.New()
// 可以设置多个字体 或使用cap.AddFont("xx.ttf")追加
cap.SetFont("comic.ttf", "xxx.ttf")
// 设置验证码大小
cap.SetSize(128, 64)
// 设置干扰强度
cap.SetDisturbance(captcha.MEDIUM)
// 设置前景色 可以多个 随机替换文字颜色 默认黑色
cap.SetFrontColor(color.RGBA{255, 255, 255, 255})
// 设置背景色 可以多个 随机替换背景色 默认白色
cap.SetBkgColor(color.RGBA{255, 0, 0, 255}, color.RGBA{0, 0, 255, 255}, color.RGBA{0, 153, 0, 255})

img,str := cap.Create(4,captcha.NUM)
img1,str1 := cap.Create(6,captcha.ALL)
自定义字符串 custom captcha words
cap = captcha.New()
// 设置字体
cap.SetFont("comic.ttf")
img := cap.CreateCustom("hello")
网站中如果使用? how to use for web

look examples/main.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bilinear

type Bilinear struct{}

func (Bilinear) RGBA

func (Bilinear) RGBA(src *image.RGBA, x, y float64) color.RGBA

type BilinearSrc

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

type Captcha

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

func New

func New() *Captcha

func (*Captcha) AddFont

func (c *Captcha) AddFont(path string) error

Add a font

This function can be called multiple times

func (*Captcha) AddFontFromBytes

func (c *Captcha) AddFontFromBytes(contents []byte) error

AddFontFromBytes allows to load font from slice of bytes, for example, load the font packed by https://github.com/jteeuwen/go-bindata

func (*Captcha) Create

func (c *Captcha) Create(num int, t StrType) (*Image, string)

Generate a captcha

func (*Captcha) SetBkgColor

func (c *Captcha) SetBkgColor(colors ...color.Color)

Set the background color of the generated captchas

If more than one argument is given the color will be picked randomly among the alternatives each time a new captcha is generated

func (*Captcha) SetDisturbance

func (c *Captcha) SetDisturbance(d DisturLevel)

func (*Captcha) SetFrontColor

func (c *Captcha) SetFrontColor(colors ...color.Color)

func (*Captcha) SetSize

func (c *Captcha) SetSize(w, h int)

Set the size of the generated captchas

The minimum width is 48 and the minimum height is 20

type DisturLevel

type DisturLevel int
const (
	NORMAL DisturLevel = 4
	MEDIUM DisturLevel = 8
	HIGH   DisturLevel = 16
)

type Image

type Image struct {
	*image.RGBA
}

Image 图片

func NewImage

func NewImage(w, h int) *Image

NewImage 创建一个新的图片

func (*Image) DrawCircle

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

DrawCircle 画圆 xc,yc 圆心坐标 r 半径 fill是否填充颜色

func (*Image) DrawLine

func (img *Image) DrawLine(x1, y1, x2, y2 int, c color.Color)

DrawLine 画直线 Bresenham算法(https://zh.wikipedia.org/zh-cn/布雷森漢姆直線演算法) x1,y1 起点 x2,y2终点

func (*Image) DrawString

func (img *Image) DrawString(font *truetype.Font, c color.Color, str string, fontsize float64)

DrawString 写字

func (*Image) FillBkg

func (img *Image) FillBkg(c image.Image)

填充背景

func (*Image) Rotate

func (img *Image) Rotate(angle float64) image.Image

Rotate 旋转

type StrType

type StrType int
const (
	NUM   StrType = iota // 数字
	LOWER                // 小写字母
	UPPER                // 大写字母
	ALL                  // 全部
	CLEAR                // Remove some confusing characters
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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