captcha

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 11 Imported by: 0

README

Captcha

This module provides a simple captcha image generator.

Installation

go get github.com/cyberxnomad/captcha

Usage

example
package main

import (
	"fmt"
	"image/png"
	"os"

	"github.com/cyberxnomad/captcha"
)

func main() {
	c, err := captcha.New(
		// replace with your font file path
		captcha.WithFont("/path/to/font.ttf", 40),
		captcha.WithSize(150, 50),
		captcha.WithLength(4, 6),
		captcha.WithScale(0.8, 1.2),
		captcha.WithRotation(-30, 30),
		captcha.WithDistortion(1.2, 2),
		captcha.WithSpacing(0.7, 0.9),
		captcha.WithNoise(0.05),
		captcha.WithLines(4, 6),
	)
	if err != nil {
		panic(err)
	}

	f, err := os.Create("captcha.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	img, code := c.Generate()
	fmt.Println(code)

	err = png.Encode(f, img)
	if err != nil {
		panic(err)
	}
}
output

code: wqufPK

image: captcha

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Captcha

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

func New

func New(opts ...Option) (*Captcha, error)

func (*Captcha) Generate

func (c *Captcha) Generate() (image.Image, string)

Generate generates a captcha image and returns the image and the code

type CharSet

type CharSet = string
const (
	Lowercase                    CharSet = "abcdefghijklmnopqrstuvwxyz"
	Uppercase                    CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Alphabetic                   CharSet = Lowercase + Uppercase
	Numeric                      CharSet = "0123456789"
	Hex                          CharSet = Numeric + "abcdef"
	LowerNumeric                 CharSet = Lowercase + Numeric
	UpperNumeric                 CharSet = Uppercase + Numeric
	AlphaNumeric                 CharSet = Alphabetic + Numeric
	AlphaNumericWithoutConfusion CharSet = "ABCDEFGHKLMNPQRSTUVWXYZabcdefghkmnpqsuvwxyz23456789"
)

type Option

type Option func(*Captcha)

func WithBackground

func WithBackground(background color.Color) Option

WithBackground sets the background color of the captcha.

Default: color.White

func WithCharSet

func WithCharSet(charSet CharSet) Option

WithCharSet sets the character set used to generate the captcha.

Default: AlphaNumeric

func WithDistortion

func WithDistortion(minDistortion, maxDistortion float64) Option

WithDistortion sets the distortion factor of characters.

Default: 0.0, 0.0

func WithFont

func WithFont(path string, size float64) Option

WithFont sets the font used to generate the captcha.

Default: nil, 40

func WithForeground

func WithForeground(foreground color.Color) Option

WithForeground sets the foreground color of the captcha.

Default: color.Black

func WithLength

func WithLength(minLength, maxLength int) Option

WithLength sets the minimum and maximum length of the captcha.

Default: 4, 4

func WithLines

func WithLines(minLines, maxLines int) Option

WithLines sets the number of lines to draw on the captcha.

Default: 3, 7

func WithNoise

func WithNoise(level float64) Option

WithNoise sets the level of noise to add to the captcha.

Default: 0.1

func WithRotation

func WithRotation(minRotation, maxRotation float64) Option

WithRotation sets the rotation angle of characters.

Default: 0.0, 0.0

func WithScale

func WithScale(minScale, maxScale float64) Option

WithScale sets the scaling factor of characters.

Default: 1.0, 1.0

func WithSize

func WithSize(width, height int) Option

WithSize sets the size of the captcha image.

Default: 140x50

func WithSpacing

func WithSpacing(minSpacing, maxSpacing float64) Option

WithSpacing sets the spacing between characters.

Default: 1.0, 1.0

Jump to

Keyboard shortcuts

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