2017-10-05 15:00:16 +01:00
|
|
|
// Package captcha provides an easy to use, unopinionated API for captcha generation
|
2017-09-16 11:04:28 +01:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
import (
|
2017-10-10 04:05:46 +01:00
|
|
|
"bytes"
|
2017-09-16 11:04:28 +01:00
|
|
|
"image"
|
2017-09-17 07:50:28 +01:00
|
|
|
"image/color"
|
2017-09-19 03:10:02 +01:00
|
|
|
"image/draw"
|
2017-11-08 06:21:40 +00:00
|
|
|
"image/gif"
|
|
|
|
"image/jpeg"
|
2017-09-16 11:04:28 +01:00
|
|
|
"image/png"
|
2017-09-17 07:50:28 +01:00
|
|
|
"io"
|
2017-09-19 03:10:02 +01:00
|
|
|
"math"
|
2017-09-16 11:04:28 +01:00
|
|
|
"math/rand"
|
2017-09-27 06:07:08 +01:00
|
|
|
"strconv"
|
2017-09-16 11:04:28 +01:00
|
|
|
"time"
|
2017-12-11 08:12:44 +00:00
|
|
|
|
|
|
|
"github.com/golang/freetype"
|
|
|
|
"github.com/golang/freetype/truetype"
|
|
|
|
"golang.org/x/image/font"
|
2017-09-16 11:04:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
|
2017-09-19 03:10:02 +01:00
|
|
|
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
var ttfFont *truetype.Font
|
|
|
|
|
2017-09-19 04:15:14 +01:00
|
|
|
// Options manage captcha generation details.
|
2017-09-16 11:04:28 +01:00
|
|
|
type Options struct {
|
2017-09-19 04:15:14 +01:00
|
|
|
// BackgroundColor is captcha image's background color.
|
|
|
|
// It defaults to color.Transparent.
|
2017-09-19 03:10:02 +01:00
|
|
|
BackgroundColor color.Color
|
2017-09-19 04:15:14 +01:00
|
|
|
// 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.
|
2017-09-19 04:52:25 +01:00
|
|
|
// It defaults to 2.
|
2017-09-19 04:15:14 +01:00
|
|
|
CurveNumber int
|
2017-10-08 03:25:22 +01:00
|
|
|
// FontDPI controls DPI (dots per inch) of font.
|
|
|
|
// The default is 72.0.
|
2017-10-07 09:13:12 +01:00
|
|
|
FontDPI float64
|
2017-10-08 03:25:22 +01:00
|
|
|
// FontScale controls the scale of font.
|
|
|
|
// The default is 1.0.
|
|
|
|
FontScale float64
|
2017-12-11 08:12:44 +00:00
|
|
|
// Noise controls the number of noise drawn.
|
|
|
|
// A noise dot is drawn for every 28 pixel by default.
|
|
|
|
// The default is 1.0.
|
|
|
|
Noise float64
|
2018-08-14 07:27:29 +01:00
|
|
|
// Palette is the set of colors to chose from
|
|
|
|
Palette color.Palette
|
2017-09-19 04:15:14 +01:00
|
|
|
|
2017-09-19 06:12:03 +01:00
|
|
|
width int
|
|
|
|
height int
|
2017-09-16 11:04:28 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 07:50:28 +01:00
|
|
|
func newDefaultOption(width, height int) *Options {
|
2017-09-16 11:04:28 +01:00
|
|
|
return &Options{
|
2017-09-19 03:10:02 +01:00
|
|
|
BackgroundColor: color.Transparent,
|
|
|
|
CharPreset: charPreset,
|
2017-09-19 04:15:14 +01:00
|
|
|
TextLength: 4,
|
2017-09-19 04:52:25 +01:00
|
|
|
CurveNumber: 2,
|
2017-10-08 03:25:22 +01:00
|
|
|
FontDPI: 72.0,
|
|
|
|
FontScale: 1.0,
|
2017-12-11 08:12:44 +00:00
|
|
|
Noise: 1.0,
|
2018-08-14 07:27:29 +01:00
|
|
|
Palette: []color.Color{},
|
2017-09-19 03:10:02 +01:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2017-09-16 11:04:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 04:15:14 +01:00
|
|
|
// SetOption is a function that can be used to modify default options.
|
2017-09-19 03:10:02 +01:00
|
|
|
type SetOption func(*Options)
|
2017-09-16 11:04:28 +01:00
|
|
|
|
2017-09-19 04:15:14 +01:00
|
|
|
// Data is the result of captcha generation.
|
|
|
|
// It has a `Text` field and a private `img` field that will
|
2018-01-16 04:58:32 +00:00
|
|
|
// be used in `WriteImage` receiver.
|
2017-09-16 11:04:28 +01:00
|
|
|
type Data struct {
|
2018-01-16 04:58:32 +00:00
|
|
|
// Text is captcha solution.
|
2017-09-16 11:04:28 +01:00
|
|
|
Text string
|
|
|
|
|
|
|
|
img *image.NRGBA
|
|
|
|
}
|
|
|
|
|
2017-10-09 07:58:28 +01:00
|
|
|
// WriteImage encodes image data and writes to an io.Writer.
|
2017-11-08 06:21:40 +00:00
|
|
|
// It returns possible error from PNG encoding.
|
2017-10-09 07:58:28 +01:00
|
|
|
func (data *Data) WriteImage(w io.Writer) error {
|
2017-09-16 11:04:28 +01:00
|
|
|
return png.Encode(w, data.img)
|
|
|
|
}
|
|
|
|
|
2017-10-30 06:17:27 +00:00
|
|
|
// WriteJPG encodes image data in JPEG format and writes to an io.Writer.
|
2017-11-08 06:21:40 +00:00
|
|
|
// It returns possible error from JPEG encoding.
|
2017-10-30 06:17:27 +00:00
|
|
|
func (data *Data) WriteJPG(w io.Writer, o *jpeg.Options) error {
|
|
|
|
return jpeg.Encode(w, data.img, o)
|
|
|
|
}
|
|
|
|
|
2017-11-08 06:21:40 +00:00
|
|
|
// WriteGIF encodes image data in GIF format and writes to an io.Writer.
|
|
|
|
// It returns possible error from GIF encoding.
|
|
|
|
func (data *Data) WriteGIF(w io.Writer, o *gif.Options) error {
|
|
|
|
return gif.Encode(w, data.img, o)
|
|
|
|
}
|
|
|
|
|
2017-09-19 03:10:02 +01:00
|
|
|
func init() {
|
2017-09-22 03:41:09 +01:00
|
|
|
ttfFont, _ = freetype.ParseFont(ttf)
|
2017-09-19 03:10:02 +01:00
|
|
|
}
|
|
|
|
|
2017-10-10 04:05:46 +01:00
|
|
|
// LoadFont let you load an external font.
|
2017-09-22 03:01:40 +01:00
|
|
|
func LoadFont(fontData []byte) error {
|
|
|
|
var err error
|
|
|
|
ttfFont, err = freetype.ParseFont(fontData)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:05:46 +01:00
|
|
|
// LoadFontFromReader load an external font from an io.Reader interface.
|
|
|
|
func LoadFontFromReader(reader io.Reader) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, reader); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadFont(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2017-09-19 04:15:14 +01:00
|
|
|
// New creates a new captcha.
|
2018-01-16 04:58:32 +00:00
|
|
|
// It returns captcha data and any freetype drawing error encountered.
|
2017-09-19 03:10:02 +01:00
|
|
|
func New(width int, height int, option ...SetOption) (*Data, error) {
|
2017-09-17 07:50:28 +01:00
|
|
|
options := newDefaultOption(width, height)
|
2017-09-16 11:04:28 +01:00
|
|
|
for _, setOption := range option {
|
|
|
|
setOption(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
text := randomText(options)
|
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
2017-09-19 03:10:02 +01:00
|
|
|
draw.Draw(img, img.Bounds(), &image.Uniform{options.BackgroundColor}, image.ZP, draw.Src)
|
2017-09-17 07:50:28 +01:00
|
|
|
drawNoise(img, options)
|
2017-09-19 04:15:14 +01:00
|
|
|
drawCurves(img, options)
|
2017-09-19 03:10:02 +01:00
|
|
|
err := drawText(text, img, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-16 11:04:28 +01:00
|
|
|
|
2017-09-19 03:10:02 +01:00
|
|
|
return &Data{Text: text, img: img}, nil
|
2017-09-16 11:04:28 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 01:18:29 +01:00
|
|
|
// NewMathExpr creates a new captcha.
|
2018-01-16 04:58:32 +00:00
|
|
|
// It will generate a image with a math expression like `1 + 2`.
|
2017-09-28 01:18:29 +01:00
|
|
|
func NewMathExpr(width int, height int, option ...SetOption) (*Data, error) {
|
|
|
|
options := newDefaultOption(width, height)
|
|
|
|
for _, setOption := range option {
|
|
|
|
setOption(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
text, equation := randomEquation()
|
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
|
|
|
draw.Draw(img, img.Bounds(), &image.Uniform{options.BackgroundColor}, image.ZP, draw.Src)
|
|
|
|
drawNoise(img, options)
|
|
|
|
drawCurves(img, options)
|
|
|
|
err := drawText(equation, img, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Data{Text: text, img: img}, nil
|
|
|
|
}
|
|
|
|
|
2017-09-16 11:04:28 +01:00
|
|
|
func randomText(opts *Options) (text string) {
|
|
|
|
n := len(opts.CharPreset)
|
2017-09-19 04:15:14 +01:00
|
|
|
for i := 0; i < opts.TextLength; i++ {
|
2017-09-17 07:50:28 +01:00
|
|
|
text += string(opts.CharPreset[rng.Intn(n)])
|
2017-09-16 11:04:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
2017-09-17 07:50:28 +01:00
|
|
|
|
|
|
|
func drawNoise(img *image.NRGBA, opts *Options) {
|
2017-12-11 08:12:44 +00:00
|
|
|
noiseCount := (opts.width * opts.height) / int(28.0/opts.Noise)
|
2017-09-17 07:50:28 +01:00
|
|
|
for i := 0; i < noiseCount; i++ {
|
|
|
|
x := rng.Intn(opts.width)
|
|
|
|
y := rng.Intn(opts.height)
|
|
|
|
img.Set(x, y, randomColor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomColor() color.RGBA {
|
2018-06-12 04:06:37 +01:00
|
|
|
red := rng.Intn(256)
|
|
|
|
green := rng.Intn(256)
|
|
|
|
blue := rng.Intn(256)
|
2017-09-17 07:50:28 +01:00
|
|
|
|
|
|
|
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
|
|
|
|
}
|
2017-09-19 03:10:02 +01:00
|
|
|
|
2017-09-19 04:15:14 +01:00
|
|
|
func drawCurves(img *image.NRGBA, opts *Options) {
|
|
|
|
for i := 0; i < opts.CurveNumber; i++ {
|
2017-09-19 03:10:02 +01:00
|
|
|
drawSineCurve(img, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally we want to draw bezier curves
|
|
|
|
// For now sine curves will do the job
|
|
|
|
func drawSineCurve(img *image.NRGBA, opts *Options) {
|
|
|
|
var xStart, xEnd int
|
|
|
|
if opts.width <= 40 {
|
|
|
|
xStart, xEnd = 1, opts.width-1
|
|
|
|
} else {
|
|
|
|
xStart = rng.Intn(opts.width/10) + 1
|
|
|
|
xEnd = opts.width - rng.Intn(opts.width/10) - 1
|
|
|
|
}
|
|
|
|
curveHeight := float64(rng.Intn(opts.height/6) + opts.height/6)
|
|
|
|
yStart := rng.Intn(opts.height*2/3) + opts.height/6
|
|
|
|
angle := 1.0 + rng.Float64()
|
|
|
|
yFlip := 1.0
|
2018-03-26 04:08:21 +01:00
|
|
|
if rng.Intn(2) == 0 {
|
2017-09-19 03:10:02 +01:00
|
|
|
yFlip = -1.0
|
|
|
|
}
|
2018-08-14 07:27:29 +01:00
|
|
|
curveColor := randomColorFromOptions(opts)
|
2017-09-19 03:10:02 +01:00
|
|
|
|
|
|
|
for x1 := xStart; x1 <= xEnd; x1++ {
|
|
|
|
y := math.Sin(math.Pi*angle*float64(x1)/float64(opts.width)) * curveHeight * yFlip
|
|
|
|
img.Set(x1, int(y)+yStart, curveColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func drawText(text string, img *image.NRGBA, opts *Options) error {
|
|
|
|
ctx := freetype.NewContext()
|
2017-10-07 09:13:12 +01:00
|
|
|
ctx.SetDPI(opts.FontDPI)
|
2017-09-19 03:10:02 +01:00
|
|
|
ctx.SetClip(img.Bounds())
|
|
|
|
ctx.SetDst(img)
|
|
|
|
ctx.SetHinting(font.HintingFull)
|
|
|
|
ctx.SetFont(ttfFont)
|
|
|
|
|
|
|
|
fontSpacing := opts.width / len(text)
|
2017-09-29 02:32:04 +01:00
|
|
|
fontOffset := rng.Intn(fontSpacing / 2)
|
2017-09-19 03:10:02 +01:00
|
|
|
|
|
|
|
for idx, char := range text {
|
2017-10-08 03:25:22 +01:00
|
|
|
fontScale := 0.8 + rng.Float64()*0.4
|
|
|
|
fontSize := float64(opts.height) / fontScale * opts.FontScale
|
2017-09-19 03:10:02 +01:00
|
|
|
ctx.SetFontSize(fontSize)
|
2018-08-14 07:27:29 +01:00
|
|
|
ctx.SetSrc(image.NewUniform(randomColorFromOptions(opts)))
|
2017-09-29 02:32:04 +01:00
|
|
|
x := fontSpacing*idx + fontOffset
|
2017-09-19 03:10:02 +01:00
|
|
|
y := opts.height/6 + rng.Intn(opts.height/3) + int(fontSize/2)
|
|
|
|
pt := freetype.Pt(x, y)
|
|
|
|
if _, err := ctx.DrawString(string(char), pt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-27 06:07:08 +01:00
|
|
|
|
2018-08-14 07:27:29 +01:00
|
|
|
func randomColorFromOptions(opts *Options) color.Color {
|
|
|
|
length := len(opts.Palette)
|
|
|
|
if length == 0 {
|
|
|
|
return randomInvertColor(opts.BackgroundColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts.Palette[rng.Intn(length)]
|
|
|
|
}
|
|
|
|
|
2017-09-27 06:07:08 +01:00
|
|
|
func randomInvertColor(base color.Color) color.Color {
|
|
|
|
baseLightness := getLightness(base)
|
|
|
|
var value float64
|
|
|
|
if baseLightness >= 0.5 {
|
2017-09-29 02:32:04 +01:00
|
|
|
value = baseLightness - 0.3 - rng.Float64()*0.2
|
2017-09-27 06:07:08 +01:00
|
|
|
} else {
|
2017-09-29 02:32:04 +01:00
|
|
|
value = baseLightness + 0.3 + rng.Float64()*0.2
|
2017-09-27 06:07:08 +01:00
|
|
|
}
|
|
|
|
hue := float64(rng.Intn(361)) / 360
|
|
|
|
saturation := 0.6 + rng.Float64()*0.2
|
|
|
|
|
2018-04-26 03:03:29 +01:00
|
|
|
return hsva{h: hue, s: saturation, v: value, a: 255}
|
2017-09-27 06:07:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLightness(colour color.Color) float64 {
|
|
|
|
r, g, b, a := colour.RGBA()
|
|
|
|
// transparent
|
|
|
|
if a == 0 {
|
|
|
|
return 1.0
|
|
|
|
}
|
|
|
|
max := maxColor(r, g, b)
|
|
|
|
min := minColor(r, g, b)
|
|
|
|
|
|
|
|
l := (float64(max) + float64(min)) / (2 * 255)
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxColor(numList ...uint32) (max uint32) {
|
|
|
|
for _, num := range numList {
|
|
|
|
colorVal := num & 255
|
|
|
|
if colorVal > max {
|
|
|
|
max = colorVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
func minColor(numList ...uint32) (min uint32) {
|
|
|
|
min = 255
|
|
|
|
for _, num := range numList {
|
|
|
|
colorVal := num & 255
|
|
|
|
if colorVal < min {
|
|
|
|
min = colorVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomEquation() (text string, equation string) {
|
|
|
|
left := 1 + rng.Intn(9)
|
|
|
|
right := 1 + rng.Intn(9)
|
|
|
|
text = strconv.Itoa(left + right)
|
|
|
|
equation = strconv.Itoa(left) + "+" + strconv.Itoa(right)
|
|
|
|
|
|
|
|
return text, equation
|
|
|
|
}
|