Remove ImageCropRatioMode and clean up GetCropUrl overloads

This commit is contained in:
Ronald Barendse
2021-08-12 09:03:02 +02:00
parent 902984f977
commit 8b1bd14df1
6 changed files with 33 additions and 127 deletions

View File

@@ -1,8 +0,0 @@
namespace Umbraco.Cms.Core.Models
{
public enum ImageCropRatioMode
{
Width,
Height
}
}

View File

@@ -101,25 +101,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
y.Height == 50)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndWidthRatioModeProvidedWithHeight_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
ratioMode: ImageCropRatioMode.Width,
height: 50);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 50 &&
y.Height == 50)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndWidthRatioModeProvidedWithWidthAndHeight_CallsImageGeneratorWithCorrectParameters()
{
@@ -130,7 +111,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
ratioMode: ImageCropRatioMode.Width,
width: 35,
height: 50);
@@ -140,25 +120,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
y.Height == 50)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndHeightRatioModeProvidedWithWidth_CallsImageGeneratorWithCorrectParameters()
{
var imageUrl = "/test.jpg";
Mock<IImageUrlGenerator> imageUrlGenerator = CreateMockImageUrlGenerator();
var result = imageUrl.GetCropUrl(
imageUrlGenerator.Object,
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
ratioMode: ImageCropRatioMode.Height,
width: 60);
imageUrlGenerator
.Verify(x => x.GetImageUrl(
It.Is<ImageUrlGenerationOptions>(y => y.Width == 60 &&
y.Height == 60)));
}
[Test]
public void GetCropUrl_WithCropSpecifiedAndHeightRatioModeProvidedWithWidthAndHeight_CallsImageGeneratorWithCorrectParameters()
{
@@ -169,7 +130,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions
CreateImageCropperValueWithCrops(),
imageCropMode: ImageCropMode.Crop,
cropAlias: "TestCrop",
ratioMode: ImageCropRatioMode.Height,
width: 60,
height: 40);

View File

@@ -145,7 +145,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
[Test]
public void GetCropUrlFurtherOptionsTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff");
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "filter=comic&roundedcorners=radius-26|bgcolor-fff");
Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=200&h=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString);
}
@@ -176,7 +176,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
[Test]
public void GetCropUrl_CropAliasHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, ratioMode: ImageCropRatioMode.Height);
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true);
Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString);
}
@@ -186,7 +186,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
[Test]
public void GetCropUrl_WidthHeightRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Height);
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=300&h=150", urlString);
}
@@ -196,7 +196,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
[Test]
public void GetCropUrl_HeightWidthRatioModeTest()
{
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Width);
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150);
Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=300&h=150", urlString);
}
@@ -321,7 +321,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"" + MediaPath + "\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "&bgcolor=fff");
var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "bgcolor=fff");
Assert.AreEqual(MediaPath + "?m=pad&w=400&h=400&bgcolor=fff", urlString);
}

View File

@@ -82,9 +82,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBuster">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -100,8 +99,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
=> mediaItem.GetCropUrl(
ImageUrlGenerator,
PublishedValueFallback,
@@ -116,8 +114,7 @@ namespace Umbraco.Extensions
preferFocalPoint,
useCropDimensions,
cacheBuster,
furtherOptions,
ratioMode
furtherOptions
);
/// <summary>
@@ -136,9 +133,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBusterValue">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -154,8 +150,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
=> imageUrl.GetCropUrl(
ImageUrlGenerator,
width,
@@ -168,8 +163,7 @@ namespace Umbraco.Extensions
preferFocalPoint,
useCropDimensions,
cacheBusterValue,
furtherOptions,
ratioMode
furtherOptions
);
/// <summary>
@@ -188,9 +182,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBusterValue">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -206,8 +199,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
=> imageUrl.GetCropUrl(
ImageUrlGenerator,
cropDataSet,
@@ -219,8 +211,7 @@ namespace Umbraco.Extensions
preferFocalPoint,
useCropDimensions,
cacheBusterValue,
furtherOptions,
ratioMode
furtherOptions
);

View File

