Change processor order and fix crop percentage calculation
This commit is contained in:
@@ -49,7 +49,9 @@ namespace Umbraco.Extensions
|
||||
})
|
||||
.SetCache<PhysicalFileSystemCache>()
|
||||
.SetCacheHash<CacheHash>()
|
||||
.AddProcessor<CropWebProcessor>();
|
||||
.RemoveProcessor<ResizeWebProcessor>()
|
||||
.AddProcessor<CropWebProcessor>()
|
||||
.AddProcessor<ResizeWebProcessor>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user