-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimage.go
58 lines (49 loc) · 1.01 KB
/
image.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package toolkit
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"strings"
)
func Base64ToImage(base64String, destination string) error {
if strings.HasPrefix(base64String, "data:image") {
parts := strings.SplitN(base64String, ",", 2)
if len(parts) > 1 {
base64String = parts[1]
} else {
return errors.New("invalid base64 string")
}
}
bts := bytes.NewBufferString(base64String)
decoded := base64.NewDecoder(base64.StdEncoding, bts)
f, err := os.Create(destination)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, decoded)
if err != nil {
return err
}
return nil
}
func ImageToBase64(source string) (string, error) {
f, err := os.Open(source)
if err != nil {
return "", err
}
defer f.Close()
fInfo, _ := f.Stat()
var size int64 = fInfo.Size()
buf := make([]byte, size)
fReader := bufio.NewReader(f)
fReader.Read(buf)
imgBase64String := base64.StdEncoding.EncodeToString(buf)
return imgBase64String, nil
}