mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-19 10:08:03 +08:00
完善功能
This commit is contained in:
32
graphics/file.go
Normal file
32
graphics/file.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"image"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"image/gif"
|
||||
)
|
||||
// 将图片保存到指定的路径
|
||||
func SaveImage(p string, src image.Image) error {
|
||||
|
||||
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 : 100 })
|
||||
} 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
|
||||
}
|
Reference in New Issue
Block a user