captcha

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

captcha provides an simple, unopinionated API for captcha generation

goreference Go Report Card

Compatibility

This package uses embed package from Go 1.16.

Usage

import "github.com/pchchv/captcha"

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)
}

Sample image

image

documentation | example | font example

Documentation

Overview

Package provides an simple, unopinionated API for captcha generation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadFont

func LoadFont(fontData []byte) (err error)

LoadFont lets load an external font.

func LoadFontFromReader

func LoadFontFromReader(reader io.Reader) error

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

Types

type Captcha

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

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

func New

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

New creates a new captcha.

func NewCustomGenerator

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

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

func NewMathExpr

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

NewMathExpr creates a new captcha. Generates an image with a mathematical expression like `1 + 2`.

func (*Captcha) WriteGIF

func (c *Captcha) WriteGIF(w io.Writer, o *gif.Options) error

WriteGIF encodes the image data into GIF format and writes it to io.Writer. Returns an error possible when encoding GIF.

func (*Captcha) WriteImage

func (c *Captcha) WriteImage(w io.Writer) error

WriteImage encodes image data and writes it to io.Writer. Returns an error possible when encoding PNG.

func (*Captcha) WriteJPG

func (c *Captcha) WriteJPG(w io.Writer, o *jpeg.Options) error

WriteJPG encodes the image data into JPEG format and writes it to io.Writer. Returns an error possible when encoding JPEG.

type Options

type Options struct {
	// BackgroundColor is captcha image's background color.
	// By default it is color.Transparent.
	BackgroundColor color.Color
	// CharPreset defines the text on the captcha image.
	// By default:
	// ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
	CharPreset string
	// TextLength is the length of the captcha text.
	// By default it is 4.
	TextLength int
	// CurveNumber is the number of curves to draw on captcha image.
	// By default it is 2.
	CurveNumber int
	// FontDPI controls DPI (dots per inch) of font.
	// By default it is 72.0.
	FontDPI float64
	// FontScale controls the scale of font.
	// By default it is 1.0.
	FontScale float64
	// Noise controls the amount of noise to be drawn.
	// By default, a noise point is drawn every 28 pixels.
	// By default it 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.

type SetOption

type SetOption func(*Options)

SetOption is a function used to change the default settings.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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