Add WriteJPG option for jpeg format captcha
This commit is contained in:
parent
abab5c281c
commit
10262641db
|
@ -15,6 +15,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
"image/jpeg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
@ -80,6 +81,12 @@ 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.
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ttfFont, _ = freetype.ParseFont(ttf)
|
ttfFont, _ = freetype.ParseFont(ttf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"errors"
|
"errors"
|
||||||
|
"image/jpeg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCaptcha(t *testing.T) {
|
func TestNewCaptcha(t *testing.T) {
|
||||||
|
@ -26,6 +27,15 @@ func TestSmallCaptcha(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEncodeJPG(t *testing.T) {
|
||||||
|
data, err := New(150, 50)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
data.WriteJPG(buf, &jpeg.Options{70})
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewCaptchaOptions(t *testing.T) {
|
func TestNewCaptchaOptions(t *testing.T) {
|
||||||
New(100, 34, func(options *Options) {
|
New(100, 34, func(options *Options) {
|
||||||
options.BackgroundColor = color.Opaque
|
options.BackgroundColor = color.Opaque
|
||||||
|
|
Reference in New Issue