-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffimage.go
215 lines (189 loc) · 4.97 KB
/
ffimage.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package ffimage
import (
"context"
"errors"
"math"
ffmpeg "github.com/u2takey/ffmpeg-go"
"gopkg.in/vansante/go-ffprobe.v2"
)
// ImageFormat
type ImageFormat string
const (
ImageFormatUnknown = ""
ImageFormatJPEG = "jpg"
ImageFormatJPEGXL = "jxl"
ImageFormatWEBP = "webp"
ImageFormatPNG = "png"
ImageFormatAVIF = "avif"
ImageFormatAPNG = "apng"
ImageFormatBMP = "bmp"
ImageFormatGIF = "gif"
)
// ResizeType
type ResizeType int
const (
ResizeTypeNone ResizeType = iota
ResizeTypeUpscale
ResizeTypeDownscale
)
// PositionType
type PositionType string
const (
PositionTypeNone PositionType = ""
PositionTypeTopLeft PositionType = "top_left"
PositionTypeTop PositionType = "top"
PositionTypeTopRight PositionType = "top_right"
PositionTypeLeft PositionType = "left"
PositionTypeCenter PositionType = "center"
PositionTypeRight PositionType = "right"
PositionTypeBottomLeft PositionType = "bottom_left"
PositionTypeBottom PositionType = "bottom"
PositionTypeBottomRight PositionType = "bottom_right"
)
// Name Worse Best Default Usage
// JPEG 31 2 17 -qscale:v
// WEBP 0 100 75 -quality
// JPXL 0 100 90 -qscale:v
// AVIF 63 0 50 -crf
// PNG - - -
// BMP - - -
// GIF - - -
// Image
type Image struct {
Stream *ffprobe.Stream
Width int
Height int
Path string
Output *Output
Silent bool
}
type Output struct {
Path string
Args []ffmpeg.KwArgs
Filters []*filter
Quality int
Format ImageFormat
Loop int
IsPreserved bool
EXIF string
Codec string
BackgroundColor string
}
// NewImage
func NewImage(path string) (*Image, error) {
image := &Image{
Path: path,
Output: &Output{
BackgroundColor: "black",
Args: make([]ffmpeg.KwArgs, 0),
Filters: make([]*filter, 0),
},
Silent: true,
}
if err := image.loadImageSize(); err != nil {
return nil, err
}
image.addArg(ffmpeg.KwArgs{"map_metadata": "-1"})
image.addFilter("format", ffmpeg.Args{"rgba"})
return image, nil
}
// loadImageSize
func (i *Image) loadImageSize() error {
data, err := ffprobe.ProbeURL(context.TODO(), i.Path)
if err != nil {
return err
}
if len(data.Streams) == 0 || data.Streams[0].Width == 0 || data.Streams[0].Height == 0 || data.Streams[0] == nil {
return errors.New("ffimage: no valid media was found")
}
i.Stream = data.Streams[0]
i.setWidthHeight(i.Stream.Width, i.Stream.Height)
return nil
}
// filter
type filter struct {
k string
args ffmpeg.Args
}
// addFilter
func (i *Image) addFilter(k string, args ffmpeg.Args) {
i.Output.Filters = append(i.Output.Filters, &filter{k, args})
}
// addArg
func (i *Image) addArg(args ffmpeg.KwArgs) {
i.Output.Args = append(i.Output.Args, args)
}
// setWidthHeight
func (i *Image) setWidthHeight(w, h int) {
i.Width = w
i.Height = h
}
// qualityFactor
func qualityFactor(min, max int, quality int, isLowerBetter bool) int {
if isLowerBetter {
return max + int((float64(quality)/100)*float64(min-max))
}
return min + int((float64(quality)/100)*float64(max-min))
}
// calcPosition
func (i *Image) calcPosition(origW, origH, w, h int, pos PositionType) (x int, y int) {
switch pos {
case PositionTypeTopLeft:
return
case PositionTypeTop:
x = (origW / 2) - (w / 2)
case PositionTypeTopRight:
x = origW - w
case PositionTypeLeft:
y = (origH / 2) - (h / 2)
case PositionTypeCenter:
x = (origW / 2) - (w / 2)
y = (origH / 2) - (h / 2)
case PositionTypeRight:
x = origW - w
y = (origH / 2) - (h / 2)
case PositionTypeBottomLeft:
y = origH - h
case PositionTypeBottom:
x = (origW / 2) - (w / 2)
y = origH - h
case PositionTypeBottomRight:
x = origW - w
y = origH - h
}
x = int(math.Abs(float64(x)))
y = int(math.Abs(float64(y)))
return
}
// calcBestfit
func (i *Image) calcBestfit(origW, origH, w, h int, typ ResizeType) (newW, newH int) {
ratio := float64(origW) / float64(origH)
if origW > origH {
newH = h
newW = int((float64(newH) * ratio))
if (typ == ResizeTypeDownscale && newW > w) || (typ == ResizeTypeUpscale && w > newW) {
newW = w
newH = int((float64(newW) / ratio))
}
} else {
newW = w
newH = int((float64(newW) / ratio))
if (typ == ResizeTypeDownscale && newH > h) || (typ == ResizeTypeUpscale && h > newH) {
newH = h
newW = int((float64(newH) * ratio))
}
}
return
}
// calcBestpad
func (i *Image) calcBestpad(origW, origH, w, h int) (newW, newH int) {
var ratio float64
if float64(origW)/float64(w) > float64(origH)/float64(h) {
ratio = float64(w) / float64(origW)
} else {
ratio = float64(h) / float64(origH)
}
newW = int(float64(origW) * ratio)
newH = int(float64(origH) * ratio)
return
}