From c21656aff2d4d8ade73f5c8f897e7e9aacdae056 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Mon, 9 Aug 2021 14:52:07 +0200 Subject: [PATCH] Change processor order and fix crop percentage calculation --- .../UmbracoBuilder.ImageSharp.cs | 4 ++- .../ImageProcessors/CropWebProcessor.cs | 35 ++++++------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilder.ImageSharp.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilder.ImageSharp.cs index 3d753975fe..ec14091092 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilder.ImageSharp.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilder.ImageSharp.cs @@ -49,7 +49,9 @@ namespace Umbraco.Extensions }) .SetCache() .SetCacheHash() - .AddProcessor(); + .RemoveProcessor() + .AddProcessor() + .AddProcessor(); return services; } diff --git a/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs b/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs index c523d6e424..c51af9a532 100644 --- a/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs +++ b/src/Umbraco.Web.Common/ImageProcessors/CropWebProcessor.cs @@ -37,36 +37,23 @@ namespace Umbraco.Cms.Web.Common.ImageProcessors { if (GetCrop(commands, parser, culture) is RectangleF crop) { - Size size = image.Image.Size(); - CropMode mode = GetMode(commands, parser, culture); - - if (mode == CropMode.Percentage) + if (GetMode(commands, parser, culture) == CropMode.Percentage) { // Convert the percentage based model of left, top, right, bottom to x, y, width, height - float x = crop.Left * size.Width; - float y = crop.Top * size.Height; - float width = crop.Right < 1 ? (1 - crop.Left - crop.Right) * size.Width : size.Width; - float height = crop.Bottom < 1 ? (1 - crop.Top - crop.Bottom) * size.Height : size.Height; + int sourceWidth = image.Image.Width; + int sourceHeight = image.Image.Height; - crop = new RectangleF(x, y, width, height); + 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); } - // Round and validate/clamp crop rectangle var cropRectangle = Rectangle.Round(crop); - if (cropRectangle.X < size.Width && cropRectangle.Y < size.Height) - { - if (cropRectangle.Width > (size.Width - cropRectangle.X)) - { - cropRectangle.Width = size.Width - cropRectangle.X; - } - - if (cropRectangle.Height > (size.Height - cropRectangle.Y)) - { - cropRectangle.Height = size.Height - cropRectangle.Y; - } - - image.Image.Mutate(x => x.Crop(cropRectangle)); - } + + image.Image.Mutate(x => x.Crop(cropRectangle)); } return image;