-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_ext.go
62 lines (53 loc) · 1.38 KB
/
image_ext.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
package main
import (
"strings"
)
func guessImageExtByMIMEType(t string, dv string) string {
loweredT := strings.ToLower(t)
switch loweredT {
case "image/pjpeg":
fallthrough
case "image/jpeg":
return ".jpg"
case "image/x-png":
fallthrough
case "image/png":
return ".png"
case "image/gif":
return ".gif"
case "image/bmp":
return ".bmp"
case "image/webp":
return ".webp"
case "image/tiff":
return ".tif"
default:
return dv
}
}
func guessImageExtByBytes(binary []byte, dv string) string {
r := dv
if len(binary) >= 4 {
if binary[0] == 0xff && binary[1] == 0xd8 && binary[2] == 0xff {
r = ".jpg"
} else if binary[0] == 0x89 && binary[1] == 0x50 && binary[2] == 0x4e && binary[3] == 0x47 {
r = ".png"
} else if binary[0] == 0x47 && binary[1] == 0x49 && binary[2] == 0x46 && binary[3] == 0x38 {
r = ".gif"
} else if binary[0] == 0x42 && binary[1] == 0x4d {
r = ".bmp"
} else if binary[0] == 0x52 && binary[1] == 0x49 && binary[2] == 0x46 && binary[3] == 0x46 {
//RIFF
if len(binary) >= 12 {
if binary[8] == 0x57 && binary[9] == 0x45 && binary[10] == 0x42 && binary[11] == 0x50 {
r = ".webp"
}
}
} else if binary[0] == 0x49 && binary[1] == 0x49 && binary[2] == 0x2a && binary[3] == 0x00 {
r = ".tif"
} else if binary[0] == 0x4d && binary[1] == 0x4d && binary[2] == 0x00 && binary[3] == 0x2a {
r = ".tif"
}
}
return r
}