gocaptcha

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 18 Imported by: 115

README

gocaptcha

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

图片实例

image image image image

简介

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

实例

使用:
	go get github.com/lifei6671/gocaptcha
使用的类库
	go get github.com/golang/freetype
	go get github.com/golang/freetype/truetype
	go get golang.org/x/image
代码

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

	
func Get(w http.ResponseWriter, r *http.Request) {
	captchaImage := gocaptcha.New(dx, dy, gocaptcha.RandLightColor())
	err := captchaImage.
		DrawBorder(gocaptcha.RandDeepColor()).
		DrawNoise(gocaptcha.NoiseDensityHigh, gocaptcha.NewTextNoiseDrawer(gocaptcha.DefaultDPI)).
		DrawNoise(gocaptcha.NoiseDensityLower, gocaptcha.NewPointNoiseDrawer()).
		DrawLine(gocaptcha.NewBezier3DLine(), gocaptcha.RandDeepColor()).
		DrawText(gocaptcha.NewTwistTextDrawer(gocaptcha.DefaultDPI, gocaptcha.DefaultAmplitude, gocaptcha.DefaultFrequency), gocaptcha.RandText(4)).
		DrawLine(gocaptcha.NewBeeline(), gocaptcha.RandDeepColor()).
		//DrawLine(gocaptcha.NewHollowLine(), gocaptcha.RandLightColor()).
		DrawBlur(gocaptcha.NewGaussianBlur(), gocaptcha.DefaultBlurKernelSize, gocaptcha.DefaultBlurSigma).
		Error
	
	if err != nil {
		fmt.Println(err)
	}
	
	_ = captchaImage.Encode(w, gocaptcha.ImageFormatJpeg)
}

// 初始化字体
func init() {
	err := gocaptcha.SetFontPath("../fonts/")
	if err != nil {
		panic(err)
	}
}

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("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

Functions

func ColorToRGB

func ColorToRGB(colorVal int) color.RGBA

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

func RandColor added in v1.0.0

func RandColor() color.RGBA

RandColor 生成随机颜色.

func RandDeepColor added in v1.0.0

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 added in v1.0.0

func SetFontPath(fontDirPath string) error

SetFontPath sets the default font family from a directory

func SetFonts added in v1.0.0

func SetFonts(fonts ...string) error

SetFonts sets the default font family

Types

type BlurDrawer added in v1.0.0

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

func NewGaussianBlur added in v1.0.0

func NewGaussianBlur() BlurDrawer

type CaptchaImage

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

func New added in v0.2.0

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

New 新建一个图片对象

func (*CaptchaImage) DrawBlur added in v1.0.0

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 added in v1.0.0

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

Encode 编码图片

type FontFamily added in v1.0.0

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

FontFamily is a font family that creates a new font family

func NewFontFamily added in v1.0.0

func NewFontFamily() *FontFamily

NewFontFamily creates a new font family with the given fonts

func (*FontFamily) AddFont added in v1.0.0

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

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

func (*FontFamily) AddFontPath added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

LineDrawer 实现划线的接口

func NewBeeline added in v1.0.0

func NewBeeline() LineDrawer

func NewBezier3DLine added in v1.0.0

func NewBezier3DLine() LineDrawer

NewBezier3DLine 3D效果的贝塞尔曲线

func NewBezierLine added in v1.0.0

func NewBezierLine() LineDrawer

NewBezierLine 贝塞尔曲线

func NewCurveLine added in v1.0.0

func NewCurveLine() LineDrawer

NewCurveLine 基于正弦函数的曲线

func NewHollowLine added in v1.0.0

func NewHollowLine() LineDrawer

NewHollowLine 空心线

type NoiseDensity added in v1.0.0

type NoiseDensity int

NoiseDensity is the complexity of captcha

const (
	NoiseDensityLower NoiseDensity = iota
	NoiseDensityMedium
	NoiseDensityHigh
)

type NoiseDrawer added in v1.0.0

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 added in v1.0.0

func NewPointNoiseDrawer() NoiseDrawer

NewPointNoiseDrawer returns a NoiseDrawer that draws noise points

func NewTextNoiseDrawer added in v1.0.0

func NewTextNoiseDrawer(dpi float64) NoiseDrawer

type TextDrawer added in v1.0.0

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

TextDrawer is a text drawer interface.

func NewTextDrawer added in v1.0.0

func NewTextDrawer(dpi float64) TextDrawer

NewTextDrawer returns a new text drawer.

func NewTwistTextDrawer added in v1.0.0

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