-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
81 lines (70 loc) · 1.39 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package mediaprobe
import (
"image"
"io"
"os"
// Expanding image package
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/rwcarlsen/goexif/exif"
)
// ParseImage used for retrieve image data
func (info *Info) ParseImage() error {
if info.data == nil {
file, err := os.Open(info.filename)
if err != nil {
return err
}
defer file.Close()
if err := info.decodeImage(file); err != nil {
return err
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return err
}
info.decodeExif(file)
return nil
}
body, err := getRemoteFile(info.uri)
if err != nil {
return err
}
err = info.decodeImage(body)
body.Close()
if err != nil {
return err
}
body, err = getRemoteFile(info.uri)
if err != nil {
return err
}
info.decodeExif(body)
body.Close()
return nil
}
func (info *Info) decodeImage(reader io.Reader) error {
img, _, err := image.DecodeConfig(reader)
if err != nil {
return err
}
info.Width = img.Width
info.Height = img.Height
return nil
}
func (info *Info) decodeExif(reader io.Reader) {
if x, err := exif.Decode(reader); err == nil {
if orientationTag, err := x.Get(exif.Orientation); err == nil {
switch orientationTag.String() {
case "5", "6", "7", "8":
w, h := info.Width, info.Height
info.Width = h
info.Height = w
}
}
}
}