main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. flagLang = flag.String("lang", "en", "language for audio captcha")
  18. flagLen = flag.Int("len", captcha.DefaultLen, "length of captcha")
  19. flagImgW = flag.Int("width", captcha.StdWidth, "image captcha width")
  20. flagImgH = flag.Int("height", captcha.StdHeight, "image captcha height")
  21. )
  22. func usage() {
  23. fmt.Fprintf(os.Stderr, "usage: capgen [flags] filename\n")
  24. flag.PrintDefaults()
  25. }
  26. func main() {
  27. flag.Parse()
  28. fname := flag.Arg(0)
  29. if fname == "" {
  30. usage()
  31. os.Exit(1)
  32. }
  33. f, err := os.Create(fname)
  34. if err != nil {
  35. log.Fatalf("%s", err)
  36. }
  37. defer f.Close()
  38. var w io.WriterTo
  39. d := captcha.RandomDigits(*flagLen)
  40. switch {
  41. case *flagAudio:
  42. w = captcha.NewAudio("", d, *flagLang)
  43. case *flagImage:
  44. w = captcha.NewImage("", d, *flagImgW, *flagImgH)
  45. }
  46. _, err = w.WriteTo(f)
  47. if err != nil {
  48. log.Fatalf("%s", err)
  49. }
  50. fmt.Println(d)
  51. }