parent
dbb96819ea
commit
98d9d078e2
|
@ -27,7 +27,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
session.Values["captcha"] = data.Text
|
session.Values["captcha"] = data.Text
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
// send image data to client
|
// send image data to client
|
||||||
data.WriteTo(w)
|
data.WriteImage(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
13
captcha.go
13
captcha.go
|
@ -2,6 +2,7 @@
|
||||||
package captcha
|
package captcha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"github.com/golang/freetype"
|
"github.com/golang/freetype"
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
"golang.org/x/image/font"
|
"golang.org/x/image/font"
|
||||||
|
@ -83,13 +84,23 @@ func init() {
|
||||||
ttfFont, _ = freetype.ParseFont(ttf)
|
ttfFont, _ = freetype.ParseFont(ttf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadFont let you load an external font
|
// LoadFont let you load an external font.
|
||||||
func LoadFont(fontData []byte) error {
|
func LoadFont(fontData []byte) error {
|
||||||
var err error
|
var err error
|
||||||
ttfFont, err = freetype.ParseFont(fontData)
|
ttfFont, err = freetype.ParseFont(fontData)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadFontFromReader load an external font from an io.Reader interface.
|
||||||
|
func LoadFontFromReader(reader io.Reader) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if _, err := io.Copy(&buf, reader); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadFont(buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new captcha.
|
// New creates a new captcha.
|
||||||
// It returns captcha data and any freetype drawing error encountered
|
// It returns captcha data and any freetype drawing error encountered
|
||||||
func New(width int, height int, option ...SetOption) (*Data, error) {
|
func New(width int, height int, option ...SetOption) (*Data, error) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"golang.org/x/image/font/gofont/goregular"
|
"golang.org/x/image/font/gofont/goregular"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,6 +74,17 @@ func TestLoadFont(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadFontFromReader(t *testing.T) {
|
||||||
|
file, err := os.Open("./fonts/Comismsh.ttf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Fail to load test file")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = LoadFontFromReader(file); err != nil {
|
||||||
|
t.Fatal("Fail to load font from io.Reader")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaxColor(t *testing.T) {
|
func TestMaxColor(t *testing.T) {
|
||||||
var result uint32
|
var result uint32
|
||||||
result = maxColor()
|
result = maxColor()
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
0.12.0 / 2017-10-07
|
0.12.0 / 2017-10-07
|
||||||
===================
|
===================
|
||||||
|
|
||||||
* Add FontDPI and FontScale options
|
* Add FontDPI and FontScale options
|
||||||
|
|
||||||
0.11.0 / 2017-09-28
|
0.11.0 / 2017-09-28
|
||||||
===================
|
===================
|
||||||
|
|
||||||
* Add NewMathExpr API
|
* Add NewMathExpr API
|
||||||
|
|
||||||
0.10.0 / 2017-09-23
|
0.10.0 / 2017-09-23
|
||||||
===================
|
===================
|
||||||
|
|
||||||
* Add LoadFont API
|
* Add LoadFont API
|
||||||
|
|
||||||
0.9.0 / 2017-09-20
|
0.9.0 / 2017-09-20
|
||||||
|
|
Reference in New Issue