diff --git a/src/Umbraco.Infrastructure/Media/ImageSharpImageUrlGenerator.cs b/src/Umbraco.Infrastructure/Media/ImageSharpImageUrlGenerator.cs index b6906d304b..12e85dfb88 100644 --- a/src/Umbraco.Infrastructure/Media/ImageSharpImageUrlGenerator.cs +++ b/src/Umbraco.Infrastructure/Media/ImageSharpImageUrlGenerator.cs @@ -42,8 +42,7 @@ namespace Umbraco.Cms.Infrastructure.Media if (options.Crop != null) { - AddQueryString("crop", options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom); - AddQueryString("cropmode", "percentage"); + AddQueryString("cc", options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom); } if (options.ImageCropMode.HasValue) diff --git a/src/Umbraco.Web.Common/ImageProcessors/CropMode.cs b/src/Umbraco.Web.Common/ImageProcessors/CropMode.cs deleted file mode 100644 index 6e08c6e05c..0000000000 --- a/src/Umbraco.Web.Common/ImageProcessors/CropMode.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Umbraco.Cms.Web.Common.ImageProcessors -{ - /// - /// Represents the mode used to calculate a crop. - /// - public enum CropMode - { - /// - /// Crops the image using the standard rectangle model of x, y, width, height. - /// - Pixels, - - /// - /// Crops the image using the percentages model of left, top, right, bottom. - /// - Percentage - } -} diff --git a/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs b/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs index c51af9a532..8945ce7f45 100644 --- a/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs +++ b/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Globalization; using Microsoft.Extensions.Logging; @@ -15,43 +16,31 @@ namespace Umbraco.Cms.Web.Common.ImageProcessors public class CropWebProcessor : IImageWebProcessor { /// - /// The command constant for the crop definition. + /// The command constant for the crop coordinates. /// - public const string Crop = "crop"; - - /// - /// The command constant for the crop mode. - /// - public const string Mode = "cropmode"; - + public const string Coordinates = "cc"; /// public IEnumerable Commands { get; } = new[] { - Crop, - Mode + Coordinates }; /// public FormattedImage Process(FormattedImage image, ILogger logger, IDictionary commands, CommandParser parser, CultureInfo culture) { - if (GetCrop(commands, parser, culture) is RectangleF crop) + RectangleF? coordinates = GetCoordinates(commands, parser, culture); + if (coordinates != null) { - if (GetMode(commands, parser, culture) == CropMode.Percentage) - { - // Convert the percentage based model of left, top, right, bottom to x, y, width, height - int sourceWidth = image.Image.Width; - int sourceHeight = image.Image.Height; + // Convert the percentage based model of left, top, right, bottom to x, y, width, height + int sourceWidth = image.Image.Width; + int sourceHeight = image.Image.Height; + int x = (int)MathF.Round(coordinates.Value.Left * sourceWidth); + int y = (int)MathF.Round(coordinates.Value.Top * sourceHeight); + int width = sourceWidth - (int)MathF.Round(coordinates.Value.Right * sourceWidth); + int height = sourceHeight - (int)MathF.Round(coordinates.Value.Bottom * sourceHeight); - float left = crop.Left * sourceWidth; - float top = crop.Top * sourceHeight; - float width = sourceWidth - (sourceWidth * crop.Width) - left; - float height = sourceHeight - (sourceHeight * crop.Height) - top; - - crop = new RectangleF(left, top, width, height); - } - - var cropRectangle = Rectangle.Round(crop); + var cropRectangle = new Rectangle(x, y, width, height); image.Image.Mutate(x => x.Crop(cropRectangle)); } @@ -59,9 +48,9 @@ namespace Umbraco.Cms.Web.Common.ImageProcessors return image; } - private static RectangleF? GetCrop(IDictionary commands, CommandParser parser, CultureInfo culture) + private static RectangleF? GetCoordinates(IDictionary commands, CommandParser parser, CultureInfo culture) { - float[] coordinates = parser.ParseValue(commands.GetValueOrDefault(Crop), culture); + float[] coordinates = parser.ParseValue(commands.GetValueOrDefault(Coordinates), culture); if (coordinates.Length != 4) { @@ -70,8 +59,5 @@ namespace Umbraco.Cms.Web.Common.ImageProcessors return new RectangleF(coordinates[0], coordinates[1], coordinates[2], coordinates[3]); } - - private static CropMode GetMode(IDictionary commands, CommandParser parser, CultureInfo culture) - => parser.ParseValue(commands.GetValueOrDefault(Mode), culture); } }