v10: Update to ImageSharp v2 (#12185)
* Update to ImageSharp 2.1.0 and ImageSharp.Web 2.0.0-alpha.0.23 * Rename CachedNameLength to CacheHashLength and add CacheFolderDepth setting * Replace PhysicalFileSystemProvider with WebRootImageProvider * Support EXIF-orientation in image dimention extractor * Remove virtual methods on FileProviderImageProvider * Simplify FileInfoImageResolver * Update to SixLabors.ImageSharp.Web 2.0.0-alpha.0.25 and remove custom providers * Make CropWebProcessor EXIF orientation-aware * Improve width/height sanitization * Also use 'v' as cache buster value * Add WebP to supported image file types * Update to SixLabors.ImageSharp.Web 2.0.0-alpha.0.27 and fix test * Fix rounding error and add test cases * Update to newest and stable releases * Move ImageSharpImageUrlGenerator to Umbraco.Web.Common * Use IConfigureOptions to configure ImageSharp options * Implement IEquatable on ImageUrlGenerationOptions classes * Fix empty/null values in image URL generation and corresponding tests * Use IsSupportedImageFormat extension method * Remove unneeded reflection
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NUnit.Framework;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Png;
|
||||
@@ -12,6 +10,7 @@ using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Web;
|
||||
using SixLabors.ImageSharp.Web.Commands;
|
||||
using SixLabors.ImageSharp.Web.Commands.Converters;
|
||||
using SixLabors.ImageSharp.Web.Middleware;
|
||||
using Umbraco.Cms.Web.Common.ImageProcessors;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.ImageProcessors
|
||||
@@ -20,61 +19,37 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.ImageProcessors
|
||||
public class CropWebProcessorTests
|
||||
{
|
||||
[Test]
|
||||
public void CropWebProcessor_CropsImage()
|
||||
// Coordinates are percentages to crop from the left, top, right and bottom sides
|
||||
[TestCase("0,0,0,0", 50, 90)]
|
||||
[TestCase("0.1,0.0,0.0,0.0", 45, 90)]
|
||||
[TestCase("0.0,0.1,0.0,0.0", 50, 81)]
|
||||
[TestCase("0.0,0.0,0.1,0.0", 45, 90)]
|
||||
[TestCase("0.0,0.0,0.0,0.1", 50, 81)]
|
||||
[TestCase("0.1,0.0,0.1,0.0", 40, 90)]
|
||||
[TestCase("0.0,0.1,0.0,0.1", 50, 72)]
|
||||
[TestCase("0.1,0.1,0.1,0.1", 40, 72)]
|
||||
[TestCase("0.25,0.25,0.25,0.25", 25, 45)]
|
||||
public void CropWebProcessor_CropsImage(string coordinates, int width, int height)
|
||||
{
|
||||
var converters = new List<ICommandConverter>
|
||||
using var image = new Image<Rgba32>(50, 90);
|
||||
using var formattedImage = new FormattedImage(image, PngFormat.Instance);
|
||||
|
||||
var logger = new NullLogger<ImageSharpMiddleware>();
|
||||
var commands = new CommandCollection
|
||||
{
|
||||
CreateArrayConverterOfFloat(),
|
||||
CreateSimpleCommandConverterOfFloat(),
|
||||
{ CropWebProcessor.Coordinates, coordinates },
|
||||
};
|
||||
|
||||
var parser = new CommandParser(converters);
|
||||
CultureInfo culture = CultureInfo.InvariantCulture;
|
||||
|
||||
var commands = new Dictionary<string, string>
|
||||
var parser = new CommandParser(new ICommandConverter[]
|
||||
{
|
||||
{ CropWebProcessor.Coordinates, "0.1,0.2,0.1,0.4" }, // left, top, right, bottom
|
||||
};
|
||||
new ArrayConverter<float>(),
|
||||
new SimpleCommandConverter<float>()
|
||||
});
|
||||
var culture = CultureInfo.InvariantCulture;
|
||||
|
||||
using var image = new Image<Rgba32>(50, 80);
|
||||
using FormattedImage formatted = CreateFormattedImage(image, PngFormat.Instance);
|
||||
new CropWebProcessor().Process(formatted, null, commands, parser, culture);
|
||||
new CropWebProcessor().Process(formattedImage, logger, commands, parser, culture);
|
||||
|
||||
Assert.AreEqual(40, image.Width); // Cropped 5 pixels from each side.
|
||||
Assert.AreEqual(32, image.Height); // Cropped 16 pixels from the top and 32 from the bottom.
|
||||
}
|
||||
|
||||
private static ICommandConverter CreateArrayConverterOfFloat()
|
||||
{
|
||||
// ImageSharp.Web's ArrayConverter is internal, so we need to use reflection to instantiate.
|
||||
var type = Type.GetType("SixLabors.ImageSharp.Web.Commands.Converters.ArrayConverter`1, SixLabors.ImageSharp.Web");
|
||||
Type[] typeArgs = { typeof(float) };
|
||||
Type genericType = type.MakeGenericType(typeArgs);
|
||||
return (ICommandConverter)Activator.CreateInstance(genericType);
|
||||
}
|
||||
|
||||
private static ICommandConverter CreateSimpleCommandConverterOfFloat()
|
||||
{
|
||||
// ImageSharp.Web's SimpleCommandConverter is internal, so we need to use reflection to instantiate.
|
||||
var type = Type.GetType("SixLabors.ImageSharp.Web.Commands.Converters.SimpleCommandConverter`1, SixLabors.ImageSharp.Web");
|
||||
Type[] typeArgs = { typeof(float) };
|
||||
Type genericType = type.MakeGenericType(typeArgs);
|
||||
return (ICommandConverter)Activator.CreateInstance(genericType);
|
||||
}
|
||||
|
||||
private FormattedImage CreateFormattedImage(Image<Rgba32> image, PngFormat format)
|
||||
{
|
||||
// Again, the constructor of FormattedImage useful for tests is internal, so we need to use reflection.
|
||||
Type type = typeof(FormattedImage);
|
||||
var instance = type.Assembly.CreateInstance(
|
||||
type.FullName,
|
||||
false,
|
||||
BindingFlags.Instance | BindingFlags.NonPublic,
|
||||
null,
|
||||
new object[] { image, format },
|
||||
null,
|
||||
null);
|
||||
return (FormattedImage)instance;
|
||||
Assert.AreEqual(width, image.Width);
|
||||
Assert.AreEqual(height, image.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Infrastructure.Media;
|
||||
using Umbraco.Cms.Web.Common.Media;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Media
|
||||
{
|
||||
[TestFixture]
|
||||
public class ImageSharpImageUrlGeneratorTests
|
||||
@@ -17,60 +17,70 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
private static readonly ImageSharpImageUrlGenerator s_generator = new ImageSharpImageUrlGenerator(new string[0]);
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_CropAliasTest()
|
||||
public void GetImageUrl_CropAliasTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Crop = s_crop, Width = 100, Height = 100 });
|
||||
Assert.AreEqual(MediaPath + "?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&width=100&height=100", urlString);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_WidthHeightTest()
|
||||
public void GetImageUrl_WidthHeightTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 200, Height = 300 });
|
||||
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=200&height=300", urlString);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrl_FocalPointTest()
|
||||
public void GetImageUrl_FocalPointTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 100, Height = 100 });
|
||||
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=100&height=100", urlString);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCropUrlFurtherOptionsTest()
|
||||
public void GetImageUrlFurtherOptionsTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus1, Width = 200, Height = 300, FurtherOptions = "&filter=comic&roundedcorners=radius-26|bgcolor-fff" });
|
||||
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
|
||||
Assert.AreEqual(MediaPath + "?rxy=0.96,0.80827067669172936&width=200&height=300&filter=comic&roundedcorners=radius-26%7Cbgcolor-fff", urlString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test that if options is null, the generated image URL is also null.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrlNullTest()
|
||||
public void GetImageUrlNullOptionsTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(null);
|
||||
Assert.AreEqual(null, urlString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test that if the image URL is null, the generated image URL is empty.
|
||||
/// Test that if the image URL is null, the generated image URL is also null.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrlEmptyTest()
|
||||
public void GetImageUrlNullTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(null));
|
||||
Assert.AreEqual(null, urlString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test that if the image URL is empty, the generated image URL is empty.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetImageUrlEmptyTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty));
|
||||
Assert.AreEqual(string.Empty, urlString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the GetCropUrl method on the ImageCropDataSet Model
|
||||
/// Test the GetImageUrl method on the ImageCropDataSet Model
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetBaseCropUrlFromModelTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(null) { Crop = s_crop, Width = 100, Height = 100 });
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(string.Empty) { Crop = s_crop, Width = 100, Height = 100 });
|
||||
Assert.AreEqual("?cc=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&width=100&height=100", urlString);
|
||||
}
|
||||
|
||||
@@ -78,7 +88,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test that if Crop mode is specified as anything other than Crop the image doesn't use the crop
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_SpecifiedCropModeTest()
|
||||
public void GetImageUrl_SpecifiedCropModeTest()
|
||||
{
|
||||
var urlStringMin = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Min, Width = 300, Height = 150 });
|
||||
var urlStringBoxPad = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.BoxPad, Width = 300, Height = 150 });
|
||||
@@ -97,7 +107,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test for upload property type
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_UploadTypeTest()
|
||||
public void GetImageUrl_UploadTypeTest()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Crop, ImageCropAnchor = ImageCropAnchor.Center, Width = 100, Height = 270 });
|
||||
Assert.AreEqual(MediaPath + "?rmode=crop&ranchor=center&width=100&height=270", urlString);
|
||||
@@ -107,7 +117,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test for preferFocalPoint when focal point is centered
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_PreferFocalPointCenter()
|
||||
public void GetImageUrl_PreferFocalPointCenter()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Width = 300, Height = 150 });
|
||||
Assert.AreEqual(MediaPath + "?width=300&height=150", urlString);
|
||||
@@ -117,7 +127,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test to check if crop ratio is ignored if useCropDimensions is true
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
|
||||
public void GetImageUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { FocalPoint = s_focus2, Width = 270, Height = 161 });
|
||||
Assert.AreEqual(MediaPath + "?rxy=0.4275,0.41&width=270&height=161", urlString);
|
||||
@@ -127,7 +137,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test to check result when only a width parameter is passed, effectivly a resize only
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_WidthOnlyParameter()
|
||||
public void GetImageUrl_WidthOnlyParameter()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Width = 200 });
|
||||
Assert.AreEqual(MediaPath + "?width=200", urlString);
|
||||
@@ -137,7 +147,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test to check result when only a height parameter is passed, effectivly a resize only
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_HeightOnlyParameter()
|
||||
public void GetImageUrl_HeightOnlyParameter()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { Height = 200 });
|
||||
Assert.AreEqual(MediaPath + "?height=200", urlString);
|
||||
@@ -147,7 +157,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
|
||||
/// Test to check result when using a background color with padding
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void GetCropUrl_BackgroundColorParameter()
|
||||
public void GetImageUrl_BackgroundColorParameter()
|
||||
{
|
||||
var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" });
|
||||
Assert.AreEqual(MediaPath + "?rmode=pad&width=400&height=400&bgcolor=fff", urlString);
|
||||
Reference in New Issue
Block a user