From 00e754631d6df2b7b12c2526c6646b619e4fc3a5 Mon Sep 17 00:00:00 2001 From: ajin Date: Fri, 2 Jul 2021 10:38:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:Image=E6=96=B0=E5=A2=9E=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=B0=86=E5=9B=BE=E7=89=87=E6=95=B0=E6=8D=AE=E5=86=99=E8=BF=9B?= =?UTF-8?q?io.Writer=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- image.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/image.go b/image.go index 844fd53..b008e57 100644 --- a/image.go +++ b/image.go @@ -297,11 +297,14 @@ func saveAs(img image.Image, path string) error { return err } defer file.Close() - switch filepath.Ext(path) { - case ".png": - return png.Encode(file, img) - case ".jpg": - return jpeg.Encode(file, img, nil) + return saveWriter(img, filepath.Ext(path)[1:], file) +} +func saveWriter(img image.Image, ext string, writer io.Writer) error { + switch ext { + case "png": + return png.Encode(writer, img) + case "jpg": + return jpeg.Encode(writer, img, nil) } return errors.New("ext not support") } @@ -515,3 +518,8 @@ func (i *Image) Height() int { func (i *Image) Image() draw.Image { return i.img } + +//将图片数据写进io.Writer ext 图片格式 支持png jpg +func (i *Image) Encode(writer io.Writer, ext string) error { + return saveWriter(i.img, ext, writer) +}