-
Notifications
You must be signed in to change notification settings - Fork 0
/
converters.go
84 lines (73 loc) · 1.74 KB
/
converters.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
82
83
84
package medias
import (
"fmt"
"image"
"os"
)
// Dimensions regroups the width and height of an image.
type Dimensions struct {
Width int
Height int
}
func ResizeTo(maxWidthOrHeight int) Dimensions {
return Dimensions{
Width: maxWidthOrHeight,
Height: maxWidthOrHeight,
}
}
func OriginalSize() Dimensions {
return Dimensions{
Width: 0,
Height: 0,
}
}
// Zero returns if the dimensions are not available.
func (d Dimensions) Zero() bool {
return d.Height == 0 && d.Width == 0
}
func (d Dimensions) Landscape() bool {
if d.Zero() {
return false
}
return d.Width > d.Height
}
func (d Dimensions) Portrait() bool {
if d.Zero() {
return false
}
return d.Width < d.Height
}
// LargerThan returns if at least one dimension exceeds the given size.
// LargerThan is conservatrice and returns true if dimensions are not available.
func (d Dimensions) LargerThan(widthOrHeight int) bool {
if d.Zero() {
// Considers any image to be larger if dimensions are not available
// Upscaling will be performed...
return true
}
return d.Height > widthOrHeight || d.Width > widthOrHeight
}
func (d Dimensions) String() string {
return fmt.Sprintf("%dx%d", d.Width, d.Height)
}
// ReadImageDimensions extracts the dimensions from a GIF/PNG/JPEG file.
func ReadImageDimensions(path string) (Dimensions, error) {
f, err := os.Open(path)
if err != nil {
return Dimensions{}, err
}
config, _, err := image.DecodeConfig(f)
if err != nil {
return Dimensions{}, err
}
return Dimensions{
Width: config.Width,
Height: config.Height,
}, nil
}
type Converter interface {
OnPreGeneration(func(cmd string, args ...string))
ToAVIF(src, dest string, dimensions Dimensions) error
ToMP3(src, dest string) error
ToWebM(src, dest string) error
}