This repository has been archived on 2024-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
captcha/captcha.go

169 lines
3.7 KiB
Go
Raw Normal View History

2017-09-16 11:04:28 +01:00
package captcha
import (
2017-09-19 03:10:02 +01:00
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
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-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"
"time"
)
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-16 11:04:28 +01:00
type Options struct {
2017-09-19 03:10:02 +01:00
BackgroundColor color.Color
2017-09-17 07:50:28 +01:00
CharPreset string
TxtLength int
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,
TxtLength: 4,
width: width,
height: height,
2017-09-16 11:04:28 +01:00
}
}
2017-09-19 03:10:02 +01:00
type SetOption func(*Options)
2017-09-16 11:04:28 +01:00
type Data struct {
Text string
img *image.NRGBA
}
func (data *Data) WriteTo(w io.Writer) error {
return png.Encode(w, data.img)
}
2017-09-19 03:10:02 +01:00
func init() {
var err error
ttfFont, err = freetype.ParseFont(goregular.TTF)
if err != nil {
panic(err)
}
}
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 03:10:02 +01:00
drawLine(img, options)
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
}
func randomText(opts *Options) (text string) {
n := len(opts.CharPreset)
2017-09-17 07:50:28 +01:00
for i := 0; i < opts.TxtLength; i++ {
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) {
noiseCount := (opts.width * opts.height) / 18
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 {
red := rng.Intn(255)
green := rng.Intn(255)
blue := rng.Intn(255)
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
}
2017-09-19 03:10:02 +01:00
func drawLine(img *image.NRGBA, opts *Options) {
for i := 0; i < 3; i++ {
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()
flip := rng.Intn(2) == 0
yFlip := 1.0
if flip {
yFlip = -1.0
}
curveColor := randomDarkGray()
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 randomDarkGray() color.Gray {
gray := rng.Intn(128) + 20
return color.Gray{Y: uint8(gray)}
}
func drawText(text string, img *image.NRGBA, opts *Options) error {
ctx := freetype.NewContext()
ctx.SetDPI(72.0)
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
ctx.SetHinting(font.HintingFull)
ctx.SetFont(ttfFont)
fontSpacing := opts.width / len(text)
for idx, char := range text {
fontScale := 1 + float64(rng.Intn(7))/float64(9)
fontSize := float64(opts.height) / fontScale
ctx.SetFontSize(fontSize)
ctx.SetSrc(image.NewUniform(randomDarkGray()))
x := fontSpacing*idx + fontSpacing/int(fontSize)
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
}