diff --git a/README.md b/README.md index 02c2a7d..8bca156 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ func handle(w http.ResponseWriter, r *http.Request) { ## sample image ![image](example/captcha.png) +![image](example/captcha-math.png) + ## Contributing If your found a bug, please contribute! see [contributing.md](contributing.md) for more detail diff --git a/captcha.go b/captcha.go index bd22868..2634db7 100644 --- a/captcha.go +++ b/captcha.go @@ -174,7 +174,7 @@ func drawSineCurve(img *image.NRGBA, opts *Options) { if flip { yFlip = -1.0 } - curveColor := randomDarkColor() + curveColor := randomInvertColor(opts.BackgroundColor) for x1 := xStart; x1 <= xEnd; x1++ { y := math.Sin(math.Pi*angle*float64(x1)/float64(opts.width)) * curveHeight * yFlip @@ -182,14 +182,6 @@ func drawSineCurve(img *image.NRGBA, opts *Options) { } } -func randomDarkColor() hsva { - hue := float64(rng.Intn(361)) / 360 - saturation := 0.6 + rng.Float64()*0.2 - value := 0.25 + rng.Float64()*0.2 - - return hsva{h: hue, s: saturation, v: value, a: uint8(255)} -} - func drawText(text string, img *image.NRGBA, opts *Options) error { ctx := freetype.NewContext() ctx.SetDPI(92.0) @@ -199,13 +191,14 @@ func drawText(text string, img *image.NRGBA, opts *Options) error { ctx.SetFont(ttfFont) fontSpacing := opts.width / len(text) + fontOffset := rng.Intn(fontSpacing / 2) for idx, char := range text { fontScale := 1 + rng.Float64()*0.5 fontSize := float64(opts.height) / fontScale ctx.SetFontSize(fontSize) ctx.SetSrc(image.NewUniform(randomInvertColor(opts.BackgroundColor))) - x := fontSpacing*idx + fontSpacing/int(fontSize) + x := fontSpacing*idx + fontOffset 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 { @@ -220,9 +213,9 @@ func randomInvertColor(base color.Color) color.Color { baseLightness := getLightness(base) var value float64 if baseLightness >= 0.5 { - value = baseLightness - 0.25 - rng.Float64()*0.2 + value = baseLightness - 0.3 - rng.Float64()*0.2 } else { - value = baseLightness + 0.25 + rng.Float64()*0.2 + value = baseLightness + 0.3 + rng.Float64()*0.2 } hue := float64(rng.Intn(361)) / 360 saturation := 0.6 + rng.Float64()*0.2 diff --git a/example/captcha-math.png b/example/captcha-math.png new file mode 100644 index 0000000..74a9322 Binary files /dev/null and b/example/captcha-math.png differ diff --git a/example/captcha.png b/example/captcha.png index abdd5a5..4f492ca 100644 Binary files a/example/captcha.png and b/example/captcha.png differ diff --git a/example/index.html b/example/index.html index 096559f..ddf4f90 100644 --- a/example/index.html +++ b/example/index.html @@ -5,6 +5,7 @@ Captcha - captcha + captcha + captcha \ No newline at end of file diff --git a/example/main.go b/example/main.go index 4cb66dc..4e4472c 100644 --- a/example/main.go +++ b/example/main.go @@ -9,7 +9,8 @@ import ( func main() { http.HandleFunc("/", indexHandle) - http.HandleFunc("/captcha", captchaHandle) + http.HandleFunc("/captcha-default", captchaHandle) + http.HandleFunc("/captcha-math", mathHandle) fmt.Println("Server start at port 8080") err := http.ListenAndServe(":8080", nil) if err != nil { @@ -35,3 +36,13 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) { } img.WriteTo(w) } + +func mathHandle(w http.ResponseWriter, _ *http.Request) { + img, err := captcha.NewMathExpr(150, 50) + if err != nil { + fmt.Fprint(w, nil) + fmt.Println(err.Error()) + return + } + img.WriteTo(w) +}