-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathencode.go
44 lines (39 loc) · 995 Bytes
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package imgo
import (
"bytes"
"encoding/base64"
"image"
"image/png"
"io/ioutil"
)
//Img2Base64 produce a base64 string from a image file.
func Img2Base64(filepath string) (encodeString string, err error) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return
}
encodeString = base64.StdEncoding.EncodeToString(data)
return
}
//Base64ToImg create a image file named dstFile from base64 encodeString.
func Base64ToImg(encodeString string, dstFile string) error {
data, err := base64.StdEncoding.DecodeString(encodeString)
if err != nil {
return err
}
err2 := ioutil.WriteFile(dstFile, data, 0666)
if err2 != nil {
return err2
}
return nil
}
//Img2base64ByGoImage produce a base64 string from Image interface.
func Img2Base64ByGoImage(img image.Image) (encodeString string, err error) {
var w bytes.Buffer
encodeErr := png.Encode(&w, img)
if encodeErr != nil {
return "", encodeErr
}
encodeString = base64.StdEncoding.EncodeToString(w.Bytes())
return
}