Files
slp-modular-cms/src/SlpModularCms.Core/Media/VariantPlanner.cs
T
SluijsensandClaude Sonnet 5.5 6999f5b2ba Hero-admin: schakelaar Actief, destructieve knoppen rood, slide-kopjes, juiste voorvertoning in het focusvenster, status per foto
- Schakelaar (role=switch) Actief / Niet actief bij de knoppen, gedimde rij; 'Verberg voor schermlezers'; veld 'Beschrijving' binnen een groep 'Slide N'
- Alle foto's zonder complete set tonen 'Wordt verwerkt' (voortgang bij de foto die bezig is, 'in de wachtrij' bij de rest); totaal bovenaan telt ook nog niet opgeslagen foto's
- Per foto 'Verwerking afgerond' (zelfde weergave voor een hergebruikte foto), foto direct bruikbaar; niet-opgeslagen foto's worden zelf ververst (GET /hero/images/{id}), ook in een achtergrondtab
- Focusvenster toont de hele foto (full-preview in elke set, achtergrond-aanvulling voor oude sets) in plaats van de uitgesneden wide-variant (vervorming)
- Prullenbak en 'Wijzigingen weggooien' in de destructieve stijl
- upload-respons: reused (intern)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 23:26:47 +01:00

86 lines
3.6 KiB
C#

namespace SlpModularCms.Core.Media;
/// <summary>Decides which files to make from an image: per ratio the crop and the widths, per width the formats. Pure.</summary>
public static class VariantPlanner
{
public const int WebPQuality = 80;
public const int AvifQuality = 50;
public const int JpegQuality = 82;
/// <summary>The widths per ratio of the first version of the hero (never above the source: see <see cref="Plan"/>).</summary>
public static IReadOnlyList<int> DefaultWidths(Ratio ratio) => ratio.Name switch
{
"wide" => [640, 960, 1280, 1600, 2560],
"tall" => [480, 640, 960, 1280],
_ => [640, 960, 1280],
};
/// <summary>
/// For every ratio: the crop around the focus, then the widths that are not wider than the crop (an image is never
/// scaled up); when the crop is narrower than the smallest wanted width, one variant at the crop width. Each width is
/// planned in WebP, and in AVIF when <paramref name="outputs"/> contains it. WebP is required: without WebP the
/// plan falls back to JPEG so a platform without a WebP encoder still publishes something.
/// </summary>
/// <summary>The ratio name of the full-frame preview (the whole picture, not cut around a point).</summary>
public const string PreviewRatioName = "full";
/// <summary>The widest full-frame preview.</summary>
public const int PreviewMaxWidth = 1280;
/// <summary>
/// The full-frame preview an administrator needs to choose the focus point: the WHOLE oriented picture at its own
/// shape, scaled down to at most <see cref="PreviewMaxWidth"/> (never up). Not published to websites.
/// </summary>
public static VariantSpec Preview(int sourceWidth, int sourceHeight, IReadOnlySet<ImageFormat> outputs)
{
var width = Math.Min(PreviewMaxWidth, sourceWidth);
var height = Math.Max(1, (int)Math.Round(width * (double)sourceHeight / sourceWidth));
var (format, quality) = outputs.Contains(ImageFormat.WebP) ? (ImageFormat.WebP, WebPQuality) : (ImageFormat.Jpeg, JpegQuality);
return new VariantSpec(new Ratio(PreviewRatioName, sourceWidth, sourceHeight), width, height, format, quality, new CropRect(0, 0, sourceWidth, sourceHeight));
}
public static IReadOnlyList<VariantSpec> Plan(
int sourceWidth,
int sourceHeight,
IReadOnlyList<RatioPlan> ratios,
IReadOnlySet<ImageFormat> outputs)
{
var formats = new List<(ImageFormat Format, int Quality)>();
if (outputs.Contains(ImageFormat.WebP))
{
formats.Add((ImageFormat.WebP, WebPQuality));
}
else
{
formats.Add((ImageFormat.Jpeg, JpegQuality));
}
if (outputs.Contains(ImageFormat.Avif))
{
formats.Add((ImageFormat.Avif, AvifQuality));
}
var result = new List<VariantSpec>();
foreach (var plan in ratios)
{
var crop = FocusMath.CropFor(sourceWidth, sourceHeight, plan.Ratio, plan.Focus);
var widths = plan.Widths.Where(w => w > 0 && w <= crop.Width).Distinct().OrderBy(w => w).ToList();
if (widths.Count == 0)
{
widths.Add(crop.Width);
}
foreach (var width in widths)
{
var height = Math.Max(1, (int)Math.Round(width * (double)plan.Ratio.Denominator / plan.Ratio.Numerator));
foreach (var (format, quality) in formats)
{
result.Add(new VariantSpec(plan.Ratio, width, height, format, quality, crop));
}
}
}
return result;
}
}