Captcha is a lightweight and powerful package for generating captcha pictures with defined keys.
go get github.com/ErfanMomeniii/captchaNext, include it in your application:
import "github.com/ErfanMomeniii/captcha"The following examples demonstrates how to generate captcha image with ideal width, height and font weight:
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
}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
}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
}package main
import (
"github.com/ErfanMomeniii/captcha"
)
func main() {
captcha.Templates = []captcha.Template{
{
Background: "#ffffff",
Color: "#000000",
},
}
}func New(width int, height int, fontWeight float64) *CaptchaNew creates a new instance of 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 (c *Captcha) Numeric(length int) (image.Image, error)Numeric generates numeric captcha image with input length.
func (c *Captcha) Alphabetical(length int) (image.Image, error)Alphabetical generates alphabetical captcha image with input length.
func (c *Captcha) Mixed(length int) (image.Image, error)Mixed generates mixed (combination of alphabetical and numeric words) captcha image with input length.
func (c *Captcha) Save(path string, im image.Image) errorSave saves png image in the input path
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() TemplateRandTemplate generates a random template from the template array.
Here are several examples of generated captcha images by only using default templates.












