-
-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathprocessing.go
More file actions
372 lines (306 loc) · 8.5 KB
/
processing.go
File metadata and controls
372 lines (306 loc) · 8.5 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package processing
import (
"context"
"errors"
"runtime"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/imagetype"
"github.com/imgproxy/imgproxy/v3/imath"
"github.com/imgproxy/imgproxy/v3/options"
"github.com/imgproxy/imgproxy/v3/router"
"github.com/imgproxy/imgproxy/v3/security"
"github.com/imgproxy/imgproxy/v3/vips"
)
var mainPipeline = pipeline{
trim,
prepare,
scaleOnLoad,
colorspaceToProcessing,
crop,
scale,
rotateAndFlip,
cropToResult,
applyFilters,
extend,
extendAspectRatio,
padding,
fixSize,
flatten,
watermark,
}
var finalizePipeline = pipeline{
colorspaceToResult,
stripMetadata,
}
func isImageTypePreferred(imgtype imagetype.Type) bool {
for _, t := range config.PreferredFormats {
if imgtype == t {
return true
}
}
return false
}
func findBestFormat(srcType imagetype.Type, animated, expectAlpha bool) imagetype.Type {
for _, t := range config.PreferredFormats {
if animated && !t.SupportsAnimationSave() {
continue
}
if expectAlpha && !t.SupportsAlpha() {
continue
}
return t
}
return config.PreferredFormats[0]
}
func ValidatePreferredFormats() error {
filtered := config.PreferredFormats[:0]
for _, t := range config.PreferredFormats {
if !vips.SupportsSave(t) {
log.Warnf("%s can't be a preferred format as it's saving is not supported", t)
} else {
filtered = append(filtered, t)
}
}
if len(filtered) == 0 {
return errors.New("No supported preferred formats specified")
}
config.PreferredFormats = filtered
return nil
}
func getImageSize(img *vips.Image) (int, int) {
width, height := img.Width(), img.Height()
if img.IsAnimated() {
// Animated images contain multiple frames, and libvips loads them stacked vertically.
// We want to return the size of a single frame
height = img.PageHeight()
}
// If the image is rotated by 90 or 270 degrees, we need to swap width and height
orientation := img.Orientation()
if orientation == 5 || orientation == 6 || orientation == 7 || orientation == 8 {
width, height = height, width
}
return width, height
}
func transformAnimated(ctx context.Context, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
if po.Trim.Enabled {
log.Warning("Trim is not supported for animated images")
po.Trim.Enabled = false
}
imgWidth := img.Width()
framesCount := imath.Min(img.Pages(), po.SecurityOptions.MaxAnimationFrames)
frameHeight, err := img.GetInt("page-height")
if err != nil {
return err
}
// Double check dimensions because animated image has many frames
if err = security.CheckDimensions(imgWidth, frameHeight, framesCount, po.SecurityOptions); err != nil {
return err
}
if img.Pages() > framesCount {
// Load only the needed frames
if err = img.Load(imgdata, 1, 1.0, framesCount); err != nil {
return err
}
}
delay, err := img.GetIntSliceDefault("delay", nil)
if err != nil {
return err
}
loop, err := img.GetIntDefault("loop", 0)
if err != nil {
return err
}
watermarkEnabled := po.Watermark.Enabled
po.Watermark.Enabled = false
defer func() { po.Watermark.Enabled = watermarkEnabled }()
frames := make([]*vips.Image, 0, framesCount)
defer func() {
for _, frame := range frames {
if frame != nil {
frame.Clear()
}
}
}()
for i := 0; i < framesCount; i++ {
frame := new(vips.Image)
if err = img.Extract(frame, 0, i*frameHeight, imgWidth, frameHeight); err != nil {
return err
}
frames = append(frames, frame)
if err = mainPipeline.Run(ctx, frame, po, nil); err != nil {
return err
}
if r, _ := frame.GetIntDefault("imgproxy-scaled-down", 0); r == 1 {
if err = frame.CopyMemory(); err != nil {
return err
}
if err = router.CheckTimeout(ctx); err != nil {
return err
}
}
}
if err = img.Arrayjoin(frames); err != nil {
return err
}
if watermarkEnabled && imagedata.Watermark != nil {
dprScale, derr := img.GetDoubleDefault("imgproxy-dpr-scale", 1.0)
if derr != nil {
dprScale = 1.0
}
if err = applyWatermark(img, imagedata.Watermark, &po.Watermark, dprScale, framesCount); err != nil {
return err
}
}
if err = img.CastUchar(); err != nil {
return err
}
if len(delay) == 0 {
delay = make([]int, framesCount)
for i := range delay {
delay[i] = 40
}
} else if len(delay) > framesCount {
delay = delay[:framesCount]
}
img.SetInt("imgproxy-is-animated", 1)
img.SetInt("page-height", frames[0].Height())
img.SetIntSlice("delay", delay)
img.SetInt("loop", loop)
img.SetInt("n-pages", img.Height()/frames[0].Height())
return nil
}
func saveImageToFitBytes(ctx context.Context, po *options.ProcessingOptions, img *vips.Image) (*imagedata.ImageData, error) {
var diff float64
quality := po.GetQuality()
if err := img.CopyMemory(); err != nil {
return nil, err
}
for {
imgdata, err := img.Save(po.Format, quality)
if err != nil || len(imgdata.Data) <= po.MaxBytes || quality <= 10 {
return imgdata, err
}
imgdata.Close()
if err := router.CheckTimeout(ctx); err != nil {
return nil, err
}
delta := float64(len(imgdata.Data)) / float64(po.MaxBytes)
switch {
case delta > 3:
diff = 0.25
case delta > 1.5:
diff = 0.5
default:
diff = 0.75
}
quality = int(float64(quality) * diff)
}
}
func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options.ProcessingOptions) (*imagedata.ImageData, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
defer vips.Cleanup()
animationSupport :=
po.SecurityOptions.MaxAnimationFrames > 1 &&
imgdata.Type.SupportsAnimationLoad() &&
(po.Format == imagetype.Unknown || po.Format.SupportsAnimationSave())
pages := 1
if animationSupport {
pages = -1
}
img := new(vips.Image)
defer img.Clear()
if po.EnforceThumbnail && imgdata.Type.SupportsThumbnail() {
if err := img.LoadThumbnail(imgdata); err != nil {
log.Debugf("Can't load thumbnail: %s", err)
// Failed to load thumbnail, rollback to the full image
if err := img.Load(imgdata, 1, 1.0, pages); err != nil {
return nil, err
}
}
} else {
if err := img.Load(imgdata, 1, 1.0, pages); err != nil {
return nil, err
}
}
originWidth, originHeight := getImageSize(img)
animated := img.IsAnimated()
expectAlpha := !po.Flatten && (img.HasAlpha() || po.Padding.Enabled || po.Extend.Enabled)
switch {
case po.Format == imagetype.Unknown:
switch {
case po.PreferJxl && !animated:
po.Format = imagetype.JXL
case po.PreferAvif && !animated:
po.Format = imagetype.AVIF
case po.PreferWebP:
po.Format = imagetype.WEBP
case isImageTypePreferred(imgdata.Type):
po.Format = imgdata.Type
default:
po.Format = findBestFormat(imgdata.Type, animated, expectAlpha)
}
case po.EnforceJxl && !animated:
po.Format = imagetype.JXL
case po.EnforceAvif && !animated:
po.Format = imagetype.AVIF
case po.EnforceWebP:
po.Format = imagetype.WEBP
}
if !vips.SupportsSave(po.Format) {
return nil, newSaveFormatError(po.Format)
}
if po.Format.SupportsAnimationSave() && animated {
if err := transformAnimated(ctx, img, po, imgdata); err != nil {
return nil, err
}
} else {
if animated {
// We loaded animated image but the resulting format doesn't support
// animations, so we need to reload image as not animated
if err := img.Load(imgdata, 1, 1.0, 1); err != nil {
return nil, err
}
}
if err := mainPipeline.Run(ctx, img, po, imgdata); err != nil {
return nil, err
}
}
if err := finalizePipeline.Run(ctx, img, po, imgdata); err != nil {
return nil, err
}
if po.Format == imagetype.AVIF && (img.Width() < 16 || img.Height() < 16) {
if img.HasAlpha() {
po.Format = imagetype.PNG
} else {
po.Format = imagetype.JPEG
}
log.Warningf(
"Minimal dimension of AVIF is 16, current image size is %dx%d. Image will be saved as %s",
img.Width(), img.Height(), po.Format,
)
}
var (
outData *imagedata.ImageData
err error
)
if po.MaxBytes > 0 && po.Format.SupportsQuality() {
outData, err = saveImageToFitBytes(ctx, po, img)
} else {
outData, err = img.Save(po.Format, po.GetQuality())
}
if err == nil {
if outData.Headers == nil {
outData.Headers = make(map[string]string)
}
resultWidth, resultHeight := getImageSize(img)
outData.Headers["X-Origin-Width"] = strconv.Itoa(originWidth)
outData.Headers["X-Origin-Height"] = strconv.Itoa(originHeight)
outData.Headers["X-Result-Width"] = strconv.Itoa(resultWidth)
outData.Headers["X-Result-Height"] = strconv.Itoa(resultHeight)
}
return outData, err
}