update: tweak captcha color and position

This commit is contained in:
Weilin Shi 2017-09-29 09:32:04 +08:00
parent f9832124ae
commit 02daf62bc0
6 changed files with 21 additions and 14 deletions

View File

@ -35,6 +35,8 @@ func handle(w http.ResponseWriter, r *http.Request) {
## sample image ## sample image
![image](example/captcha.png) ![image](example/captcha.png)
![image](example/captcha-math.png)
## Contributing ## Contributing
If your found a bug, please contribute! If your found a bug, please contribute!
see [contributing.md](contributing.md) for more detail see [contributing.md](contributing.md) for more detail

View File

@ -174,7 +174,7 @@ func drawSineCurve(img *image.NRGBA, opts *Options) {
if flip { if flip {
yFlip = -1.0 yFlip = -1.0
} }
curveColor := randomDarkColor() curveColor := randomInvertColor(opts.BackgroundColor)
for x1 := xStart; x1 <= xEnd; x1++ { for x1 := xStart; x1 <= xEnd; x1++ {
y := math.Sin(math.Pi*angle*float64(x1)/float64(opts.width)) * curveHeight * yFlip 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 { func drawText(text string, img *image.NRGBA, opts *Options) error {
ctx := freetype.NewContext() ctx := freetype.NewContext()
ctx.SetDPI(92.0) ctx.SetDPI(92.0)
@ -199,13 +191,14 @@ func drawText(text string, img *image.NRGBA, opts *Options) error {
ctx.SetFont(ttfFont) ctx.SetFont(ttfFont)
fontSpacing := opts.width / len(text) fontSpacing := opts.width / len(text)
fontOffset := rng.Intn(fontSpacing / 2)
for idx, char := range text { for idx, char := range text {
fontScale := 1 + rng.Float64()*0.5 fontScale := 1 + rng.Float64()*0.5
fontSize := float64(opts.height) / fontScale fontSize := float64(opts.height) / fontScale
ctx.SetFontSize(fontSize) ctx.SetFontSize(fontSize)
ctx.SetSrc(image.NewUniform(randomInvertColor(opts.BackgroundColor))) 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) y := opts.height/6 + rng.Intn(opts.height/3) + int(fontSize/2)
pt := freetype.Pt(x, y) pt := freetype.Pt(x, y)
if _, err := ctx.DrawString(string(char), pt); err != nil { if _, err := ctx.DrawString(string(char), pt); err != nil {
@ -220,9 +213,9 @@ func randomInvertColor(base color.Color) color.Color {
baseLightness := getLightness(base) baseLightness := getLightness(base)
var value float64 var value float64
if baseLightness >= 0.5 { if baseLightness >= 0.5 {
value = baseLightness - 0.25 - rng.Float64()*0.2 value = baseLightness - 0.3 - rng.Float64()*0.2
} else { } else {
value = baseLightness + 0.25 + rng.Float64()*0.2 value = baseLightness + 0.3 + rng.Float64()*0.2
} }
hue := float64(rng.Intn(361)) / 360 hue := float64(rng.Intn(361)) / 360
saturation := 0.6 + rng.Float64()*0.2 saturation := 0.6 + rng.Float64()*0.2

BIN
example/captcha-math.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@ -5,6 +5,7 @@
<title>Captcha</title> <title>Captcha</title>
</head> </head>
<body> <body>
<img src="/captcha" alt="captcha"> <img src="/captcha-default" alt="captcha">
<img src="/captcha-math" alt="captcha">
</body> </body>
</html> </html>

View File

@ -9,7 +9,8 @@ import (
func main() { func main() {
http.HandleFunc("/", indexHandle) http.HandleFunc("/", indexHandle)
http.HandleFunc("/captcha", captchaHandle) http.HandleFunc("/captcha-default", captchaHandle)
http.HandleFunc("/captcha-math", mathHandle)
fmt.Println("Server start at port 8080") fmt.Println("Server start at port 8080")
err := http.ListenAndServe(":8080", nil) err := http.ListenAndServe(":8080", nil)
if err != nil { if err != nil {
@ -35,3 +36,13 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) {
} }
img.WriteTo(w) 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)
}