-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathresize_image.go
49 lines (39 loc) · 933 Bytes
/
resize_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
package images
import (
"image"
"image/jpeg"
"log"
"os"
"github.com/nfnt/resize"
)
// 得到图片的宽和高
func GetImageSize(imagePath string) (width, height int) {
file, err := os.Open(imagePath)
if err != nil {
log.Println("open file failed:", err)
}
image, _, err := image.DecodeConfig(file)
if err != nil {
log.Println("decode image config failed:", err)
}
return image.Width, image.Height
}
// 修改图片尺寸
func Resize(imagePath, targePath string, width, height uint) {
file, err := os.Open(imagePath)
if err != nil {
log.Println("open file failed:", err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Println("decode file failed:", err)
}
file.Close()
newImg := resize.Resize(width, height, img, resize.Lanczos3)
newFile, err := os.Create(targePath)
if err != nil {
log.Println("create file failed:", err)
}
defer newFile.Close()
jpeg.Encode(newFile, newImg, nil)
}