Merge pull request #10623 from umbraco/v9/bugfix/image-cropping

V9: Fix ImageSharp integration and add support for custom crops
This commit is contained in:
Bjarke Berg
2021-08-17 07:49:32 +02:00
committed by GitHub
27 changed files with 795 additions and 826 deletions

View File

@@ -0,0 +1,30 @@
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Web.Middleware;
namespace Umbraco.Cms.Web.Common.DependencyInjection
{
/// <summary>
/// Configures the ImageSharp middleware options to use the registered configuration.
/// </summary>
/// <seealso cref="Microsoft.Extensions.Options.IConfigureOptions&lt;SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddlewareOptions&gt;" />
public sealed class ImageSharpConfigurationOptions : IConfigureOptions<ImageSharpMiddlewareOptions>
{
/// <summary>
/// The ImageSharp configuration.
/// </summary>
private readonly Configuration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="ImageSharpConfigurationOptions" /> class.
/// </summary>
/// <param name="configuration">The ImageSharp configuration.</param>
public ImageSharpConfigurationOptions(Configuration configuration) => _configuration = configuration;
/// <summary>
/// Invoked to configure a <typeparamref name="TOptions" /> instance.
/// </summary>
/// <param name="options">The options instance to configure.</param>
public void Configure(ImageSharpMiddlewareOptions options) => options.Configuration = _configuration;
}
}

View File

@@ -2,14 +2,16 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SixLabors.ImageSharp.Memory;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.DependencyInjection;
using SixLabors.ImageSharp.Web.Middleware;
using SixLabors.ImageSharp.Web.Processors;
using SixLabors.ImageSharp.Web.Providers;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Cms.Web.Common.ImageProcessors;
namespace Umbraco.Extensions
{
@@ -20,56 +22,44 @@ namespace Umbraco.Extensions
/// </summary>
public static IServiceCollection AddUmbracoImageSharp(this IUmbracoBuilder builder)
{
IConfiguration configuration = builder.Config;
IServiceCollection services = builder.Services;
ImagingSettings imagingSettings = configuration.GetSection(Cms.Core.Constants.Configuration.ConfigImaging)
ImagingSettings imagingSettings = builder.Config.GetSection(Cms.Core.Constants.Configuration.ConfigImaging)
.Get<ImagingSettings>() ?? new ImagingSettings();
services.AddImageSharp(options =>
builder.Services.AddImageSharp(options =>
{
options.Configuration = SixLabors.ImageSharp.Configuration.Default;
// The configuration is set using ImageSharpConfigurationOptions
options.BrowserMaxAge = imagingSettings.Cache.BrowserMaxAge;
options.CacheMaxAge = imagingSettings.Cache.CacheMaxAge;
options.CachedNameLength = imagingSettings.Cache.CachedNameLength;
// Use configurable maximum width and height (overwrite ImageSharps default)
options.OnParseCommandsAsync = context =>
{
RemoveIntParamenterIfValueGreatherThen(context.Commands, ResizeWebProcessor.Width, imagingSettings.Resize.MaxWidth);
RemoveIntParamenterIfValueGreatherThen(context.Commands, ResizeWebProcessor.Height, imagingSettings.Resize.MaxHeight);
if (context.Commands.Count == 0)
{
return Task.CompletedTask;
}
uint width = context.Parser.ParseValue<uint>(context.Commands.GetValueOrDefault(ResizeWebProcessor.Width), context.Culture);
uint height = context.Parser.ParseValue<uint>(context.Commands.GetValueOrDefault(ResizeWebProcessor.Height), context.Culture);
if (width > imagingSettings.Resize.MaxWidth || height > imagingSettings.Resize.MaxHeight)
{
context.Commands.Remove(ResizeWebProcessor.Width);
context.Commands.Remove(ResizeWebProcessor.Height);
}
return Task.CompletedTask;
};
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
options.OnProcessedAsync = _ => Task.CompletedTask;
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
})
.SetRequestParser<QueryCollectionRequestParser>()
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheFolder = imagingSettings.Cache.CacheFolder;
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheHash<CacheHash>()
.AddProvider<PhysicalFileSystemProvider>()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>();
.Configure<PhysicalFileSystemCacheOptions>(options => options.CacheFolder = imagingSettings.Cache.CacheFolder)
// We need to add CropWebProcessor before ResizeWebProcessor (until https://github.com/SixLabors/ImageSharp.Web/issues/182 is fixed)
.RemoveProcessor<ResizeWebProcessor>()
.AddProcessor<CropWebProcessor>()
.AddProcessor<ResizeWebProcessor>();
return services;
}
builder.Services.AddTransient<IConfigureOptions<ImageSharpMiddlewareOptions>, ImageSharpConfigurationOptions>();
private static void RemoveIntParamenterIfValueGreatherThen(IDictionary<string, string> commands, string parameter, int maxValue)
{
if (commands.TryGetValue(parameter, out var command))
{
if (int.TryParse(command, out var i))
{
if (i > maxValue)
{
commands.Remove(parameter);
}
}
}
return builder.Services;
}
}
}