• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cshum / imagor / 15495688029

06 Jun 2025 04:55PM UTC coverage: 92.216% (-0.2%) from 92.43%
15495688029

Pull #562

github

cshum
cleanup
Pull Request #562: build: libvips 8.17.0, vipsgen 1.1.1

9 of 13 new or added lines in 1 file covered. (69.23%)

10 existing lines in 2 files now uncovered.

4893 of 5306 relevant lines covered (92.22%)

1.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.56
/processor/vipsprocessor/process.go
1
package vipsprocessor
2

3
import (
4
        "context"
5
        "github.com/cshum/vipsgen/vips"
6
        "math"
7
        "strconv"
8
        "strings"
9
        "time"
10

11
        "github.com/cshum/imagor"
12
        "github.com/cshum/imagor/imagorpath"
13
        "go.uber.org/zap"
14
)
15

16
var imageTypeMap = map[string]vips.ImageType{
17
        "gif":  vips.ImageTypeGif,
18
        "jpeg": vips.ImageTypeJpeg,
19
        "jpg":  vips.ImageTypeJpeg,
20
        "pdf":  vips.ImageTypePdf,
21
        "png":  vips.ImageTypePng,
22
        "svg":  vips.ImageTypeSvg,
23
        "tiff": vips.ImageTypeTiff,
24
        "webp": vips.ImageTypeWebp,
25
        "heif": vips.ImageTypeHeif,
26
        "bmp":  vips.ImageTypeBmp,
27
        "avif": vips.ImageTypeAvif,
28
        "jp2":  vips.ImageTypeJp2k,
29
}
30

31
// IsAnimationSupported indicates if image type supports animation
32
func IsAnimationSupported(imageType vips.ImageType) bool {
1✔
33
        return imageType == vips.ImageTypeGif || imageType == vips.ImageTypeWebp
1✔
34
}
1✔
35

