gocaptcha

package module
v0.0.0-...-76f9998 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 20 Imported by: 1

README

gocaptcha

一个简单的Go语言实现的验证码

图片实例

image image image image

简介

基于Golang实现的图片验证码生成库,可以实现随机字母个数,随机直线,随机噪点等。可以设置任意多字体,每个验证码随机选一种字体展示。

实例

使用:
	go get github.com/23233/gocaptcha
使用的类库

内置了字体与难度调节

	go get github.com/golang/freetype
	go get github.com/golang/freetype/truetype
	go get golang.org/x/image
代码

具体实例可以查看example目录,有生成的验证码图片。

Documentation

Index

Constants

View Source
const (
	// DefaultDPI 默认的dpi
	DefaultDPI = 72.0
	// DefaultBlurKernelSize 默认模糊卷积核大小
	DefaultBlurKernelSize = 2
	// DefaultBlurSigma 默认模糊sigma值
	DefaultBlurSigma = 0.65
	// DefaultAmplitude 默认图片扭曲的振幅
	DefaultAmplitude = 20
	//DefaultFrequency 默认图片扭曲的波频率
	DefaultFrequency = 0.05
)

Variables

View Source
var (
	ErrNilCanvas = errors.New("canvas is nil")
	ErrNilText   = errors.New("text is nil")
)
View Source
var DefaultFontFamily = NewFontFamily()
View Source
var ErrNoFontsInFamily = os.ErrNotExist
View Source
var TextCharacters = []rune("ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789")

Functions

func ColorToRGB

func ColorToRGB(colorVal int) color.RGBA

ColorToRGB 颜色代码转换为RGB input int output int red, green, blue.

func GenerateCaptcha

func GenerateCaptcha(width, height int, textLength int, difficulty CaptchaDifficulty) (text string, imgBytes []byte, err error)

GenerateCaptcha 生成验证码图片和对应的文本

func RandColor

func RandColor() color.RGBA

RandColor 生成随机颜色.

func RandDeepColor

func RandDeepColor() color.RGBA

RandDeepColor 随机生成深色系.

func RandLightColor

func RandLightColor() color.RGBA

RandLightColor 随机生成浅色.

func RandText

func RandText(num int) string

RandText 生成随机字体.

func Random

func Random(min int64, max int64) float64

Random 生成指定大小的随机数.

func SetFontPath

func SetFontPath(fontDirPath string) error

SetFontPath sets the default font family from a directory

func SetFonts

func SetFonts(fonts ...string) error

SetFonts sets the default font family

Types

type BlurDrawer

type BlurDrawer interface {
	DrawBlur(canvas draw.Image, kernelSize int, sigma float64) error
}

func NewGaussianBlur

func NewGaussianBlur() BlurDrawer

type CaptchaDifficulty

type CaptchaDifficulty int

CaptchaDifficulty 验证码难度级别

const (
	// CaptchaVeryEasy 非常简单难度 - 清晰文字,无噪点,无扭曲
	CaptchaVeryEasy CaptchaDifficulty = iota
	// CaptchaEasy 简单难度 - 轻微扭曲,少量噪点
	CaptchaEasy
	// CaptchaMedium 中等难度 - 原来的简单模式
	CaptchaMedium
	// CaptchaHard 困难难度 - 原来的困难模式
	CaptchaHard
)

type CaptchaImage

type CaptchaImage struct {
	Complex int
	Error   error
	// contains filtered or unexported fields
}

func New

func New(width int, height int, bgColor color.RGBA) *CaptchaImage

New 新建一个图片对象

func (*CaptchaImage) DrawBlur

func (captcha *CaptchaImage) DrawBlur(drawer BlurDrawer, kernelSize int, sigma float64) *CaptchaImage

DrawBlur 对图片进行模糊处理

func (*CaptchaImage) DrawBorder

func (captcha *CaptchaImage) DrawBorder(borderColor color.RGBA) *CaptchaImage

DrawBorder 画边框.

func (*CaptchaImage) DrawLine

func (captcha *CaptchaImage) DrawLine(drawer LineDrawer, lineColor color.Color) *CaptchaImage

DrawLine 画直线.

func (*CaptchaImage) DrawNoise

func (captcha *CaptchaImage) DrawNoise(complex NoiseDensity, noiseDrawer NoiseDrawer) *CaptchaImage

DrawNoise 画噪点.

func (*CaptchaImage) DrawText

func (captcha *CaptchaImage) DrawText(textDrawer TextDrawer, text string) *CaptchaImage

DrawText 写字.

func (*CaptchaImage) Encode

func (captcha *CaptchaImage) Encode(w io.Writer, imageFormat ImageFormat) error

Encode 编码图片

type FontFamily

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

FontFamily is a font family that creates a new font family

func NewFontFamily

func NewFontFamily() *FontFamily

NewFontFamily creates a new font family with the embedded fonts

func (*FontFamily) AddFont

func (f *FontFamily) AddFont(fontFile string) error

AddFont adds a font to the family and returns an error if it fails

func (*FontFamily) AddFontPath

func (f *FontFamily) AddFontPath(dirPath string) error

AddFontPath adds all .ttf files from the given directory to the font family and returns an error if any

func (*FontFamily) Random

func (f *FontFamily) Random() (*truetype.Font, error)

Random returns a random font from the family

type ImageFormat

type ImageFormat int

ImageFormat 图片格式

const (
	ImageFormatPng ImageFormat = iota
	ImageFormatJpeg
	ImageFormatGif
)

type LineDrawer

type LineDrawer interface {
	DrawLine(canvas draw.Image, x image.Point, y image.Point, color color.Color) error
}

LineDrawer 实现划线的接口

func NewBeeline

func NewBeeline() LineDrawer

func NewBezier3DLine

func NewBezier3DLine() LineDrawer

NewBezier3DLine 3D效果的贝塞尔曲线

func NewBezierLine

func NewBezierLine() LineDrawer

NewBezierLine 贝塞尔曲线

func NewCurveLine

func NewCurveLine() LineDrawer

NewCurveLine 基于正弦函数的曲线

func NewHollowLine

func NewHollowLine() LineDrawer

NewHollowLine 空心线

type NoiseDensity

type NoiseDensity int

NoiseDensity is the complexity of captcha

const (
	NoiseDensityLower NoiseDensity = iota
	NoiseDensityMedium
	NoiseDensityHigh
)

type NoiseDrawer

type NoiseDrawer interface {
	// DrawNoise draws noise on the image
	DrawNoise(img draw.Image, density NoiseDensity) error
}

NoiseDrawer is a type that can make noise on an image

func NewPointNoiseDrawer

func NewPointNoiseDrawer() NoiseDrawer

NewPointNoiseDrawer returns a NoiseDrawer that draws noise points

func NewTextNoiseDrawer

func NewTextNoiseDrawer(dpi float64) NoiseDrawer

type TextDrawer

type TextDrawer interface {
	DrawString(canvas draw.Image, text string) error
}

TextDrawer is a text drawer interface.

func NewTextDrawer

func NewTextDrawer(dpi float64) TextDrawer

NewTextDrawer returns a new text drawer.

func NewTwistTextDrawer

func NewTwistTextDrawer(dpi float64, amplitude float64, frequency float64) TextDrawer

NewTwistTextDrawer returns a new text drawer with twist effect.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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