Package captcha implements generation and verification of image CAPTCHAs.
A captcha solution is the sequence of digits 0-9 with the defined length.
An image representation is a PNG-encoded or JPEG-encoded image with the solution printed on it in such a way that makes it hard for computers to solve it using OCR.
This package doesn't require external files or libraries to generate captcha representations; it is self-contained.
Package code refer from dchest/captcha
Advantages:
- High-Performanceļ¼Generation captcha use goroutine + channel, get captcha ready in advance by channel
- Change panic to error, avoid runtime panic
- Not inline store interface, can use any store method such as Redis, Memcache, Memory and so on after get captcha image
- Use uuid instead of original random id avoid conflict
- Add Context to control generate captcha goroutine, can stop generate programming active
go get github.com/xkeyideal/captcha
NewCaptchaPool(width, height, wordLength, poolsize, parallelNum, imageType int)
Creates a new captcha pool
- width, height: image's width and height
- wordLength: generate words' length
- poolsize: buffer size
- parallelNum: goroutine number
- imageType: PNG or JPEG
func (p *CaptchaPool) Stop()
Stop CaptchaPool active
type CaptchaBody struct {
Id string
Data *bytes.Buffer
Val []byte
}
CaptchaPool = pool.NewCaptchaPool(240, 80, 6, 10, 1, 2)
captchaBody := CaptchaPool.GetImage()
CaptchaPool.Stop()
See detail in file captcha.go
Golang context can't control goroutine channel will deadlock by using sync.WaitGroup to wait all goroutine return and close channels.