captchy

package module
v0.0.0-...-5555e94 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2019 License: MIT Imports: 26 Imported by: 0

README

Package captchy

build Go Report Card GoDoc

import "github.com/yujiahaol68/captchy"

captchy implements a abundant CAPTCHAs image generation through flexible config

Split and rotate subImage to against OCRs

Support custom TTF font file or Go builtin fonts

Support export to Base64, PNG, JPEG image

Only std lib so it is clean

API is still not stable. Not recommend for using in production env !

Preview

Generate from very simple to very complex captcha especially for machine, but still friendly to human

simple-captcha-png

complex-captcha-jpg

complex-captcha-png

Usage

Initialize resource
// Load fonts and setup default config
captchy.New(captchy.Default())
Gen solution
rbs := captchy.RandomString()
// Then hash and save rbs into session by yourself
// ..
Gen Image
encoder := captchy.GenerateImg(rbs)
// encoder can encode image into io.Writer, and write it anywhere you prefer.
// It can be local file writer or http response writer
// ..

Reference

DESIGNING CAPTCHA ALGORITHM: SPLITTING AND ROTATING THE IMAGES AGAINST OCRs

Documentation

Index

Constants

View Source
const (
	DefaultSize float64 = 48
	DefaultDPI  float64 = 72

	RegularFont = string(iota)
	MonoFont
	BoldFont
	MediumFont
	ItalicFont
)

Variables

View Source
var (
	RulerColor  = ColorInHex("0x456D7C")
	GridColor   = ColorInHex("0xB9B6B6")
	CircleColor = ColorInHex("#728C7F")
)

Functions

func ColorInHex

func ColorInHex(hex string) color.Color

ColorInHex is a warpper helper to get color by Hex. Support Hex color format like "0xF8AA5D" or "#728C7F"

func DisableCircle

func DisableCircle()

DisableCircle can disable drawing circle

func DisableRotate

func DisableRotate()

func DisableRuler

func DisableRuler()

DisableRuler will make drawer not to draw ruler

func New

func New(cfg *Config)

New load config and initialize all resource

func RandomString

func RandomString() []byte

RandomString is a goroutine safe string generator

func SetBg

func SetBg(c color.Color)

SetBg can set background color

func SetFontDeviation

func SetFontDeviation(d int)

func SetNoiseThreshold

func SetNoiseThreshold(p float64)

func SetRotateDegree

func SetRotateDegree(d float64)

SetRotateDegree restricts sub image rotate in range [-d, d], d should not greater than 8

func SetRotateDivideMat

func SetRotateDivideMat(dx, dy int)

SetRotateDivideMat set the image divide matrix [y, x], default div matrix x=0, y=0 is [2, 4]

func SetRulerDeviation

func SetRulerDeviation(d int)

Types

type CaptchaEncoder

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

CaptchaEncoder encode image.Image into common pic format

func (*CaptchaEncoder) ToBase64

func (ce *CaptchaEncoder) ToBase64(w io.Writer) error

func (*CaptchaEncoder) ToJPEG

func (ce *CaptchaEncoder) ToJPEG(w io.Writer, opt *jpeg.Options) error

func (*CaptchaEncoder) ToPNG

func (ce *CaptchaEncoder) ToPNG(w io.Writer) error

type Config

type Config struct {
	ImgW int
	ImgH int
	Len  int

	FontSize    float64
	FontColors  []color.Color
	BgColor     color.Color
	NoiseColors []color.Color
	RotateMatX  int
	RotateMatY  int
	// contains filtered or unexported fields
}

Config stores config of captcha image

func Default

func Default() *Config

Default return a basic captchy config

func (*Config) SetFont

func (c *Config) SetFont(path string)

type Encoder

type Encoder interface {
	ToPNG(io.Writer) error
	ToBase64(io.Writer) error
	ToJPEG(io.Writer, *jpeg.Options) error
}

func GenerateImg

func GenerateImg(t []byte) Encoder

GenerateImg gen image data according to random string

Jump to

Keyboard shortcuts

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