36
// Process implements imagor.Processor interface
37
func (v *Processor) Process(
38
        ctx context.Context, blob *imagor.Blob, p imagorpath.Params, load imagor.LoadFunc,
39
) (*imagor.Blob, error) {
1✔
40
        ctx = withContext(ctx)
1✔
41
        defer contextDone(ctx)
1✔
42
        var (
1✔
43
                thumbnailNotSupported bool
1✔
44
                upscale               = true
1✔
45
                stretch               = p.Stretch
1✔
46
                thumbnail             = false
1✔
47
                stripExif             bool
1✔
48
                stripMetadata         = v.StripMetadata
1✔
49
                orient                int
1✔
50
                img                   *vips.Image
1✔
51
                format                = vips.ImageTypeUnknown
1✔
52
                maxN                  = v.MaxAnimationFrames
1✔
53
                maxBytes              int
1✔
54
                page                  = 1
1✔
55
                dpi                   = 0
1✔
56
                focalRects            []focal
1✔
57
                err                   error
1✔
58
        )
1✔
59
        if p.Trim {
2✔
60
                thumbnailNotSupported = true
1✔
61
        }
1✔
62
        if p.FitIn {
2✔
63
                upscale = false
1✔
64
        }
1✔
65
        if maxN == 0 || maxN < -1 {
2✔
66
                maxN = 1
1✔
67
        }
1✔
68
        if blob != nil && !blob.SupportsAnimation() {
2✔
69
                maxN = 1
1✔
70
        }
1✔
71
        for _, p := range p.Filters {
2✔
72
                if v.disableFilters[p.Name] {
2✔
73
                        continue
1✔
74
                }
75
                switch p.Name {
1✔
76
                case "format":
1✔
77
                        if imageType, ok := imageTypeMap[p.Args]; ok {
2✔
78
                                format = supportedSaveFormat(imageType)
1✔
79
                                if !IsAnimationSupported(format) {
2✔
80
                                        // no frames if export format not support animation
1✔
81
                                        maxN = 1
1✔
82
                                }
1✔
83
                        }
84
                        break
1✔
85
                case "max_frames":
1✔
86
                        if n, _ := strconv.Atoi(p.Args); n > 0 && (maxN == -1 || n < maxN) {
2✔
87
                                maxN = n
1✔
88
                        }
1✔
89
                        break
1✔
90
                case "stretch":
1✔
91
                        stretch = true
1✔
92
                        break
1✔
93
                case "upscale":
1✔
94
                        upscale = true
1✔
95
                        break
1✔
96
                case "no_upscale":
1✔
97
                        upscale = false
1✔
98
                        break
1✔
99
                case "fill", "background_color":
1✔
100
                        if args := strings.Split(p.Args, ","); args[0] == "auto" {
2✔
101
                                thumbnailNotSupported = true
1✔
102
                        }
1✔
103
                        break
1✔
104
                case "page":
1✔
105
                        if n, _ := strconv.Atoi(p.Args); n > 0 {
2✔
106
                                page = n
1✔
107
                        }
1✔
108
                        break
1✔
109
                case "dpi":
×
110
                        if n, _ := strconv.Atoi(p.Args); n > 0 {
×
111
                                dpi = n
×
112
                        }
×
113
                        break
×
114
                case "orient":
1✔
115
                        if n, _ := strconv.Atoi(p.Args); n > 0 {
2✔
116
                                orient = n
1✔
117
                                thumbnailNotSupported = true
1✔
118
                        }
1✔
119
                        break
1✔
120
                case "max_bytes":
1✔
121
                        if n, _ := strconv.Atoi(p.Args); n > 0 {
2✔
122
                                maxBytes = n
1✔
123
                                thumbnailNotSupported = true
1✔
124
                        }
1✔
125
                        break
1✔
126
                case "trim", "focal", "rotate":
1✔
127
                        thumbnailNotSupported = true
1✔
128
                        break
1✔
129
                case "strip_exif":
1✔
130
                        stripExif = true
1✔
131
                case "strip_metadata":
1✔
132
                        stripMetadata = true
1✔
133
                        break
1✔
134
                }
135
        }
136

137
        if !thumbnailNotSupported &&
1✔
138
                p.CropBottom == 0.0 && p.CropTop == 0.0 && p.CropLeft == 0.0 && p.CropRight == 0.0 {
2✔
139
                // apply shrink-on-load where possible
1✔
140
                if p.FitIn {
2✔
141
                        if p.Width > 0 || p.Height > 0 {
2✔
142
                                w := p.Width
1✔
143
                                h := p.Height
1✔
144
                                if w == 0 {
2✔
145
                                        w = v.MaxWidth
1✔
146
                                }
1✔
147
                                if h == 0 {
2✔
148
                                        h = v.MaxHeight
1✔
149
                                }
1✔
150
                                size := vips.SizeDown
1✔
151
                                if upscale {
2✔
152
                                        size = vips.SizeBoth
1✔
153
                                }
1✔
154
                                if img, err = v.NewThumbnail(
1✔
155
                                        ctx, blob, w, h, vips.InterestingNone, size, maxN, page, dpi,
1✔
156
                                ); err != nil {
1✔
157
                                        return nil, err
×
158
                                }
×
159
                                thumbnail = true
1✔
160
                        }
161
                } else if stretch {
2✔
162
                        if p.Width > 0 && p.Height > 0 {
2✔
163
                                if img, err = v.NewThumbnail(
1✔
164
                                        ctx, blob, p.Width, p.Height,
1✔
165
                                        vips.InterestingNone, vips.SizeForce, maxN, page, dpi,
1✔
166
                                ); err != nil {
1✔
167
                                        return nil, err
×
168
                                }
×
169
                                thumbnail = true
1✔
170
                        }
171
                } else {
1✔
172
                        if p.Width > 0 && p.Height > 0 {
2✔
173
                                interest := vips.InterestingNone
1✔
174
                                if p.Smart {
2✔
175
                                        interest = vips.InterestingAttention
1✔
176
                                        thumbnail = true
1✔
177
                                } else if (p.VAlign == imagorpath.VAlignTop && p.HAlign == "") ||
2✔
178
                                        (p.HAlign == imagorpath.HAlignLeft && p.VAlign == "") {
2✔
179
                                        interest = vips.InterestingLow
1✔
180
                                        thumbnail = true
1✔
181
                                } else if (p.VAlign == imagorpath.VAlignBottom && p.HAlign == "") ||
2✔
182
                                        (p.HAlign == imagorpath.HAlignRight && p.VAlign == "") {
2✔
183
                                        interest = vips.InterestingHigh
1✔
184
                                        thumbnail = true
1✔
185
                                } else if (p.VAlign == "" || p.VAlign == "middle") &&
2✔
186
                                        (p.HAlign == "" || p.HAlign == "center") {
2✔
187
                                        interest = vips.InterestingCentre
1✔
188
                                        thumbnail = true
1✔
189
                                }
1✔
190
                                if thumbnail {
2✔
191
                                        if img, err = v.NewThumbnail(
1✔
192
                                                ctx, blob, p.Width, p.Height,
1✔
193
                                                interest, vips.SizeBoth, maxN, page, dpi,
1✔
194
                                        ); err != nil {
2✔
195
                                                return nil, err
1✔
196
                                        }
1✔
197
                                }
198
                        } else if p.Width > 0 && p.Height == 0 {
2✔
199
                                if img, err = v.NewThumbnail(
1✔
200
                                        ctx, blob, p.Width, v.MaxHeight,
1✔
201
                                        vips.InterestingNone, vips.SizeBoth, maxN, page, dpi,
1✔
202
                                ); err != nil {
2✔
203
                                        return nil, err
1✔
204
                                }
1✔
205
                                thumbnail = true
1✔
206
                        } else if p.Height > 0 && p.Width == 0 {
2✔
207
                                if img, err = v.NewThumbnail(
1✔
208
                                        ctx, blob, v.MaxWidth, p.Height,
1✔
209
                                        vips.InterestingNone, vips.SizeBoth, maxN, page, dpi,
1✔
210
                                ); err != nil {
1✔
211
                                        return nil, err
×
212
                                }
×
213
                                thumbnail = true
1✔
214
                        }
215
                }
216
        }
217
        if !thumbnail {
2✔
218
                if thumbnailNotSupported {
2✔
219
                        if img, err = v.NewImage(ctx, blob, maxN, page, dpi); err != nil {
1✔
220
                                return nil, err
×
221
                        }
×
222
                } else {
1✔
223
                        if img, err = v.NewThumbnail(
1✔
224
                                ctx, blob, v.MaxWidth, v.MaxHeight,
1✔
225
                                vips.InterestingNone, vips.SizeDown, maxN, page, dpi,
1✔
226
                        ); err != nil {
2✔
227
                                return nil, err
1✔
228
                        }
1✔
229
                }
230
        }
231
        // this should be called BEFORE vipscontext.contextDone
232
        defer img.Close()
1✔
233

1✔
234
        if orient > 0 {
2✔
235
                // orient rotate before resize
1✔
236
                if err = img.RotMultiPage(getAngle(orient)); err != nil {
1✔
237
                        return nil, err
×
238
                }
×
239
        }
240
        var (
1✔
241
                quality     int
1✔
242
                bitdepth    int
1✔
243
                compression int
1✔
244
                palette     bool
1✔
245
                origWidth   = float64(img.Width())
1✔
246
                origHeight  = float64(img.PageHeight())
1✔
247
        )
1✔
248
        if format == vips.ImageTypeUnknown {
2✔
249
                if blob.BlobType() == imagor.BlobTypeAVIF {
1✔
250
                        // meta loader determined as heif
×
251
                        format = vips.ImageTypeAvif
×
252
                } else {
1✔
253
                        format = img.Format()
1✔
254
                }
1✔
255
        }
256
        if v.Debug {
2✔
257
                v.Logger.Debug("image",
1✔
258
                        zap.Int("width", img.Width()),
1✔
259
                        zap.Int("height", img.Height()),
1✔
260
                        zap.Int("page_height", img.PageHeight()))
1✔
261
        }
1✔
262
        for _, p := range p.Filters {
2✔
263
                if v.disableFilters[p.Name] {
2✔
264
                        continue
1✔
265
                }
266
                switch p.Name {
1✔
267
                case "quality":
1✔
268
                        quality, _ = strconv.Atoi(p.Args)
1✔
269
                        break
1✔
270
                case "autojpg":
1✔
271
                        format = vips.ImageTypeJpeg
1✔
272
                        break
1✔
273
                case "focal":
1✔
274
                        args := strings.FieldsFunc(p.Args, argSplit)
1✔
275
                        switch len(args) {
1✔
276
                        case 4:
1✔
277
                                f := focal{}
1✔
278
                                f.Left, _ = strconv.ParseFloat(args[0], 64)
1✔
279
                                f.Top, _ = strconv.ParseFloat(args[1], 64)
1✔
280
                                f.Right, _ = strconv.ParseFloat(args[2], 64)
1✔
281
                                f.Bottom, _ = strconv.ParseFloat(args[3], 64)
1✔
282
                                if f.Left < 1 && f.Top < 1 && f.Right <= 1 && f.Bottom <= 1 {
2✔
283
                                        f.Left *= origWidth
1✔
284
                                        f.Right *= origWidth
1✔
285
                                        f.Top *= origHeight
1✔
286
                                        f.Bottom *= origHeight
1✔
287
                                }
1✔
288
                                if f.Right > f.Left && f.Bottom > f.Top {
2✔
289
                                        focalRects = append(focalRects, f)
1✔
290
                                }
1✔
291
                        case 2:
1✔
292
                                f := focal{}
1✔
293
                                f.Left, _ = strconv.ParseFloat(args[0], 64)
1✔
294
                                f.Top, _ = strconv.ParseFloat(args[1], 64)
1✔
295
                                if f.Left < 1 && f.Top < 1 {
2✔
296
                                        f.Left *= origWidth
1✔
297
                                        f.Top *= origHeight
1✔
298
                                }
1✔
299
                                f.Right = f.Left + 1
1✔
300
                                f.Bottom = f.Top + 1
1✔
301
                                focalRects = append(focalRects, f)
1✔
302
                        }
303
                        break
1✔
304
                case "palette":
1✔
305
                        palette = true
1✔
306
                        break
1✔
307
                case "bitdepth":
1✔
308
                        bitdepth, _ = strconv.Atoi(p.Args)
1✔
309
                        break
1✔
310
                case "compression":
1✔
311
                        compression, _ = strconv.Atoi(p.Args)
1✔
312
                        break
1✔
313
                }
314
        }
315
        if err := v.process(ctx, img, p, load, thumbnail, stretch, upscale, focalRects); err != nil {
1✔
UNCOV
316
                return nil, WrapErr(err)
×
UNCOV
317
        }
×
318
        if p.Meta {
2✔
319
                // metadata without export
1✔
320
                return imagor.NewBlobFromJsonMarshal(metadata(img, format, stripExif)), nil
1✔
321
        }
1✔
322
        format = supportedSaveFormat(format) // convert to supported export format
1✔
323
        for {
2✔
324
                buf, err := v.export(img, format, compression, quality, palette, bitdepth, stripMetadata)
1✔
325
                if err != nil {
1✔
326
                        return nil, WrapErr(err)
×
327
                }
×
328
                if maxBytes > 0 && (quality > 10 || quality == 0) && format != vips.ImageTypePng {
2✔
329
                        ln := len(buf)
1✔
330
                        if v.Debug {
2✔
331
                                v.Logger.Debug("max_bytes",
1✔
332
                                        zap.Int("bytes", ln),
1✔
333
                                        zap.Int("quality", quality),
1✔
334
                                )
1✔
335
                        }
1✔
336
                        if ln > maxBytes {
2✔
337
                                if quality == 0 {
2✔
338
                                        quality = 80
1✔
339
                                }
1✔
340
                                delta := float64(ln) / float64(maxBytes)
1✔
341
                                switch {
1✔
342
                                case delta > 3:
1✔
343
                                        quality = quality * 25 / 100
1✔
344
                                case delta > 1.5:
1✔
345
                                        quality = quality * 50 / 100
1✔
346
                                default:
×
347
                                        quality = quality * 75 / 100
×
348
                                }
349
                                if err := ctx.Err(); err != nil {
1✔
350
                                        return nil, WrapErr(err)
×
351
                                }
×
352
                                continue
1✔
353
                        }
354
                }
355
                blob := imagor.NewBlobFromBytes(buf)
1✔
356
                if typ, ok := format.MimeType(); ok {
2✔
357
                        blob.SetContentType(typ)
1✔
358
                }
1✔
359
                return blob, nil
1✔
360
        }
361
}
362

