add: draw font and curves

This commit is contained in:
Weilin Shi 2017-09-19 10:10:02 +08:00
parent cd62640c27
commit abf1a76526
3 changed files with 143 additions and 11 deletions

View File

@ -1,18 +1,27 @@
package captcha package captcha
import ( import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"image" "image"
"image/color" "image/color"
"image/draw"
"image/png" "image/png"
"io" "io"
"math"
"math/rand" "math/rand"
"time" "time"
) )
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
var ttfFont *truetype.Font
type Options struct { type Options struct {
BackgroundColor color.RGBA BackgroundColor color.Color
CharPreset string CharPreset string
TxtLength int TxtLength int
width int width int
@ -21,6 +30,7 @@ type Options struct {
func newDefaultOption(width, height int) *Options { func newDefaultOption(width, height int) *Options {
return &Options{ return &Options{
BackgroundColor: color.Transparent,
CharPreset: charPreset, CharPreset: charPreset,
TxtLength: 4, TxtLength: 4,
width: width, width: width,
@ -28,7 +38,7 @@ func newDefaultOption(width, height int) *Options {
} }
} }
type Option func(*Options) type SetOption func(*Options)
type Data struct { type Data struct {
Text string Text string
@ -40,7 +50,15 @@ func (data *Data) WriteTo(w io.Writer) error {
return png.Encode(w, data.img) return png.Encode(w, data.img)
} }
func New(width int, height int, option ...Option) *Data { 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) {
options := newDefaultOption(width, height) options := newDefaultOption(width, height)
for _, setOption := range option { for _, setOption := range option {
setOption(options) setOption(options)
@ -48,14 +66,19 @@ func New(width int, height int, option ...Option) *Data {
text := randomText(options) text := randomText(options)
img := image.NewNRGBA(image.Rect(0, 0, width, height)) 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) drawNoise(img, options)
drawLine(img, options)
err := drawText(text, img, options)
if err != nil {
return nil, err
}
return &Data{Text: text, img: img} return &Data{Text: text, img: img}, nil
} }
func randomText(opts *Options) (text string) { func randomText(opts *Options) (text string) {
n := len(opts.CharPreset) n := len(opts.CharPreset)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < opts.TxtLength; i++ { for i := 0; i < opts.TxtLength; i++ {
text += string(opts.CharPreset[rng.Intn(n)]) text += string(opts.CharPreset[rng.Intn(n)])
} }
@ -64,7 +87,6 @@ func randomText(opts *Options) (text string) {
} }
func drawNoise(img *image.NRGBA, opts *Options) { func drawNoise(img *image.NRGBA, opts *Options) {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
noiseCount := (opts.width * opts.height) / 18 noiseCount := (opts.width * opts.height) / 18
for i := 0; i < noiseCount; i++ { for i := 0; i < noiseCount; i++ {
x := rng.Intn(opts.width) x := rng.Intn(opts.width)
@ -74,10 +96,73 @@ func drawNoise(img *image.NRGBA, opts *Options) {
} }
func randomColor() color.RGBA { func randomColor() color.RGBA {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
red := rng.Intn(255) red := rng.Intn(255)
green := rng.Intn(255) green := rng.Intn(255)
blue := rng.Intn(255) blue := rng.Intn(255)
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)} return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
} }
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
}

10
example/index.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Captcha</title>
</head>
<body>
<img src="/captcha" alt="captcha">
</body>
</html>

37
example/main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"github.com/steambap/captcha"
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/", IndexHandle)
http.HandleFunc("/captcha", CaptchaHandle)
fmt.Println("Server start at port 8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
func IndexHandle(w http.ResponseWriter, _ *http.Request) {
doc, err := template.ParseFiles("index.html")
if err != nil {
fmt.Fprint(w, err.Error())
return
}
doc.Execute(w, nil)
}
func CaptchaHandle(w http.ResponseWriter, _ *http.Request) {
img, err := captcha.New(150, 50)
if err != nil {
fmt.Fprint(w, nil)
fmt.Println(err.Error())
return
}
img.WriteTo(w)
}