/captcha

a lightweight and powerful package for generating captcha pictures

Primary LanguageGoMIT LicenseMIT

go version license version

Captcha

Captcha is a lightweight and powerful package for generating captcha pictures with defined keys.

Documentation

Install

go get github.com/ErfanMomeniii/captcha

Next, include it in your application:

import "github.com/ErfanMomeniii/captcha"

Quick Start

The following examples demonstrates how to generate captcha image with ideal width, height and font weight:

1. Numeric Captcha

package main

import (
	"github.com/ErfanMomeniii/captcha"
)

func main() {
	c := captcha.New(300, 400, 40)

	im, _ := c.Numeric(6)
	// im is a numeric captcha that has a numeric word of 
	// length six in it

	c.save("./", im)
	// it saves image (im) in the input path
}

2. Alphabetical Captcha

package main

import (
	"github.com/ErfanMomeniii/captcha"
)

func main() {
	c := captcha.New(300, 400, 40)

	im, _ := c.Alphabetical(6)
	// im is a Alphabetical captcha that has an alphabetical word of 
	// length six in it

	c.save("./", im)
	// it saves image (im) in the input path
}

3. Mixed Captcha

package main

import (
	"github.com/ErfanMomeniii/captcha"
)

func main() {
	c := captcha.New(300, 400, 40)

	im, _ := c.Alphabetical(6)
	// im is a Mixed captcha that has a mixed word of 
	// length six in it (combination of alphabets and numbers)

	_ = c.save("./", im)
	// it saves image (im) in the input path
}

⚠️ This package uses some defined templates for choosing random template from those(you can those see in this) that can customize by the following :

package main

import (
	"github.com/ErfanMomeniii/captcha"
)

func main() {
	captcha.Templates = []captcha.Template{
		{
			Background: "#ffffff",
			Color:      "#000000",
		},
	}
}

Usage

func New

func New(width int, height int, fontWeight float64) *Captcha

New creates a new instance of Captcha.

type Captcha

type Captcha struct {
	Width      int     // Width of the generated image
	Height     int     // Height of the generated image
	FontWeight float64 // FontWeight of captcha word
}

Captcha is an instantiation used for defining some information of the captcha image.

func (*captcha) Numeric

func (c *Captcha) Numeric(length int) (image.Image, error)

Numeric generates numeric captcha image with input length.

func (*captcha) Alphabetical

func (c *Captcha) Alphabetical(length int) (image.Image, error)

Alphabetical generates alphabetical captcha image with input length.

func (*captcha) Mixed

func (c *Captcha) Mixed(length int) (image.Image, error)

Mixed generates mixed (combination of alphabetical and numeric words) captcha image with input length.

func (*captcha) Save

func (c *Captcha) Save(path string, im image.Image) error

Save saves png image in the input path

type Template

type Template struct {
	Background string // Background color of captcha image
	Color      string // Color of the captcha word
}

Template is an instantiation that used for defining some templates for captcha image.

func RandTemplate

func RandTemplate() Template

RandTemplate generates a random template from the template array.

Examples

Here are several examples of generated captcha images by only using default templates.