363
func (v *Processor) process(
364
        ctx context.Context, img *vips.Image, p imagorpath.Params, load imagor.LoadFunc, thumbnail, stretch, upscale bool, focalRects []focal,
365
) error {
1✔
366
        var (
1✔
367
                origWidth  = float64(img.Width())
1✔
368
                origHeight = float64(img.PageHeight())
1✔
369
                cropLeft,
1✔
370
                cropTop,
1✔
371
                cropRight,
1✔
372
                cropBottom float64
1✔
373
        )
1✔
374
        if p.CropRight > 0 || p.CropLeft > 0 || p.CropBottom > 0 || p.CropTop > 0 {
2✔
375
                // percentage
1✔
376
                cropLeft = math.Max(p.CropLeft, 0)
1✔
377
                cropTop = math.Max(p.CropTop, 0)
1✔
378
                cropRight = p.CropRight
1✔
379
                cropBottom = p.CropBottom
1✔
380
                if p.CropLeft < 1 && p.CropTop < 1 && p.CropRight <= 1 && p.CropBottom <= 1 {
2✔
381
                        cropLeft = math.Round(cropLeft * origWidth)
1✔
382
                        cropTop = math.Round(cropTop * origHeight)
1✔
383
                        cropRight = math.Round(cropRight * origWidth)
1✔
384
                        cropBottom = math.Round(cropBottom * origHeight)
1✔
385
                }
1✔
386
                if cropRight == 0 {
2✔
387
                        cropRight = origWidth - 1
1✔
388
                }
1✔
389
                if cropBottom == 0 {
2✔
390
                        cropBottom = origHeight - 1
1✔
391
                }
1✔
392
                cropRight = math.Min(cropRight, origWidth-1)
1✔
393
                cropBottom = math.Min(cropBottom, origHeight-1)
1✔
394
        }
395
        if p.Trim {
2✔
396
                if l, t, w, h, err := findTrim(ctx, img, p.TrimBy, p.TrimTolerance); err == nil {
2✔
397
                        cropLeft = math.Max(cropLeft, float64(l))
1✔
398
                        cropTop = math.Max(cropTop, float64(t))
1✔
399
                        if cropRight > 0 {
2✔
400
                                cropRight = math.Min(cropRight, float64(l+w))
1✔
401
                        } else {
2✔
402
                                cropRight = float64(l + w)
1✔
403
                        }
1✔
404
                        if cropBottom > 0 {
2✔
405
                                cropBottom = math.Min(cropBottom, float64(t+h))
1✔
406
                        } else {
2✔
407
                                cropBottom = float64(t + h)
1✔
408
                        }
1✔
409
                }
410
        }
411
        if cropRight > cropLeft && cropBottom > cropTop {
2✔
412
                if err := img.ExtractAreaMultiPage(
1✔
413
                        int(cropLeft), int(cropTop), int(cropRight-cropLeft), int(cropBottom-cropTop),
1✔
414
                ); err != nil {
1✔
415
                        return err
×
416
                }
×
417
        }
418
        var (
1✔
419
                w = p.Width
1✔
420
                h = p.Height
1✔
421
        )
1✔
422
        if w == 0 && h == 0 {
2✔
423
                w = img.Width()
1✔
424
                h = img.PageHeight()
1✔
425
        } else if w == 0 {
3✔
426
                w = img.Width() * h / img.PageHeight()
1✔
427
                if !upscale && w > img.Width() {
1✔
428
                        w = img.Width()
×
429
                }
×
430
        } else if h == 0 {
2✔
431
                h = img.PageHeight() * w / img.Width()
1✔
432
                if !upscale && h > img.PageHeight() {
1✔
433
                        h = img.PageHeight()
×
434
                }
×
435
        }
436
        if !thumbnail {
2✔
437
                if p.FitIn {
2✔
438
                        if upscale || w < img.Width() || h < img.PageHeight() {
2✔
439
                                if err := img.ThumbnailImage(w, &vips.ThumbnailImageOptions{Height: h, Crop: vips.InterestingNone}); err != nil {
1✔
440
                                        return err
×
441
                                }
×
442
                        }
443
                } else if stretch {
2✔
444
                        if upscale || (w < img.Width() && h < img.PageHeight()) {
2✔
445
                                if err := img.ThumbnailImage(
1✔
446
                                        w, &vips.ThumbnailImageOptions{Height: h, Crop: vips.InterestingNone, Size: vips.SizeForce},
1✔
447
                                ); err != nil {
1✔
448
                                        return err
×
449
                                }
×
450
                        }
451
                } else if upscale || w < img.Width() || h < img.PageHeight() {
2✔
452
                        interest := vips.InterestingCentre
1✔
453
                        if p.Smart {
1✔
454
                                interest = vips.InterestingAttention
×
455
                        } else if float64(w)/float64(h) > float64(img.Width())/float64(img.PageHeight()) {
2✔
456
                                if p.VAlign == imagorpath.VAlignTop {
2✔
457
                                        interest = vips.InterestingLow
1✔
458
                                } else if p.VAlign == imagorpath.VAlignBottom {
3✔
459
                                        interest = vips.InterestingHigh
1✔
460
                                }
1✔
461
                        } else {
1✔
462
                                if p.HAlign == imagorpath.HAlignLeft {
2✔
463
                                        interest = vips.InterestingLow
1✔
464
                                } else if p.HAlign == imagorpath.HAlignRight {
3✔
465
                                        interest = vips.InterestingHigh
1✔
466
                                }
1✔
467
                        }
468
                        if len(focalRects) > 0 {
2✔
469
                                focalX, focalY := parseFocalPoint(focalRects...)
1✔
470
                                if err := v.FocalThumbnail(
1✔
471
                                        img, w, h,
1✔
472
                                        (focalX-cropLeft)/float64(img.Width()),
1✔
473
                                        (focalY-cropTop)/float64(img.PageHeight()),
1✔
474
                                ); err != nil {
1✔
475
                                        return err
×
476
                                }
×
477
                        } else {
1✔
478
                                if err := v.Thumbnail(img, w, h, interest, vips.SizeBoth); err != nil {
1✔
479
                                        return err
×
480
                                }
×
481
                        }
482
                        if _, err := v.CheckResolution(img, nil); err != nil {
1✔
UNCOV
483
                                return err
×
UNCOV
484
                        }
×
485
                }
486
        }
487
        if p.HFlip {
2✔
488
                if err := img.Flip(vips.DirectionHorizontal); err != nil {
1✔
489
                        return err
×
490
                }
×
491
        }
492
        if p.VFlip {
2✔
493
                if err := img.Flip(vips.DirectionVertical); err != nil {
1✔
494
                        return err
×
495
                }
×
496
        }
497
        for i, filter := range p.Filters {
2✔
498
                if err := ctx.Err(); err != nil {
1✔
499
                        return err
×
500
                }
×
501
                if v.disableFilters[filter.Name] {
2✔
502
                        continue
1✔
503
                }
504
                if v.MaxFilterOps > 0 && i >= v.MaxFilterOps {
2✔
505
                        if v.Debug {
2✔
506
                                v.Logger.Debug("max-filter-ops-exceeded",
1✔
507
                                        zap.String("name", filter.Name), zap.String("args", filter.Args))
1✔
508
                        }
1✔
509
                        break
1✔
510
                }
511
                start := time.Now()
1✔
512
                var args []string
1✔
513
                if filter.Args != "" {
2✔
514
                        args = strings.Split(filter.Args, ",")
1✔
515
                }
1✔
516
                if fn := v.Filters[filter.Name]; fn != nil {
2✔
517
                        if err := fn(ctx, img, load, args...); err != nil {
1✔
518
                                return err
×
519
                        }
×
520
                } else if filter.Name == "fill" {
2✔
521
                        if err := v.fill(ctx, img, w, h,
1✔
522
                                p.PaddingLeft, p.PaddingTop, p.PaddingRight, p.PaddingBottom,
1✔
523
                                filter.Args); err != nil {
1✔
524
                                return err
×
525
                        }
×
526
                }
527
                if v.Debug {
2✔
528
                        v.Logger.Debug("filter",
1✔
529
                                zap.String("name", filter.Name), zap.String("args", filter.Args),
1✔
530
                                zap.Duration("took", time.Since(start)))
1✔
531
                }
1✔
532
        }
533
        return nil
1✔
534
}
535

