From 3512d3993a483b559c70ea6e153b703b2710b054 Mon Sep 17 00:00:00 2001 From: zxysilent Date: Fri, 10 Jul 2020 08:28:28 +0800 Subject: [PATCH] =?UTF-8?q?[U]=20=E4=BC=98=E5=8C=96=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/auth.go | 2 +- internal/vcode/vcode.go | 14 ++++---------- internal/vcode/vcode_test.go | 6 +----- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/control/auth.go b/control/auth.go index c4a9dd1..97a6ad5 100644 --- a/control/auth.go +++ b/control/auth.go @@ -117,7 +117,7 @@ func Vcode(ctx echo.Context) error { Vcode string `json:"vcode"` Vreal string `json:"vreal"` }{ - Vcode: vcode.NewImage(rnd).Bs64(), + Vcode: vcode.NewImage(rnd).Base64(), Vreal: hmc(rnd, "v.c.o.d.e"), } return ctx.JSON(utils.Succ("succ", out)) diff --git a/internal/vcode/vcode.go b/internal/vcode/vcode.go index f99f963..416ce85 100644 --- a/internal/vcode/vcode.go +++ b/internal/vcode/vcode.go @@ -6,7 +6,6 @@ import ( "image" "image/color" "image/png" - "io" "math" "math/rand" "time" @@ -75,9 +74,9 @@ func (m *Image) getRandomPalette() color.Palette { return p } -// encodedPNG encodes an image to PNG and returns +// EncodedPNG encodes an image to PNG and returns // the result as a byte slice. -func (m *Image) encodedPNG() []byte { +func (m *Image) EncodedPNG() []byte { var buf bytes.Buffer if err := png.Encode(&buf, m.Paletted); err != nil { panic(err.Error()) @@ -85,13 +84,8 @@ func (m *Image) encodedPNG() []byte { return buf.Bytes() } -// WriteTo writes captcha image in PNG format into the given writer. -func (m *Image) WriteTo(w io.Writer) (int64, error) { - n, err := w.Write(m.encodedPNG()) - return int64(n), err -} -func (m *Image) Bs64() string { - return base64.RawStdEncoding.EncodeToString(m.encodedPNG()) +func (m *Image) Base64() string { + return base64.RawStdEncoding.EncodeToString(m.EncodedPNG()) } func (m *Image) calculateSizes(stdWidth, stdHeight, ncount int) { diff --git a/internal/vcode/vcode_test.go b/internal/vcode/vcode_test.go index 34af9b0..1ecbb6d 100644 --- a/internal/vcode/vcode_test.go +++ b/internal/vcode/vcode_test.go @@ -2,7 +2,6 @@ package vcode import ( "fmt" - "os" "testing" "github.com/zxysilent/utils" @@ -13,8 +12,5 @@ func TestVcode(t *testing.T) { for i := 0; i < len(s); i++ { fmt.Println(s[i]) } - fout, _ := os.Create("out.png") - NewImage(utils.RandDigitStr(6)).WriteTo(fout) - fmt.Println(NewImage(utils.RandDigitStr(6)).Bs64()) - fout.Close() + fmt.Println(NewImage(utils.RandDigitStr(6)).Base64()) }