-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
41 lines (38 loc) · 866 Bytes
/
file.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
package gopic
import (
"errors"
"image/jpeg"
"image/png"
"os"
)
// ImageFile Import the image and determine the type according to the suffix
//only jpg or png
func ImageFile(filename string) *PicFile {
imgType := ImgType(filename)
switch imgType {
case 1: //JPEG
img, err := FileOpen(filename)
if err != nil {
return nil
}
jpgfile, _ := jpeg.Decode(img)
return &PicFile{jpgfile, "jpg", nil}
case 3: //PNG
img, err := FileOpen(filename)
if err != nil {
return nil
}
pngfile, _ := png.Decode(img)
return &PicFile{pngfile, "png", nil}
default:
return &PicFile{nil, "None", errors.New("unsupported image type")}
}
}
//FileOpen Open the file and determine the type according to the suffix
func FileOpen(filename string) (*os.File, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return file, nil
}