536
// Metadata image attributes
537
type Metadata struct {
538
        Format      string            `json:"format"`
539
        ContentType string            `json:"content_type"`
540
        Width       int               `json:"width"`
541
        Height      int               `json:"height"`
542
        Orientation int               `json:"orientation"`
543
        Pages       int               `json:"pages"`
544
        Bands       int               `json:"bands"`
545
        Exif        map[string]string `json:"exif"`
546
}
547

548
func metadata(img *vips.Image, format vips.ImageType, stripExif bool) *Metadata {
1✔
549
        pages := 1
1✔
550
        if IsAnimationSupported(format) {
2✔
551
                pages = img.Height() / img.PageHeight()
1✔
552
        }
1✔
553
        if format == vips.ImageTypePdf {
2✔
554
                pages = img.Pages()
1✔
555
        }
1✔
556
        exif := map[string]string{}
1✔
557
        if !stripExif {
2✔
558
                exif = extractExif(img.Exif())
1✔
559
        }
1✔
560
        mimeType, _ := format.MimeType()
1✔
561
        return &Metadata{
1✔
562
                Format:      string(format),
1✔
563
                ContentType: mimeType,
1✔
564
                Width:       img.Width(),
1✔
565
                Height:      img.PageHeight(),
1✔
566
                Pages:       pages,
1✔
567
                Bands:       img.Bands(),
1✔
568
                Orientation: img.Orientation(),
1✔
569
                Exif:        exif,
1✔
570
        }
1✔
571
}
572

