diff --git a/.gitignore b/.gitignore index a1338d6..902846f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ +.idea/workspace.xml diff --git a/.idea/captcha.iml b/.idea/captcha.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/captcha.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a4a694b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/captcha.go b/captcha.go new file mode 100644 index 0000000..c8713a2 --- /dev/null +++ b/captcha.go @@ -0,0 +1,59 @@ +package captcha + +import ( + "io" + "image/color" + "image" + "image/png" + "math/rand" + "time" +) + +const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + +type Options struct { + BackgroundColor color.RGBA + CharPreset string + TxtLength int +} + +func newDefaultOption() *Options { + return &Options{ + CharPreset: charPreset, + TxtLength: 4, + } +} + +type Option func(*Options) + +type Data struct { + Text string + + img *image.NRGBA +} + +func (data *Data) WriteTo(w io.Writer) error { + return png.Encode(w, data.img) +} + +func New(width int, height int, option... Option) *Data { + options := newDefaultOption() + for _, setOption := range option { + setOption(options) + } + + text := randomText(options) + img := image.NewNRGBA(image.Rect(0, 0, width, height)) + + return &Data{Text: text, img: img} +} + +func randomText(opts *Options) (text string) { + n := len(opts.CharPreset) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i :=0; i < opts.TxtLength; i++ { + text += string(opts.CharPreset[r.Intn(n)]) + } + + return text +} diff --git a/fonts/Comismsh.ttf b/fonts/Comismsh.ttf new file mode 100644 index 0000000..0c4a384 Binary files /dev/null and b/fonts/Comismsh.ttf differ diff --git a/fonts/LICENSE.md b/fonts/LICENSE.md new file mode 100644 index 0000000..9c4315c --- /dev/null +++ b/fonts/LICENSE.md @@ -0,0 +1,2 @@ +license: free +website: http://www.moorstation.org/typoasis/designers/gemnew/home.htm \ No newline at end of file