add: basic shape for API

This commit is contained in:
Weilin Shi 2017-09-16 18:04:28 +08:00
parent c2173db527
commit 95aa54b0c3
7 changed files with 85 additions and 0 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/ .glide/
.idea/workspace.xml

9
.idea/captcha.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/captcha.iml" filepath="$PROJECT_DIR$/.idea/captcha.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

59
captcha.go Normal file
View File

@ -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
}

BIN
fonts/Comismsh.ttf Normal file

Binary file not shown.

2
fonts/LICENSE.md Normal file
View File

@ -0,0 +1,2 @@
license: free
website: http://www.moorstation.org/typoasis/designers/gemnew/home.htm