captcha

package module
v0.0.0-...-4ae261e Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 16 Imported by: 2

README

本分支主要解决:

1.字母超出边界问题

2.公开img变量,以便保存成任何格式

3.增加保存jpg、png、gif文件函数

Package captcha provides an easy to use, unopinionated API for captcha generation.

PkgGoDev Build Status codecov Go Report Card

Why another captcha generator?

I want a simple and framework-independent way to generate captcha. It also should be flexible, at least allow me to pick my favorite font.

install

go get github.com/mzky/captcha

usage

func handle(w http.ResponseWriter, r *http.Request) {
	// create a captcha of 150x50px
	data, _ := captcha.New(150, 50)

	// session come from other library such as gorilla/sessions
	session.Values["captcha"] = data.Text
	session.Save(r, w)
	// send image data to client
	data.WriteImage(w)
}

documentation | example | font example

sample image

image

image

Compatibility

This package uses embed package from Go 1.16. If for some reasons you have to use pre 1.16 version of Go, reference pre 1.4 version of this module in your go.mod.

Contributing

If your found a bug, please contribute! see contributing.md for more detail.

License

MIT

Documentation

Overview

Package captcha provides an easy to use, unopinionated API for captcha generation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadFont

func LoadFont(fontData []byte) error

LoadFont let you load an external font.

func LoadFontFromReader

func LoadFontFromReader(reader io.Reader) error

LoadFontFromReader load an external font from an io.Reader interface.

Types

type Data

type Data struct {
	// Text is captcha solution.
	Text string
	Img  *image.NRGBA
}

Data is the result of captcha generation. It has a `Text` field and a private `img` field that will be used in `WriteImage` receiver.

func New

func New(width int, height int, option ...SetOption) (*Data, error)

New creates a new captcha. It returns captcha data and any freetype drawing error encountered.

func NewCustomGenerator

func NewCustomGenerator(
	width int, height int, generator func() (anwser string, question string), option ...SetOption,
) (*Data, error)

NewCustomGenerator creates a new captcha based on a custom text generator.

func NewMathExpr

func NewMathExpr(width int, height int, option ...SetOption) (*Data, error)

NewMathExpr creates a new captcha. It will generate a image with a math expression like `1 + 2`.

func (*Data) WriteGIF

func (data *Data) WriteGIF(w io.Writer, o *gif.Options) error

WriteGIF encodes image data in GIF format and writes to an io.Writer. It returns possible error from GIF encoding.

func (*Data) WriteGIFFile

func (data *Data) WriteGIFFile(fp string) error

将图像保存为GIF文件

func (*Data) WriteImage

func (data *Data) WriteImage(w io.Writer) error

WriteImage encodes image data and writes to an io.Writer. It returns possible error from PNG encoding.

func (*Data) WriteJPG

func (data *Data) WriteJPG(w io.Writer, o *jpeg.Options) error

WriteJPG encodes image data in JPEG format and writes to an io.Writer. It returns possible error from JPEG encoding.

func (*Data) WriteJPGFile

func (data *Data) WriteJPGFile(fp string) error

将图像保存为JPG文件

func (*Data) WritePNGFile

func (data *Data) WritePNGFile(fp string) error

将图像保存为PNG文件

type Options

type Options struct {
	// BackgroundColor is captcha image's background color.
	// It defaults to color.Transparent.
	BackgroundColor color.Color
	// CharPreset decides what text will be on captcha image.
	// It defaults to digit 0-9 and all English alphabet.
	// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
	CharPreset string
	// TextLength is the length of captcha text.
	// It defaults to 4.
	TextLength int
	// CurveNumber is the number of curves to draw on captcha image.
	// It defaults to 2.
	CurveNumber int
	// FontDPI controls DPI (dots per inch) of font.
	// The default is 72.0.
	FontDPI float64
	// FontScale controls the scale of font.
	// The default is 1.0.
	FontScale float64
	// Noise controls the number of noise drawn.
	// A noise dot is drawn for every 28 pixel by default.
	// The default is 1.0.
	Noise float64
	// Palette is the set of colors to chose from
	Palette color.Palette
	// contains filtered or unexported fields
}

Options manage captcha generation details.

type SetOption

type SetOption func(*Options)

SetOption is a function that can be used to modify default options.

Directories

Path Synopsis
example
basic Module
load-font Module

Jump to

Keyboard shortcuts

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