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/color"
|
||||
"image/draw"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
"image/jpeg"
|
||||
)
|
||||
|
||||
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
@ -76,17 +77,23 @@ type Data struct {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
return png.Encode(w, data.img)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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() {
|
||||
ttfFont, _ = freetype.ParseFont(ttf)
|
||||
}
|
||||
|
|
|
@ -2,13 +2,14 @@ package captcha
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"image/color"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"math/rand"
|
||||
"os"
|
||||
"testing"
|
||||
"errors"
|
||||
"image/jpeg"
|
||||
)
|
||||
|
||||
func TestNewCaptcha(t *testing.T) {
|
||||
|
@ -17,7 +18,10 @@ func TestNewCaptcha(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
data.WriteImage(buf)
|
||||
err = data.WriteImage(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmallCaptcha(t *testing.T) {
|
||||
|
@ -33,7 +37,22 @@ func TestEncodeJPG(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
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) {
|
||||
|
@ -74,6 +93,7 @@ func TestCovNilFontError(t *testing.T) {
|
|||
}
|
||||
|
||||
type errReader struct{}
|
||||
|
||||
func (errReader) Read(_ []byte) (int, error) {
|
||||
return 0, errors.New("")
|
||||
}
|
||||
|
|
Reference in New Issue