image.go (view raw)
1package captcha
2
3import (
4 "image"
5 "image/png"
6 "io"
7 "os"
8 "rand"
9)
10
11
12func NewImage(numbers []byte) *image.NRGBA {
13 w := numberWidth * (dotSize + 3) * len(numbers)
14 h := numberHeight * (dotSize + 5)
15 img := image.NewNRGBA(w, h)
16 color := image.NRGBAColor{uint8(rand.Intn(50)), uint8(rand.Intn(50)), uint8(rand.Intn(128)), 0xFF}
17 fillWithCircles(img, color, 40, 4)
18 x := rand.Intn(dotSize)
19 y := 0
20 setRandomBrightness(&color, 180)
21 for _, n := range numbers {
22 y = rand.Intn(dotSize * 4)
23 drawNumber(img, font[n], x, y, color)
24 x += dotSize*numberWidth + rand.Intn(maxSkew) + 8
25 }
26 drawCirclesLine(img, color)
27 return img
28}
29
30func EncodeNewImage(w io.Writer) (numbers []byte, err os.Error) {
31 numbers = randomNumbers()
32 err = png.Encode(w, NewImage(numbers))
33 return
34}
35