@@ -103,9 +103,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBuster">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -124,8 +123,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode);
string furtherOptions = null) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions);
public static string GetCropUrl(
this MediaWithCrops mediaWithCrops,
@@ -142,15 +140,14 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
{
if (mediaWithCrops == null)
{
throw new ArgumentNullException(nameof(mediaWithCrops));
}
return mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode);
return mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions);
}
private static string GetCropUrl(
@@ -170,8 +167,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
{
if (mediaItem == null)
{
@@ -219,7 +215,7 @@ namespace Umbraco.Extensions
return GetCropUrl(
mediaItemUrl, imageUrlGenerator, localCrops, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions,
cacheBusterValue, furtherOptions, ratioMode);
cacheBusterValue, furtherOptions);
}
/// <summary>
@@ -239,9 +235,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBusterValue">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -258,8 +253,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
{
if (string.IsNullOrWhiteSpace(imageUrl))
{
@@ -274,7 +268,7 @@ namespace Umbraco.Extensions
return GetCropUrl(
imageUrl, imageUrlGenerator, cropDataSet, width, height, cropAlias, quality, imageCropMode,
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode);
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions);
}
/// <summary>
@@ -294,9 +288,8 @@ namespace Umbraco.Extensions
/// <param name="cacheBusterValue">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param>
/// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example:
/// <example><![CDATA[
/// furtherOptions: "&bgcolor=fff"
/// furtherOptions: "bgcolor=fff"
/// ]]></example></param>
/// <param name="ratioMode">Use a dimension as a ratio.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
@@ -313,8 +306,7 @@ namespace Umbraco.Extensions
bool preferFocalPoint = false,
bool useCropDimensions = false,
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null)
string furtherOptions = null)
{
if (string.IsNullOrWhiteSpace(imageUrl))
{
@@ -334,14 +326,14 @@ namespace Umbraco.Extensions
options = cropDataSet.GetCropBaseOptions(imageUrl, crop, preferFocalPoint || string.IsNullOrWhiteSpace(cropAlias));
if (crop != null & useCropDimensions)
if (crop != null && useCropDimensions)
{
width = crop.Width;
height = crop.Height;
}
// Calculate missing dimension if a predefined crop has been specified, there are no coordinates and no ratio mode
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null)
// Calculate missing dimension if a predefined crop has been specified, but has no coordinates
if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null)
{
if (width != null && height == null)
{
@@ -357,41 +349,14 @@ namespace Umbraco.Extensions
{
options = new ImageUrlGenerationOptions(imageUrl)
{
ImageCropMode = (imageCropMode ?? ImageCropMode.Pad),
ImageCropMode = (imageCropMode ?? ImageCropMode.Pad), // Not sure why we default to Pad
ImageCropAnchor = imageCropAnchor
};
}
options.Quality = quality;
options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width;
options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height;
if (ratioMode == ImageCropRatioMode.Width && height != null)
{
// If only height specified then assume a square
if (width == null)
{
options.Width = height;
}
else
{
options.Width = (int)MathF.Round(height.Value * ((float)width.Value / height.Value));
}
}
if (ratioMode == ImageCropRatioMode.Height && width != null)
{
// If only width specified then assume a square
if (height == null)
{
options.Height = width;
}
else
{
options.Height = (int)MathF.Round(width.Value * ((float)height.Value / width.Value));
}
}
options.Width = width;
options.Height = height;
options.FurtherOptions = furtherOptions;
options.CacheBusterValue = cacheBusterValue;

View File

@@ -246,7 +246,6 @@ namespace Umbraco.Extensions
bool useCropDimensions = false,
bool cacheBuster = true,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null,
bool htmlEncode = true)
{
if (mediaItem == null)
@@ -255,7 +254,7 @@ namespace Umbraco.Extensions
}
var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode,
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode);
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions);
return CreateHtmlString(url, htmlEncode);
}
@@ -272,14 +271,13 @@ namespace Umbraco.Extensions
bool useCropDimensions = true,
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null,
bool htmlEncode = true)
{
if (imageCropperValue == null) return HtmlString.Empty;
var imageUrl = imageCropperValue.Src;
var url = imageUrl.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode,
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode);
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions);
return CreateHtmlString(url, htmlEncode);
}