573
func supportedSaveFormat(format vips.ImageType) vips.ImageType {
1✔
574
        switch format {
1✔
575
        case vips.ImageTypePng, vips.ImageTypeWebp, vips.ImageTypeTiff, vips.ImageTypeGif, vips.ImageTypeAvif, vips.ImageTypeHeif, vips.ImageTypeJp2k:
1✔
576
                return format
1✔
577
        }
578
        return vips.ImageTypeJpeg
1✔
579
}
580

581
func (v *Processor) export(
582
        image *vips.Image, format vips.ImageType, compression int, quality int, palette bool, bitdepth int, stripMetadata bool,
583
) ([]byte, error) {
1✔
584
        // check resolution before export
1✔
585
        if _, err := v.CheckResolution(image, nil); err != nil {
1✔
586
                return nil, err
×
587
        }
×
588
        switch format {
1✔
589
        case vips.ImageTypePng:
1✔
590
                opts := &vips.PngsaveBufferOptions{
1✔
591
                        Q:           quality,
1✔
592
                        Palette:     palette,
1✔
593
                        Bitdepth:    bitdepth,
1✔
594
                        Compression: compression,
1✔
595
                }
1✔
596
                if stripMetadata {
2✔
597
                        opts.Keep = vips.KeepNone
1✔
598
                }
1✔
599
                return image.PngsaveBuffer(opts)
1✔
600
        case vips.ImageTypeWebp:
1✔
601
                opts := &vips.WebpsaveBufferOptions{
1✔
602
                        Q: quality,
1✔
603
                }
1✔
604
                if stripMetadata {
2✔
605
                        opts.Keep = vips.KeepNone
1✔
606
                }
1✔
607
                return image.WebpsaveBuffer(opts)
1✔
608
        case vips.ImageTypeTiff:
1✔
609
                opts := &vips.TiffsaveBufferOptions{
1✔
610
                        Q: quality,
1✔
611
                }
1✔
612
                if stripMetadata {
2✔
613
                        opts.Keep = vips.KeepNone
1✔
614
                }
1✔
615
                return image.TiffsaveBuffer(opts)
1✔
616
        case vips.ImageTypeGif:
1✔
617
                opts := &vips.GifsaveBufferOptions{}
1✔
618
                if stripMetadata {
2✔
619
                        opts.Keep = vips.KeepNone
1✔
620
                }
1✔
621
                return image.GifsaveBuffer(opts)
1✔
622
        case vips.ImageTypeAvif:
×
623
                opts := &vips.HeifsaveBufferOptions{
×
624
                        Q:           quality,
×
625
                        Compression: vips.HeifCompressionAv1,
×
626
                }
×
627
                if stripMetadata {
×
628
                        opts.Keep = vips.KeepNone
×
629
                }
×
630
                opts.Effort = 9 - v.AvifSpeed
×
631
                return image.HeifsaveBuffer(opts)
×
632
        case vips.ImageTypeHeif:
×
633
                opts := &vips.HeifsaveBufferOptions{
×
634
                        Q: quality,
×
635
                }
×
636
                if stripMetadata {
×
637
                        opts.Keep = vips.KeepNone
×
638
                }
×
639
                return image.HeifsaveBuffer(opts)
×
640
        case vips.ImageTypeJp2k:
×
641
                opts := &vips.Jp2ksaveBufferOptions{
×
642
                        Q: quality,
×
643
                }
×
644
                if stripMetadata {
×
645
                        opts.Keep = vips.KeepNone
×
646
                }
×
647
                return image.Jp2ksaveBuffer(opts)
×
648
        default:
1✔
649
                opts := &vips.JpegsaveBufferOptions{}
1✔
650
                if v.MozJPEG {
1✔
651
                        opts.Q = 75
×
652
                        opts.Keep = vips.KeepNone
×
653
                        opts.OptimizeCoding = true
×
654
                        opts.Interlace = true
×
655
                        opts.OptimizeScans = true
×
656
                        opts.TrellisQuant = true
×
657
                        opts.QuantTable = 3
×
658
                }
×
659
                if quality > 0 {
2✔
660
                        opts.Q = quality
1✔
661
                }
1✔
662
                if stripMetadata {
2✔
663
                        opts.Keep = vips.KeepNone
1✔
664
                }
1✔
665
                return image.JpegsaveBuffer(opts)
1✔
666
        }
667
}
668

