Change processor order and fix crop percentage calculation

This commit is contained in:
Ronald Barendse
2021-08-09 14:52:07 +02:00
parent 5cdcd021fa
commit c21656aff2
2 changed files with 14 additions and 25 deletions

View File

@@ -49,7 +49,9 @@ namespace Umbraco.Extensions
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheHash<CacheHash>()
.AddProcessor<CropWebProcessor>();
.RemoveProcessor<ResizeWebProcessor>()
.AddProcessor<CropWebProcessor>()
.AddProcessor<ResizeWebProcessor>();
return services;
}

View File

@@ -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;