Add writeGIF API
This commit is contained in:
parent
10262641db
commit
e5ca1c346e
13
captcha.go
13
captcha.go
|
@ -9,13 +9,14 @@ import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
|
"image/gif"
|
||||||
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"image/jpeg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
@ -76,17 +77,23 @@ type Data struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteImage encodes image data and writes to an io.Writer.
|
// WriteImage encodes image data and writes to an io.Writer.
|
||||||
// It returns possible error from PNG encoding
|
// It returns possible error from PNG encoding.
|
||||||
func (data *Data) WriteImage(w io.Writer) error {
|
func (data *Data) WriteImage(w io.Writer) error {
|
||||||
return png.Encode(w, data.img)
|
return png.Encode(w, data.img)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteJPG encodes image data in JPEG format and writes to an io.Writer.
|
// WriteJPG encodes image data in JPEG format and writes to an io.Writer.
|
||||||
// It returns possible error from JPEG encoding
|
// It returns possible error from JPEG encoding.
|
||||||
func (data *Data) WriteJPG(w io.Writer, o *jpeg.Options) error {
|
func (data *Data) WriteJPG(w io.Writer, o *jpeg.Options) error {
|
||||||
return jpeg.Encode(w, data.img, o)
|
return jpeg.Encode(w, data.img, o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteGIF encodes image data in GIF format and writes to an io.Writer.
|
||||||
|
// It returns possible error from GIF encoding.
|
||||||
|
func (data *Data) WriteGIF(w io.Writer, o *gif.Options) error {
|
||||||
|
return gif.Encode(w, data.img, o)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ttfFont, _ = freetype.ParseFont(ttf)
|
ttfFont, _ = freetype.ParseFont(ttf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,14 @@ package captcha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"golang.org/x/image/font/gofont/goregular"
|
"golang.org/x/image/font/gofont/goregular"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"image/gif"
|
||||||
|
"image/jpeg"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"errors"
|
|
||||||
"image/jpeg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCaptcha(t *testing.T) {
|
func TestNewCaptcha(t *testing.T) {
|
||||||
|
@ -17,7 +18,10 @@ func TestNewCaptcha(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
data.WriteImage(buf)
|
err = data.WriteImage(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSmallCaptcha(t *testing.T) {
|
func TestSmallCaptcha(t *testing.T) {
|
||||||
|
@ -33,7 +37,22 @@ func TestEncodeJPG(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
data.WriteJPG(buf, &jpeg.Options{70})
|
err = data.WriteJPG(buf, &jpeg.Options{Quality: 70})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeGIF(t *testing.T) {
|
||||||
|
data, err := New(150, 50)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err = data.WriteGIF(buf, &gif.Options{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCaptchaOptions(t *testing.T) {
|
func TestNewCaptchaOptions(t *testing.T) {
|
||||||
|
@ -73,7 +92,8 @@ func TestCovNilFontError(t *testing.T) {
|
||||||
ttfFont = temp
|
ttfFont = temp
|
||||||
}
|
}
|
||||||
|
|
||||||
type errReader struct {}
|
type errReader struct{}
|
||||||
|
|
||||||
func (errReader) Read(_ []byte) (int, error) {
|
func (errReader) Read(_ []byte) (int, error) {
|
||||||
return 0, errors.New("")
|
return 0, errors.New("")
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue