MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / scaleOnLoad

Method scaleOnLoad

processing/scale_on_load.go:43–156  ·  view source on GitHub ↗
(c *Context)

Source from the content-addressed store, hash-verified

41}
42
43func (p *Processor) scaleOnLoad(c *Context) error {
44 // Get the preshrink value based on the requested scales.
45 // We calculate it based on the image dimensions that we would get
46 // with the current scales.
47 // We can't just use c.WScale and c.HScale since this may lead to
48 // overshrinking when only one target dimension is set.
49 wshrink := float64(c.SrcWidth) / float64(imath.Scale(c.SrcWidth, c.WScale))
50 hshrink := float64(c.SrcHeight) / float64(imath.Scale(c.SrcHeight, c.HScale))
51 preshrink := min(wshrink, hshrink)
52
53 // For vector images, apply the vector base shrink.
54 // We might set it in the [Processor.vectorGuardScale] step in case the image
55 // is too large.
56 if c.ImgData != nil && c.ImgData.Format().IsVector() {
57 preshrink *= c.VectorBaseShrink
58 }
59
60 // Check if we can and should scale the image on load
61 if !p.canScaleOnLoad(c, preshrink) {
62 return nil
63 }
64
65 // We will load the prescaled image into this new image.
66 // On success, we will swap it with the original image in the context,
67 // so we can safely clear it on function exit.
68 newImg := new(vips.Image)
69 defer newImg.Clear()
70
71 loadThumbnail := c.ImgData.Format().SupportsThumbnail()
72
73 if loadThumbnail {
74 // If the image supports embedded thumbnails, try to load it
75 if err := newImg.LoadThumbnail(c.ImgData); err != nil {
76 slog.Debug("Can't load thumbnail: %s", "error", err)
77 return nil
78 }
79 } else {
80 // JPEG shrink-on-load must be 1, 2, 4 or 8.
81 // We need to normalize it before passing to libvips.
82 // For other formats, we can pass any float value.
83 if c.ImgData.Format() == imagetype.JPEG {
84 preshrink = calcJpegShink(preshrink)
85 }
86
87 // if preshrink is 1, we can skip reloading the image
88 if preshrink == 1 {
89 return nil
90 }
91
92 // Reload the image with preshrink
93 if err := newImg.Load(c.ImgData, preshrink, 0, 1); err != nil {
94 return err
95 }
96 }
97
98 // Get the geometry of the preshrunk image
99 newWidth, newHeight, newAngle, newFlip := ExtractGeometry(
100 newImg, c.PO.Rotate(), c.PO.AutoRotate(),

Callers

nothing calls this directly

Calls 14

canScaleOnLoadMethod · 0.95
ScaleFunction · 0.92
ShrinkFunction · 0.92
calcJpegShinkFunction · 0.85
ExtractGeometryFunction · 0.85
IsVectorMethod · 0.80
ClearMethod · 0.80
SupportsThumbnailMethod · 0.80
LoadThumbnailMethod · 0.80
LoadMethod · 0.80
AutoRotateMethod · 0.80
SwapMethod · 0.80

Tested by

no test coverage detected