image.go 774 B

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