669
func argSplit(r rune) bool {
1✔
670
        return r == 'x' || r == ',' || r == ':'
1✔
671
}
1✔
672

673
type focal struct {
674
        Left   float64
675
        Right  float64
676
        Top    float64
677
        Bottom float64
678
}
679

680
func parseFocalPoint(focalRects ...focal) (focalX, focalY float64) {
1✔
681
        var sumWeight float64
1✔
682
        for _, f := range focalRects {
2✔
683
                sumWeight += (f.Right - f.Left) * (f.Bottom - f.Top)
1✔
684
        }
1✔
685
        for _, f := range focalRects {
2✔
686
                r := (f.Right - f.Left) * (f.Bottom - f.Top) / sumWeight
1✔
687
                focalX += (f.Left + f.Right) / 2 * r
1✔
688
                focalY += (f.Top + f.Bottom) / 2 * r
1✔
689
        }
1✔
690
        return
1✔
691
}
692

693
func findTrim(
694
        _ context.Context, img *vips.Image, pos string, tolerance int,
695
) (l, t, w, h int, err error) {
1✔
696
        if isAnimated(img) {
2✔
697
                // skip animation support
1✔
698
                return
1✔
699
        }
1✔
700
        tmp, err := img.Copy(&vips.CopyOptions{Interpretation: vips.InterpretationSrgb})
1✔
701
        if err != nil {
1✔
NEW
702
                return
×
NEW
703
        }
×
704
        defer tmp.Close()
1✔
705
        if tmp.HasAlpha() {
2✔
706
                if err = tmp.Flatten(&vips.FlattenOptions{Background: []float64{255, 0, 255}}); err != nil {
1✔
NEW
707
                        return
×
NEW
708
                }
×
709
        }
710
        var x, y int
1✔
711
        if pos == imagorpath.TrimByBottomRight {
2✔
712
                x = tmp.Width() - 1
1✔
713
                y = tmp.PageHeight() - 1
1✔
714
        }
1✔
715
        if tolerance == 0 {
2✔
716
                tolerance = 1
1✔
717
        }
1✔
718
        background, err := tmp.Getpoint(x, y, nil)
1✔
719
        if err != nil {
1✔
720
                return
×
721
        }
×
722
        l, t, w, h, err = tmp.FindTrim(&vips.FindTrimOptions{
1✔
723
                Threshold:  float64(tolerance),
1✔
724
                Background: background,
1✔
725
        })
1✔
726
        return
1✔
727
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc