forked from nanjishidu/gomini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
63 lines (59 loc) · 1.49 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
59
60
61
62
63
package gomini
import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"io"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
var (
imgBase64Reg = regexp.MustCompile(`src=\"data:image\/([a-zA-Z]*);base64,(.*?)\"`)
// 支持的图片后缀名
supportImageExtNames = []string{".jpg", ".jpeg", ".png", ".ico", ".svg", ".bmp", ".gif"}
)
// 检查文件扩展名是否是图片
func IsImage(extName string) bool {
for i := 0; i < len(supportImageExtNames); i++ {
if supportImageExtNames[i] == extName {
return true
}
}
return false
}
// 转换base64为图片链接
func ConvertBase64ToImage(content string, imagePath, prefixURI string) (string, error) {
if !IsExist(imagePath) {
err := Mkdir(imagePath)
if err != nil {
return "", err
}
}
// 0: src="data:image/jpeg;base64,2/9j/4AAQSkZJRgABAQAAAQABAAD/4Q+">
// 1: jpeg
// 2: 2/9j/4AAQSkZJRgABAQAAAQABAAD/4Q+
all := imgBase64Reg.FindAllStringSubmatch(content, -1)
for _, v := range all {
if len(v) == 3 {
hash := md5.New()
io.Copy(hash, bytes.NewBufferString(v[2]))
filename := hex.EncodeToString(hash.Sum(nil)) + "." + v[1]
toFilename := filepath.Join(imagePath, filename)
if !IsExist(toFilename) {
dst, err := base64.StdEncoding.DecodeString(v[2])
if err != nil {
return "", err
}
err = ioutil.WriteFile(toFilename, dst, 0644)
if err != nil {
return "", err
}
}
content = strings.Replace(content, v[0], `src="`+prefixURI+`/`+filename+`"`, -1)
}
}
return content, nil
}