captcha

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 10 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Templates = []Template{
	{
		Background: "#ffffff",
		Color:      "#8aaae5",
	}, {
		Background: "#fBf8Be",
		Color:      "#234e70",
	}, {
		Background: "#000000",
		Color:      "#f3ca20",
	}, {
		Background: "d9a5b3",
		Color:      "#1868ae",
	}, {
		Background: "fff1e1",
		Color:      "1d3c45",
	}, {
		Background: "#ced7d8",
		Color:      "#f47a60",
	}, {
		Background: "#ffffff",
		Color:      "#cc9999",
	},
}

Functions

This section is empty.

Types

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 New

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

New creates a new instance of Captcha.

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

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

Numeric generates numeric 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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