func main() { flag.Parse() fname := flag.Arg(0) if fname == "" { usage() os.Exit(1) } f, err := os.Create(fname) if err != nil { log.Fatalf("%s", err) } defer f.Close() var w io.WriterTo d := captcha.RandomDigits(*flagLen) switch { case *flagAudio: w = captcha.NewAudio(d, *flagLang) case *flagImage: w = captcha.NewImage(d, *flagImgW, *flagImgH) } _, err = w.WriteTo(f) if err != nil { log.Fatalf("%s", err) } fmt.Println(d) }
func (this *Captcha) GetImage(width int, height int) ([]byte, error) { buffer := bytes.NewBuffer(nil) img := captcha.NewImage("", this.data, width, height) _, err := img.WriteTo(buffer) if err != nil { return nil, err } return buffer.Bytes(), nil }
// Will serve image, when user does not click reload // yet. func (rcv *controller) createNewImage(image string) error { c := redis.Get() secret, err := goredis.Bytes(c.Do("GET", image)) if err != nil { return err } png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight)) return nil }
func GetImageCaptcha(c *gin.Context) { session := sessions.Default(c) captchaId := captcha.New() bytes := captcha.RandomDigits(4) captcheString := util.GetCaptchaString(bytes) session.Set(constant.LOGIN_CAPTCHA, captcheString) Logger.Debug("new captcha:%v", captcheString) session.Save() image := captcha.NewImage(captchaId, bytes, captcha.StdWidth/2, captcha.StdHeight/2) _, err := image.WriteTo(c.Writer) if err != nil { Logger.Error("error occured when writing captcha image to response") } }
func gen(fname string) (Id string) { f, err := os.Create(fname) if err != nil { log.Fatalf("%s", err) } defer f.Close() var w io.WriterTo d := captcha.RandomDigits(6) // six digits is enough w = captcha.NewImage(d, captcha.StdWidth, captcha.StdHeight) _, err = w.WriteTo(f) if err != nil { log.Fatalf("%s", err) } log.Println("DEBUG Captcha: Created new image ", d) str := trans(d) return str }
// Request new image, if the previous captcha is difficult to recognize. func (rcv *controller) changeImage(image string) { c := redis.Get() // Configure out, if the image still available. _, err := goredis.Bytes(c.Do("GET", image)) if err != nil { http.NotFound(rcv.response, rcv.request) return } secret := captcha.RandomDigits(7) if _, err := c.Do("SET", image, secret); err != nil { panic(err.Error()) } if _, err := c.Do("EXPIRE", image, expired); err != nil { panic(err.Error()) } png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight)) }