Better CAPTCHA image generation algorithm.

This commit is contained in:
Alexander Andreev 2022-06-27 20:48:39 +04:00
parent 21e5ec118e
commit a31c995909
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
1 changed files with 18 additions and 21 deletions

View File

@ -1,10 +1,9 @@
package captcha
import (
"crypto/rand"
"image"
"justcaptcha/pkg/captcha"
"math/big"
"math/rand"
"time"
"github.com/fogleman/gg"
@ -47,30 +46,28 @@ func (c *DwellingCaptcha) generateImage(style string) *image.Image {
ctx.DrawStringAnchored(string(c.GetAnswer()), 20, 5, .5, .5)
// draw lines and points
h := float64(ctx.Height())
ctx.SetRGB255(0x9f, 0x2b, 0x68)
for i := 0; i < 16; i++ {
x0, _ := rand.Int(rand.Reader, big.NewInt(int64(ctx.Height())))
x1, _ := rand.Int(rand.Reader, big.NewInt(int64(ctx.Height())))
r, _ := rand.Int(rand.Reader, big.NewInt(int64(4)))
ctx.SetLineWidth(2)
ctx.DrawPoint(float64(x0.Int64()), float64(x1.Int64()), float64(r.Int64()))
ctx.DrawLine(float64(x0.Int64()), 0, float64(ctx.Height()), float64(x1.Int64()))
ctx.Stroke()
}
for i := 0; i < 16; i++ {
x0, _ := rand.Int(rand.Reader, big.NewInt(int64(ctx.Height())))
x1, _ := rand.Int(rand.Reader, big.NewInt(int64(ctx.Height())))
r, _ := rand.Int(rand.Reader, big.NewInt(int64(4)))
ctx.SetLineWidth(2)
ctx.DrawPoint(float64(x0.Int64()), float64(x1.Int64()), float64(r.Int64()))
ctx.DrawLine(0, float64(x0.Int64()), float64(x1.Int64()), float64(ctx.Height()))
for i := 0; i < 24; i++ {
x0 := rand.Float64() * h
x1 := rand.Float64() * h
x2 := rand.Float64() * h
x3 := rand.Float64() * h
ctx.DrawLine(x0, 0, h, x1)
ctx.DrawLine(0, x2, x3, h)
r := rand.Intn(256)
g := rand.Intn(256)
b := rand.Intn(256)
ctx.SetRGB255(r, g, b)
lw := float64(rand.Float32() * 4.0)
if lw == 0 {
lw = 0.1
}
ctx.SetLineWidth(lw)
ctx.Stroke()
}
c.Image = ctx.Image()
return &c.Image
}