-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.go
48 lines (35 loc) · 1020 Bytes
/
base64.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
package main
import (
"bufio"
"encoding/base64"
"fmt"
"os"
)
func main() {
var input string
input = "/home/svarkey/Pictures/Garden_Alteration.png"
//outFile = os.Args[2]
imgFile, err := os.Open(input) // a QR code image
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer imgFile.Close()
// create a new buffer base on file size
fInfo, _ := imgFile.Stat()
var size = fInfo.Size()
buf := make([]byte, size)
// read file content into buffer
fReader := bufio.NewReader(imgFile)
fReader.Read(buf)
// if you create a new image instead of loading from file, encode the image to buffer instead with png.Encode()
// png.Encode(&buf, image)
// convert the buffer bytes to base64 string - use buf.Bytes() for new image
imgBase64Str := base64.StdEncoding.EncodeToString(buf)
file, err := os.Create("/tmp/1.html")
if err != nil {
panic("Cannot create file")
}
defer file.Close()
fmt.Fprintf(file, "<html><body><img src=\"data:image/png;base64," + imgBase64Str + "\" /></body></html>")
}