Skip to content

Commit

Permalink
feat:Image新增返回将图片数据写进io.Writer方法
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyudekuangxiang committed Jul 2, 2021
1 parent 63156e7 commit 00e7546
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
}

0 comments on commit 00e7546

Please sign in to comment.