main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2011 Dmitry Chestnykh. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // capgen is an utility to test captcha generation.
  5. package main
  6. import (
  7. "flag"
  8. "fmt"
  9. "github.com/dchest/captcha"
  10. "io"
  11. "log"
  12. "os"
  13. )
  14. var (
  15. flagImage = flag.Bool("i", true, "output image captcha")
  16. flagAudio = flag.Bool("a", false, "output audio captcha")
  17. flagLen = flag.Int("len", captcha.DefaultLen, "length of captcha")
  18. flagImgW = flag.Int("width", captcha.StdWidth, "image captcha width")
  19. flagImgH = flag.Int("height", captcha.StdHeight, "image captcha height")
  20. )
  21. func usage() {
  22. fmt.Fprintf(os.Stderr, "usage: captcha [flags] filename\n")
  23. flag.PrintDefaults()
  24. }
  25. func main() {
  26. flag.Parse()
  27. fname := flag.Arg(0)
  28. if fname == "" {
  29. usage()
  30. os.Exit(1)
  31. }
  32. f, err := os.Create(fname)
  33. if err != nil {
  34. log.Fatalf("%s", err)
  35. }
  36. defer f.Close()
  37. var w io.WriterTo
  38. d := captcha.RandomDigits(*flagLen)
  39. switch {
  40. case *flagAudio:
  41. w = captcha.NewAudio(d)
  42. case *flagImage:
  43. w = captcha.NewImage(d, *flagImgW, *flagImgH)
  44. }
  45. _, err = w.WriteTo(f)
  46. if err != nil {
  47. log.Fatalf("%s", err)
  48. }
  49. fmt.Println(d)
  50. }