README ¶
Package captcha provides an easy to use, unopinionated API for captcha generation
Why another captcha generator?
I want a simple and framework-independent way to generate captcha. It also should be flexible, at least allow me to pick my favorite font.
install
go get github.com/steambap/captcha
usage
func handle(w http.ResponseWriter, r *http.Request) {
// create a captcha of 150x50px
data, _ := captcha.New(150, 50)
// session come from other library such as gorilla/sessions
session.Values["captcha"] = data.Text
session.Save(r, w)
// send image data to client
data.WriteImage(w)
}
sample image
Contributing
If your found a bug, please contribute! see contributing.md for more detail
License
Documentation ¶
Overview ¶
Package captcha provides an easy to use, unopinionated API for captcha generation
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadFontFromReader ¶ added in v1.0.0
LoadFontFromReader load an external font from an io.Reader interface.
Types ¶
type Data ¶
type Data struct { // Text is captcha solution Text string // contains filtered or unexported fields }
Data is the result of captcha generation. It has a `Text` field and a private `img` field that will be used in `WriteImage` receiver
func New ¶
New creates a new captcha. It returns captcha data and any freetype drawing error encountered
func NewMathExpr ¶ added in v0.11.0
NewMathExpr creates a new captcha. It will generate a image with a math expression like `1 + 2`
func (*Data) WriteGIF ¶ added in v1.1.0
WriteGIF encodes image data in GIF format and writes to an io.Writer. It returns possible error from GIF encoding.
func (*Data) WriteImage ¶ added in v1.0.0
WriteImage encodes image data and writes to an io.Writer. It returns possible error from PNG encoding.
type Options ¶
type Options struct { // BackgroundColor is captcha image's background color. // It defaults to color.Transparent. BackgroundColor color.Color // CharPreset decides what text will be on captcha image. // It defaults to digit 0-9 and all English alphabet. // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 CharPreset string // TextLength is the length of captcha text. // It defaults to 4. TextLength int // CurveNumber is the number of curves to draw on captcha image. // It defaults to 2. CurveNumber int // FontDPI controls DPI (dots per inch) of font. // The default is 72.0. FontDPI float64 // FontScale controls the scale of font. // The default is 1.0. FontScale float64 // contains filtered or unexported fields }
Options manage captcha generation details.