Product images
A catalogue needs the same photo at half a dozen sizes, on a consistent background, cropped the same way every time. One upload produces the whole set here, in a single pass, before anything is sent anywhere.
Try it
Recipe
One source, many outputs
Fit rather than Cover: a product must never be cropped, so it is
scaled to fit the padded area and centred on an ImageCanvas filled with the
catalogue background. (ResizeMode.Contain pads too, but it fits to the full canvas
first - fine for a letterbox, wrong for a margin.) Each size is a separate run against pixels
that were decoded once, which is the expensive part.
// Fit, never Cover: a product must never be cropped. It is scaled to fit the
// padded area and centred on a canvas of the catalogue background.
foreach (var size in sizes)
{
var inner = (int)(size * (1 - padding * 2));
using var fitted = await ImagePipeline.Create()
.AutoOrient()
.Resize(inner, inner, ResizeMode.Fit)
.Sharpen(0.25f)
.ExecuteAsync(decodedOnce);
using var canvas = ImageCanvas.Create(size, size, catalogueBackground);
canvas.DrawImage(fitted, new RectangleF(
(size - fitted.Width) / 2f, (size - fitted.Height) / 2f, fitted.Width, fitted.Height));
using var variant = canvas.Snapshot();
results.Add(await exporter.EncodeAsync(variant, exportOptions));
}
Or all in one pipeline
CanvasSize grows the canvas around the fitted product without scaling it, so the
margin can be a step of the pipeline itself - one value to store, reuse on a server, or save as
JSON. It fills only the area it adds: a product cut out on transparency stays transparent in WebP
or PNG, which is why this page composites onto a canvas instead, so the background shows through
the product's own transparent pixels too. For opaque photos the two are the same.
// Fit inside the padded area, then grow the canvas to the full size around it.
var tile = ImagePipeline.Create()
.AutoOrient()
.Resize(inner, inner, ResizeMode.Fit)
.Sharpen(0.25f)
.CanvasSize(size, size, Anchor.Center, catalogueBackground);
// Or a fixed border on every side, whatever the size.
var framed = ImagePipeline.Create().MaxSize(1000, 1000).Pad(40, catalogueBackground);