loadOverlayImage loads a watermark overlay image using the image cache. Cache key is image path only. Size must be known (w > 0 && h > 0) to use the cache; unknown size or size exceeding cache max dims bypasses the cache.
( ctx context.Context, load imagor.LoadFunc, url string, w, h, n int, size vips.Size, )
| 15 | // Cache key is image path only. Size must be known (w > 0 && h > 0) to use the cache; |
| 16 | // unknown size or size exceeding cache max dims bypasses the cache. |
| 17 | func (v *Processor) loadOverlayImage( |
| 18 | ctx context.Context, load imagor.LoadFunc, |
| 19 | url string, w, h, n int, size vips.Size, |
| 20 | ) (*vips.Image, error) { |
| 21 | sizeKnown := w > 0 && h > 0 |
| 22 | |
| 23 | // Unknown size or cache disabled: load directly. |
| 24 | if !sizeKnown || v.cache == nil { |
| 25 | blob, err := load(url) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | if sizeKnown { |
| 30 | return v.NewThumbnail(ctx, blob, w, h, vips.InterestingNone, size, n, 1, 0) |
| 31 | } |
| 32 | return v.NewThumbnail(ctx, blob, v.MaxWidth, v.MaxHeight, vips.InterestingNone, vips.SizeDown, n, 1, 0) |
| 33 | } |
| 34 | |
| 35 | // Size exceeds cache max dims — bypass cache, load directly at requested size. |
| 36 | if w > v.CacheMaxWidth || h > v.CacheMaxHeight { |
| 37 | blob, err := load(url) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | return v.NewThumbnail(ctx, blob, w, h, vips.InterestingNone, size, n, 1, 0) |
| 42 | } |
| 43 | |
| 44 | // Cache hit — serve from cached memory blob without loading. |
| 45 | if memBlob, ok := v.cache.Get(url); ok { |
| 46 | return v.NewThumbnail(ctx, memBlob, w, h, vips.InterestingNone, size, 1, 1, 0) |
| 47 | } |
| 48 | |
| 49 | // Cache miss: fetch and decode inside the singleflight to deduplicate concurrent |
| 50 | // network requests. load is passed so loadOrCache can call it inside the singleflight. |
| 51 | memBlob, origBlob, err := v.loadOrCache(nil, url, n, load) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | // Animated source — origBlob is set; fall back to direct thumbnail at w×h. |
| 57 | if origBlob != nil { |
| 58 | return v.NewThumbnail(ctx, origBlob, w, h, vips.InterestingNone, size, n, 1, 0) |
| 59 | } |
| 60 | |
| 61 | if memBlob == nil { |
| 62 | // Cache disabled or blob could not be loaded — should not happen here. |
| 63 | return nil, imagor.ErrNotFound |
| 64 | } |
| 65 | |
| 66 | // Static: resize from cached memory blob to requested w×h. |
| 67 | return v.NewThumbnail(ctx, memBlob, w, h, vips.InterestingNone, size, 1, 1, 0) |
| 68 | } |
| 69 | |
| 70 | // loadFilterImage runs the imagor pipeline for an image() filter using the image cache. |
| 71 | // Bypasses cache for unknown size, exceeds max dims, or requests that depend on |