gcaptcha

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

golang实现的验证码 golang captcha

优点

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

使用 Start using it

Download and install it:

go get github.com/snail007/gmc/util/captcha
最简单的示例 sample use
cap = gcaptcha.NewDefault()
// 创建验证码 4个字符 captcha.NUM 字符模式数字类型
// 返回验证码图像对象以及验证码字符串 后期可以对字符串进行对比 判断验证
img,str := cap.Create(4,captcha.NUM)
设置 set options
cap := gcaptcha.New()
// 可以设置多个字体 或使用cap.AddFont("xx.ttf")追加
cap.SetFont("comic.ttf", "xxx.ttf") //必须设置font
// 设置验证码大小
cap.SetSize(128, 64)
// 设置干扰强度
cap.SetDisturbance(gcaptcha.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,gcaptcha.NUM)
img1,str1 := cap.Create(6,gcaptcha.ALL)
自定义字符串 custom captcha words
cap = gcaptcha.New()
// 设置字体
cap.SetFont("comic.ttf")
img := cap.CreateCustom("hello")
网页中使用? how to use for web

read: 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 NewDefault

func NewDefault() *Captcha

func (*Captcha) AddFont

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

AddFont 添加一个字体

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)

Create 生成一个验证码图片

func (*Captcha) CreateCustom

func (c *Captcha) CreateCustom(str string) *Image

func (*Captcha) LoadDefaultFonts

func (c *Captcha) LoadDefaultFonts()

func (*Captcha) SetBkgColor

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

func (*Captcha) SetDisturbance

func (c *Captcha) SetDisturbance(d DisturLevel)

func (*Captcha) SetFont

func (c *Captcha) SetFont(paths ...string) error

SetFont 设置字体 可以设置多个

func (*Captcha) SetFrontColor

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

func (*Captcha) SetSize

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

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)

FillBkg 填充背景

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                // 去除部分易混淆的字符
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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