-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
48 lines (41 loc) · 1.04 KB
/
data.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
package imageprocess
import (
"errors"
"strings"
)
const (
JPG = "jpg"
JPEG = "jpeg"
PNG = "png"
BMP = "bmp"
GIF = "gif"
WEBP = "webp"
TIFF = "tiff"
)
// Supported formats
var supportedFormats = map[string]Format{
"jpg": JPG,
"jpeg": JPEG,
"png": PNG,
"gif": GIF,
"tif": TIFF,
"tiff": TIFF,
"bmp": BMP,
"webp": WEBP,
}
// Format is an image file format.
type Format string
// ErrUnsupportedFormat means the given image format is not supported.
var ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
var ErrUnsupportedParameter = errors.New("imaging: unsupported image parameter")
// FormatFromExtension parses image format from filename extension:
func FormatFromExtension(ext string) (Format, error) {
extArr := strings.Split(strings.ToLower(ext), ".")
if f, ok := supportedFormats[extArr[len(extArr)-1]]; ok {
return f, nil
}
return "", ErrUnsupportedFormat
}
func FormatFromFilename(filename string) (Format, error) {
return FormatFromExtension(strings.ToLower(strings.TrimPrefix(filename, ".")))
}