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

cshum / imagor / 15233893740

25 May 2025 03:47AM UTC coverage: 92.118% (+2.4%) from 89.678%
15233893740

Pull #544

github

cshum
update exif extraction
Pull Request #544: feat: introducing vipsgen

308 of 346 new or added lines in 6 files covered. (89.02%)

6 existing lines in 1 file now uncovered.

4862 of 5278 relevant lines covered (92.12%)

1.1 hits per line

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

85.23
/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
// Color represents an RGB
32
type Color struct {
33
        R, G, B float64
34
}
35

36
// ColorRGBA represents an RGB with alpha channel (A)
37
type ColorRGBA struct {
38
        R, G, B, A float64
39
}
40

41
// IsAnimationSupported indicates if image type supports animation
42
func IsAnimationSupported(imageType vips.ImageType) bool {
1✔
43
        return imageType == vips.ImageTypeGif || imageType == vips.ImageTypeWebp
1✔
44
}
1✔
45

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

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

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

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

546
// Metadata image attributes
547
type Metadata struct {
548
        Format      string         `json:"format"`
549
        ContentType string         `json:"content_type"`
550
        Width       int            `json:"width"`
551
        Height      int            `json:"height"`
552
        Orientation int            `json:"orientation"`
553
        Pages       int            `json:"pages"`
554
        Bands       int            `json:"bands"`
555
        Exif        map[string]any `json:"exif"`
556
}
557

558
func metadata(img *vips.Image, format vips.ImageType, stripExif bool) *Metadata {
1✔
559
        pages := 1
1✔
560
        if IsAnimationSupported(format) {
2✔
561
                pages = img.Height() / img.PageHeight()
1✔
562
        }
1✔
563
        if format == vips.ImageTypePdf {
2✔
564
                pages = img.Pages()
1✔
565
        }
1✔
566
        exif := map[string]any{}
1✔
567
        if !stripExif {
2✔
568
                exif = extractExif(img.Exif())
1✔
569
        }
1✔
570
        mimeType, _ := format.MimeType()
1✔
571
        return &Metadata{
1✔
572
                Format:      string(format),
1✔
573
                ContentType: mimeType,
1✔
574
                Width:       img.Width(),
1✔
575
                Height:      img.PageHeight(),
1✔
576
                Pages:       pages,
1✔
577
                Bands:       img.Bands(),
1✔
578
                Orientation: img.Orientation(),
1✔
579
                Exif:        exif,
1✔
580
        }
1✔
581
}
582

583
func supportedSaveFormat(format vips.ImageType) vips.ImageType {
1✔
584
        switch format {
1✔
585
        case vips.ImageTypePng, vips.ImageTypeWebp, vips.ImageTypeTiff, vips.ImageTypeGif, vips.ImageTypeAvif, vips.ImageTypeHeif, vips.ImageTypeJp2k:
1✔
586
                return format
1✔
587
        }
588
        return vips.ImageTypeJpeg
1✔
589
}
590

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

675
func argSplit(r rune) bool {
1✔
676
        return r == 'x' || r == ',' || r == ':'
1✔
677
}
1✔
678

679
type focal struct {
680
        Left   float64
681
        Right  float64
682
        Top    float64
683
        Bottom float64
684
}
685

686
func parseFocalPoint(focalRects ...focal) (focalX, focalY float64) {
1✔
687
        var sumWeight float64
1✔
688
        for _, f := range focalRects {
2✔
689
                sumWeight += (f.Right - f.Left) * (f.Bottom - f.Top)
1✔
690
        }
1✔
691
        for _, f := range focalRects {
2✔
692
                r := (f.Right - f.Left) * (f.Bottom - f.Top) / sumWeight
1✔
693
                focalX += (f.Left + f.Right) / 2 * r
1✔
694
                focalY += (f.Top + f.Bottom) / 2 * r
1✔
695
        }
1✔
696
        return
1✔
697
}
698

699
func findTrim(
700
        _ context.Context, img *vips.Image, pos string, tolerance int,
701
) (l, t, w, h int, err error) {
1✔
702
        if isAnimated(img) {
2✔
703
                // skip animation support
1✔
704
                return
1✔
705
        }
1✔
706
        var x, y int
1✔
707
        if pos == imagorpath.TrimByBottomRight {
2✔
708
                x = img.Width() - 1
1✔
709
                y = img.PageHeight() - 1
1✔
710
        }
1✔
711
        if tolerance == 0 {
2✔
712
                tolerance = 1
1✔
713
        }
1✔
714
        background, err := img.Getpoint(x, y, nil)
1✔
715
        if err != nil {
1✔
NEW
716
                return
×
NEW
717
        }
×
718
        l, t, w, h, err = img.FindTrim(&vips.FindTrimOptions{
1✔
719
                Threshold:  float64(tolerance),
1✔
720
                Background: background,
1✔
721
        })
1✔
722
        return
1✔
723
}
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