H1: beeldpijplijn in Core.Media (Magick.NET achter IImageProcessor), pure functies met property-tests, uploadwacht
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
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>
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user