parent
32fccd9c5c
commit
b25acadd0a
|
@ -33,7 +33,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
```
|
```
|
||||||
|
|
||||||
[documentation](https://godoc.org/github.com/steambap/captcha) |
|
[documentation](https://godoc.org/github.com/steambap/captcha) |
|
||||||
[example](example/main.go)
|
[example](example/basic/main.go)
|
||||||
|
|
||||||
## sample image
|
## sample image
|
||||||
![image](example/captcha.png)
|
![image](example/captcha.png)
|
||||||
|
|
|
@ -36,6 +36,9 @@ type Options struct {
|
||||||
// CurveNumber is the number of curves to draw on captcha image.
|
// CurveNumber is the number of curves to draw on captcha image.
|
||||||
// It defaults to 2.
|
// It defaults to 2.
|
||||||
CurveNumber int
|
CurveNumber int
|
||||||
|
// FontDPI controls scale of font.
|
||||||
|
// The default is 92.0.
|
||||||
|
FontDPI float64
|
||||||
|
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
@ -47,6 +50,7 @@ func newDefaultOption(width, height int) *Options {
|
||||||
CharPreset: charPreset,
|
CharPreset: charPreset,
|
||||||
TextLength: 4,
|
TextLength: 4,
|
||||||
CurveNumber: 2,
|
CurveNumber: 2,
|
||||||
|
FontDPI: 92.0,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
}
|
}
|
||||||
|
@ -184,7 +188,7 @@ func drawSineCurve(img *image.NRGBA, opts *Options) {
|
||||||
|
|
||||||
func drawText(text string, img *image.NRGBA, opts *Options) error {
|
func drawText(text string, img *image.NRGBA, opts *Options) error {
|
||||||
ctx := freetype.NewContext()
|
ctx := freetype.NewContext()
|
||||||
ctx.SetDPI(92.0)
|
ctx.SetDPI(opts.FontDPI)
|
||||||
ctx.SetClip(img.Bounds())
|
ctx.SetClip(img.Bounds())
|
||||||
ctx.SetDst(img)
|
ctx.SetDst(img)
|
||||||
ctx.SetHinting(font.HintingFull)
|
ctx.SetHinting(font.HintingFull)
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Load Font</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<img src="/captcha" alt="captcha">
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,45 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/steambap/captcha"
|
||||||
|
"golang.org/x/image/font/gofont/goregular"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := captcha.LoadFont(goregular.TTF)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.HandleFunc("/", indexHandle)
|
||||||
|
http.HandleFunc("/captcha", captchaHandle)
|
||||||
|
fmt.Println("Server start at port 8080")
|
||||||
|
err = http.ListenAndServe(":8080", nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexHandle(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
doc, err := template.ParseFiles("index.html")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
doc.Execute(w, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func captchaHandle(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
img, err := captcha.New(150, 50, func(options *captcha.Options) {
|
||||||
|
options.FontDPI = 72.0
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, nil)
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
img.WriteTo(w)
|
||||||
|
}
|
Reference in New Issue