default commands for image presets

This commit is contained in:
2024-03-19 13:25:39 +01:00
parent 23704b6cef
commit b63b75ce57
3 changed files with 31 additions and 13 deletions
+23 -8
View File
@@ -54,14 +54,26 @@ public sealed class PresetRequestParser : IRequestParser
return [];
}
FallbackFormat fallbackFormat = FindFallbackFormatInContext(context);
// get default processing commands (which are added to each request) from options
Dictionary<string, string> defaults = _options.ImageSharp.DefaultCommands.Select(x => x.Split("=", 2)).ToDictionary(x => x[0], x => x[1]);
// fall back to webp, as avif is not supported yet by ImageSharp
string format = fallbackFormat != FallbackFormat.None ? "webp" : null;
string quality = _options.ImageSharp.DefaultQuality.ToString();
FallbackFormat fallbackFormat = FindFallbackFormatInContext(context);
string format = defaults.GetValueOrDefault("format", fallbackFormat != FallbackFormat.None ? "webp" : null);
string quality = defaults.GetValueOrDefault("quality");
CommandCollection filters = [];
foreach (KeyValuePair<string, string> kv in defaults)
{
string key = kv.Key;
if (key != "format" && key != "quality")
{
filters[key] = kv.Value;
}
}
foreach (string[] kv in transformed.Select(x => x.Split("=", 2)))
{
string key = kv[0];
@@ -79,25 +91,28 @@ public sealed class PresetRequestParser : IRequestParser
continue;
}
filters.Add(key, value);
filters[key] = value;
}
// add positioning filters
if (focalPoint != null)
{
filters.Add("rxy", focalPoint);
filters["rxy"] = focalPoint;
}
else
{
filters.Add("ranchor", "center");
filters["ranchor"] = "center";
}
// add format/quality filters
if (format.HasValue())
{
filters.Add("format", format);
filters["format"] = format;
}
if (quality.HasValue())
{
filters["quality"] = quality;
}
filters.Add("quality", quality);
return filters;
}
+2 -2
View File
@@ -25,9 +25,9 @@ public class ImageSharpOptions
public PhysicalFileSystemCacheOptions Cache { get; set; } = new();
public Dictionary<string, string[]> Presets { get; set; } = new();
public Dictionary<string, string[]> Presets { get; set; } = [];
public int DefaultQuality { get; set; } = 75;
public string[] DefaultCommands { get; set; } = [];
}
+6 -3
View File
@@ -23,6 +23,7 @@ internal class ZeroMediaModule : ZeroModule
.ClearProviders()
.AddProvider<PhysicalFileProvider>()
.AddProcessor<RotateWebProcessor>()
.AddProcessor<StripMetadataWebProcessor>()
.Configure<PhysicalFileSystemCacheOptions>(configuration.GetSection("Zero:Media:ImageSharp:Cache"));
//configuration.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Config/imaging.json"), true, true);
@@ -32,15 +33,17 @@ internal class ZeroMediaModule : ZeroModule
{
IOptions<MediaOptions> options = svc.GetRequiredService<IOptions<MediaOptions>>();
IWebHostEnvironment env = svc.GetRequiredService<IWebHostEnvironment>();
Console.WriteLine("media path: " + options.Value.FolderPath);
return new(Path.Combine(env.WebRootPath, options.Value.FolderPath), options.Value.PublicPathPrefix + options.Value.FolderPath.EnsureStartsWith('/'));
});
services.AddScoped<IMediaCreator, MediaCreator>();
services.AddScoped<IMediaManagement, MediaManagement>();
services.AddScoped<IMediaMetadataCache, MediaMetadataCache>();
services.AddScoped<IZeroMediaStoreDbProvider, EmptyZeroMediaStoreDbProvider>();
services.AddSingleton<IImageDimensionReader, ImageDimensionReader>();
services.AddSingleton<MediaMetadataCache>();
services.AddOptions<MediaOptions>().Bind(configuration.GetSection("Zero:Media")).Configure(opts =>
services.AddOptions<MediaOptions>().Configure(opts =>
{
opts.FolderPath = "media";
opts.AllowedOtherFileExtensions = new() { ".pdf", ".docx", ".doc", ".svg", ".xml" };
@@ -50,7 +53,7 @@ internal class ZeroMediaModule : ZeroModule
// { "thumb", new ResizeOptions() { Size = new(100, 100), Mode = ResizeMode.Max } },
// { "preview", new ResizeOptions() { Size = new(210, 210), Mode = ResizeMode.Min } }
// };
});
}).Bind(configuration.GetSection("Zero:Media"));
// services.Configure<ZeroOptions>(opts =>
// {