Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Middleware;
using SixLabors.ImageSharp.Web.Processors;
using Umbraco.Cms.Core.Configuration.Models;

namespace Umbraco.Cms.Imaging.ImageSharp.V2;

/// <summary>
/// Configures the ImageSharp middleware options.
/// </summary>
/// <seealso cref="IConfigureOptions{ImageSharpMiddlewareOptions}" />
public sealed class ConfigureImageSharpMiddlewareOptions : IConfigureOptions<ImageSharpMiddlewareOptions>
{
private readonly Configuration _configuration;
private readonly ImagingSettings _imagingSettings;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureImageSharpMiddlewareOptions" /> class.
/// </summary>
/// <param name="configuration">The ImageSharp configuration.</param>
/// <param name="imagingSettings">The Umbraco imaging settings.</param>
public ConfigureImageSharpMiddlewareOptions(Configuration configuration, IOptions<ImagingSettings> imagingSettings)
{
_configuration = configuration;
_imagingSettings = imagingSettings.Value;
}

/// <inheritdoc />
public void Configure(ImageSharpMiddlewareOptions options)
{
options.Configuration = _configuration;

options.BrowserMaxAge = _imagingSettings.Cache.BrowserMaxAge;
options.CacheMaxAge = _imagingSettings.Cache.CacheMaxAge;
options.CacheHashLength = _imagingSettings.Cache.CacheHashLength;

// Use configurable maximum width and height
options.OnParseCommandsAsync = context =>
{
if (context.Commands.Count == 0)
{
return Task.CompletedTask;
}

var width = context.Parser.ParseValue<int>(
context.Commands.GetValueOrDefault(ResizeWebProcessor.Width),
context.Culture);
if (width <= 0 || width > _imagingSettings.Resize.MaxWidth)
{
context.Commands.Remove(ResizeWebProcessor.Width);
}

var height = context.Parser.ParseValue<int>(
context.Commands.GetValueOrDefault(ResizeWebProcessor.Height),
context.Culture);
if (height <= 0 || height > _imagingSettings.Resize.MaxHeight)
{
context.Commands.Remove(ResizeWebProcessor.Height);
}

return Task.CompletedTask;
};

// Change Cache-Control header when cache buster value is present
options.OnPrepareResponseAsync = context =>
{
if (context.Request.Query.ContainsKey("rnd") || context.Request.Query.ContainsKey("v"))
{
ResponseHeaders headers = context.Response.GetTypedHeaders();

CacheControlHeaderValue cacheControl =
headers.CacheControl ?? new CacheControlHeaderValue { Public = true };
cacheControl.MustRevalidate = false; // ImageSharp enables this by default
cacheControl.Extensions.Add(new NameValueHeaderValue("immutable"));

headers.CacheControl = cacheControl;
}

return Task.CompletedTask;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Web.Caching;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Extensions;

namespace Umbraco.Cms.Imaging.ImageSharp.V2;

/// <summary>
/// Configures the ImageSharp physical file system cache options.
/// </summary>
/// <seealso cref="IConfigureOptions{PhysicalFileSystemCacheOptions}" />
public sealed class ConfigurePhysicalFileSystemCacheOptions : IConfigureOptions<PhysicalFileSystemCacheOptions>
{
private readonly IHostEnvironment _hostEnvironment;
private readonly ImagingSettings _imagingSettings;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurePhysicalFileSystemCacheOptions" /> class.
/// </summary>
/// <param name="imagingSettings">The Umbraco imaging settings.</param>
/// <param name="hostEnvironment">The host environment.</param>
public ConfigurePhysicalFileSystemCacheOptions(
IOptions<ImagingSettings> imagingSettings,
IHostEnvironment hostEnvironment)
{
_imagingSettings = imagingSettings.Value;
_hostEnvironment = hostEnvironment;
}

/// <inheritdoc />
public void Configure(PhysicalFileSystemCacheOptions options)
{
options.CacheFolder = _hostEnvironment.MapPathContentRoot(_imagingSettings.Cache.CacheFolder);
options.CacheFolderDepth = _imagingSettings.Cache.CacheFolderDepth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Globalization;
using System.Numerics;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Web;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Processors;

namespace Umbraco.Cms.Imaging.ImageSharp.V2.ImageProcessors;

/// <summary>
/// Allows the cropping of images.
/// </summary>
/// <seealso cref="SixLabors.ImageSharp.Web.Processors.IImageWebProcessor" />
public class CropWebProcessor : IImageWebProcessor
{
/// <summary>
/// The command constant for the crop coordinates.
/// </summary>
public const string Coordinates = "cc";

/// <summary>
/// The command constant for the resize orientation handling mode.
/// </summary>
public const string Orient = "orient";

/// <inheritdoc />
public IEnumerable<string> Commands { get; } = new[] { Coordinates, Orient };

/// <inheritdoc />
public FormattedImage Process(FormattedImage image, ILogger logger, CommandCollection commands, CommandParser parser, CultureInfo culture)
{
Rectangle? cropRectangle = GetCropRectangle(image, commands, parser, culture);
if (cropRectangle.HasValue)
{
image.Image.Mutate(x => x.Crop(cropRectangle.Value));
}

return image;
}

/// <inheritdoc />
public bool RequiresTrueColorPixelFormat(CommandCollection commands, CommandParser parser, CultureInfo culture) =>
false;

private static Rectangle? GetCropRectangle(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
{
var coordinates = parser.ParseValue<float[]>(commands.GetValueOrDefault(Coordinates), culture);
if (coordinates.Length != 4 ||
(coordinates[0] == 0 && coordinates[1] == 0 && coordinates[2] == 0 && coordinates[3] == 0))
{
return null;
}

// The right and bottom values are actually the distance from those sides, so convert them into real coordinates and transform to correct orientation
var left = Math.Clamp(coordinates[0], 0, 1);
var top = Math.Clamp(coordinates[1], 0, 1);
var right = Math.Clamp(1 - coordinates[2], 0, 1);
var bottom = Math.Clamp(1 - coordinates[3], 0, 1);
var orientation = GetExifOrientation(image, commands, parser, culture);
Vector2 xy1 = ExifOrientationUtilities.Transform(new Vector2(left, top), Vector2.Zero, Vector2.One, orientation);
Vector2 xy2 = ExifOrientationUtilities.Transform(new Vector2(right, bottom), Vector2.Zero, Vector2.One, orientation);

// Scale points to a pixel based rectangle
Size size = image.Image.Size();

return Rectangle.Round(RectangleF.FromLTRB(
MathF.Min(xy1.X, xy2.X) * size.Width,
MathF.Min(xy1.Y, xy2.Y) * size.Height,
MathF.Max(xy1.X, xy2.X) * size.Width,
MathF.Max(xy1.Y, xy2.Y) * size.Height));
}

private static ushort GetExifOrientation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
{
if (commands.Contains(Orient) && !parser.ParseValue<bool>(commands.GetValueOrDefault(Orient), culture))
{
return ExifOrientationMode.Unknown;
}

image.TryGetExifOrientation(out var orientation);

return orientation;
}
}
16 changes: 16 additions & 0 deletions src/Umbraco.Cms.Imaging.ImageSharp.V2/ImageSharpComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Extensions;

namespace Umbraco.Cms.Imaging.ImageSharp.V2;

/// <summary>
/// Adds imaging support using ImageSharp/ImageSharp.Web.
/// </summary>
/// <seealso cref="Umbraco.Cms.Core.Composing.IComposer" />
public sealed class ImageSharpComposer : IComposer
{
/// <inheritdoc />
public void Compose(IUmbracoBuilder builder)
=> builder.AddUmbracoImageSharp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using Umbraco.Cms.Core.Media;
using Size = System.Drawing.Size;

namespace Umbraco.Cms.Imaging.ImageSharp.V2.Media;

public sealed class ImageSharpDimensionExtractor : IImageDimensionExtractor
{
private readonly Configuration _configuration;

/// <inheritdoc />
public IEnumerable<string> SupportedImageFileTypes { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ImageSharpDimensionExtractor" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public ImageSharpDimensionExtractor(Configuration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));

SupportedImageFileTypes = configuration.ImageFormats.SelectMany(f => f.FileExtensions).ToArray();
}

/// <inheritdoc />
public Size? GetDimensions(Stream? stream)
{
Size? size = null;

IImageInfo imageInfo = Image.Identify(_configuration, stream);
if (imageInfo != null)
{
size = IsExifOrientationRotated(imageInfo)
? new Size(imageInfo.Height, imageInfo.Width)
: new Size(imageInfo.Width, imageInfo.Height);
}

return size;
}

private static bool IsExifOrientationRotated(IImageInfo imageInfo)
=> GetExifOrientation(imageInfo) switch
{
ExifOrientationMode.LeftTop
or ExifOrientationMode.RightTop
or ExifOrientationMode.RightBottom
or ExifOrientationMode.LeftBottom => true,
_ => false,
};

private static ushort GetExifOrientation(IImageInfo imageInfo)
{
IExifValue<ushort>? orientation = imageInfo.Metadata.ExifProfile?.GetValue(ExifTag.Orientation);
if (orientation is not null)
{
if (orientation.DataType == ExifDataType.Short)
{
return orientation.Value;
}

return Convert.ToUInt16(orientation.Value);
}

return ExifOrientationMode.Unknown;
}
}
Loading