forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
36 lines (28 loc) · 697 Bytes
/
file.go
File metadata and controls
36 lines (28 loc) · 697 Bytes
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
package graphics
import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
)
// 将图片保存到指定的路径
func SaveImage(p string, src image.Image) error {
os.MkdirAll(filepath.Dir(p),0666)
f, err := os.OpenFile(p, os.O_SYNC|os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
ext := filepath.Ext(p)
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") {
err = jpeg.Encode(f, src, &jpeg.Options{Quality: 80})
} else if strings.EqualFold(ext, ".png") {
err = png.Encode(f, src)
} else if strings.EqualFold(ext, ".gif") {
err = gif.Encode(f, src, &gif.Options{NumColors: 256})
}
return err
}