2020-12-20 08:36:11 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic ;
using System.Globalization ;
using System.Text ;
2016-02-04 16:03:43 +01:00
using Newtonsoft.Json ;
2014-03-20 12:22:50 +00:00
using Newtonsoft.Json.Linq ;
2020-12-20 08:36:11 +01:00
using NUnit.Framework ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core ;
using Umbraco.Cms.Core.Media ;
using Umbraco.Cms.Core.Models ;
2018-01-26 17:55:20 +01:00
using Umbraco.Core.PropertyEditors.ValueConverters ;
2020-08-04 14:12:22 +02:00
using Umbraco.Extensions ;
2014-03-20 12:22:50 +00:00
2021-02-10 14:58:22 +01:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common
2014-03-20 12:22:50 +00:00
{
[TestFixture]
public class ImageCropperTest
{
2017-07-20 11:21:28 +02:00
private const string CropperJson1 = "{\"focalPoint\": {\"left\": 0.96,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}" ;
2016-06-30 16:39:05 +02:00
private const string CropperJson2 = "{\"focalPoint\": {\"left\": 0.98,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0672.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}" ;
private const string CropperJson3 = "{\"focalPoint\": {\"left\": 0.98,\"top\": 0.80827067669172936},\"src\": \"/media/1005/img_0672.jpg\",\"crops\": []}" ;
private const string MediaPath = "/media/1005/img_0671.jpg" ;
2016-02-05 13:51:43 +01:00
[Test]
2016-02-04 16:03:43 +01:00
public void CanConvertImageCropperDataSetSrcToString ( )
{
2020-12-20 08:36:11 +01:00
// cropperJson3 - has not crops
ImageCropperValue cropperValue = CropperJson3 . DeserializeImageCropperValue ( ) ;
Attempt < string > serialized = cropperValue . TryConvertTo < string > ( ) ;
2018-01-29 19:23:25 +01:00
Assert . IsTrue ( serialized . Success ) ;
Assert . AreEqual ( "/media/1005/img_0672.jpg" , serialized . Result ) ;
2016-02-04 16:03:43 +01:00
}
2016-02-05 13:51:43 +01:00
[Test]
2016-02-04 16:03:43 +01:00
public void CanConvertImageCropperDataSetJObject ( )
{
2020-12-20 08:36:11 +01:00
// cropperJson3 - has not crops
ImageCropperValue cropperValue = CropperJson3 . DeserializeImageCropperValue ( ) ;
Attempt < JObject > serialized = cropperValue . TryConvertTo < JObject > ( ) ;
2018-01-29 19:23:25 +01:00
Assert . IsTrue ( serialized . Success ) ;
Assert . AreEqual ( cropperValue , serialized . Result . ToObject < ImageCropperValue > ( ) ) ;
2016-02-04 16:03:43 +01:00
}
2016-02-05 13:51:43 +01:00
[Test]
2016-02-04 16:03:43 +01:00
public void CanConvertImageCropperDataSetJsonToString ( )
{
2020-12-20 08:36:11 +01:00
ImageCropperValue cropperValue = CropperJson1 . DeserializeImageCropperValue ( ) ;
Attempt < string > serialized = cropperValue . TryConvertTo < string > ( ) ;
2018-01-29 19:23:25 +01:00
Assert . IsTrue ( serialized . Success ) ;
Assert . IsTrue ( serialized . Result . DetectIsJson ( ) ) ;
2020-12-20 08:36:11 +01:00
ImageCropperValue obj = JsonConvert . DeserializeObject < ImageCropperValue > ( CropperJson1 , new JsonSerializerSettings { Culture = CultureInfo . InvariantCulture , FloatParseHandling = FloatParseHandling . Decimal } ) ;
2018-01-29 19:23:25 +01:00
Assert . AreEqual ( cropperValue , obj ) ;
2016-02-04 16:03:43 +01:00
}
2020-12-20 08:36:11 +01:00
//// [TestCase(CropperJson1, CropperJson1, true)]
//// [TestCase(CropperJson1, CropperJson2, false)]
//// public void CanConvertImageCropperPropertyEditor(string val1, string val2, bool expected)
//// {
//// try
//// {
//// var container = TestHelper.GetRegister();
//// var composition = new Composition(container, TestHelper.GetMockedTypeLoader(), Mock.Of<IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run), TestHelper.GetConfigs(), TestHelper.IOHelper, AppCaches.NoCache);
////
//// composition.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>();
////
//// Current.Factory = composition.CreateFactory();
////
//// var logger = Mock.Of<ILogger>();
//// var scheme = Mock.Of<IMediaPathScheme>();
//// var shortStringHelper = Mock.Of<IShortStringHelper>();
////
//// var mediaFileSystem = new MediaFileSystem(Mock.Of<IFileSystem>(), scheme, logger, shortStringHelper);
////
//// var dataTypeService = new TestObjects.TestDataTypeService(
//// new DataType(new ImageCropperPropertyEditor(Mock.Of<ILogger>(), mediaFileSystem, Mock.Of<IContentSettings>(), Mock.Of<IDataTypeService>(), Mock.Of<ILocalizationService>(), TestHelper.IOHelper, TestHelper.ShortStringHelper, Mock.Of<ILocalizedTextService>())) { Id = 1 });
////
//// var factory = new PublishedContentTypeFactory(Mock.Of<IPublishedModelFactory>(), new PropertyValueConverterCollection(Array.Empty<IPropertyValueConverter>()), dataTypeService);
////
//// var converter = new ImageCropperValueConverter();
//// var result = converter.ConvertSourceToIntermediate(null, factory.CreatePropertyType("test", 1), val1, false); // does not use type for conversion
////
//// var resultShouldMatch = val2.DeserializeImageCropperValue();
//// if (expected)
//// {
//// Assert.AreEqual(resultShouldMatch, result);
//// }
//// else
//// {
//// Assert.AreNotEqual(resultShouldMatch, result);
//// }
//// }
//// finally
//// {
//// Current.Reset();
//// }
//// }
2014-03-24 16:32:50 +00:00
2014-03-20 12:22:50 +00:00
[Test]
2014-03-24 16:32:50 +00:00
public void GetCropUrl_CropAliasTest ( )
2014-03-20 12:22:50 +00:00
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , cropAlias : "Thumb" , useCropDimensions : true ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100" , urlString ) ;
2014-03-24 16:32:50 +00:00
}
2014-03-20 12:22:50 +00:00
2015-08-20 09:31:20 +01:00
/// <summary>
/// Test to ensure useCropDimensions is observed
/// </summary>
[Test]
public void GetCropUrl_CropAliasIgnoreWidthHeightTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , cropAlias : "Thumb" , useCropDimensions : true , width : 50 , height : 50 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100" , urlString ) ;
2015-08-20 09:31:20 +01:00
}
2014-03-24 16:32:50 +00:00
[Test]
public void GetCropUrl_WidthHeightTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 200 , height : 300 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300" , urlString ) ;
2014-03-24 16:32:50 +00:00
}
2014-03-20 12:22:50 +00:00
2014-03-24 16:32:50 +00:00
[Test]
public void GetCropUrl_FocalPointTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , cropAlias : "thumb" , preferFocalPoint : true , useCropDimensions : true ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.80827067669172936x0.96&w=100&h=100" , urlString ) ;
2014-03-20 12:22:50 +00:00
}
2014-03-24 17:51:52 +00:00
[Test]
public void GetCropUrlFurtherOptionsTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 200 , height : 300 , furtherOptions : "&filter=comic&roundedcorners=radius-26|bgcolor-fff" ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.80827067669172936x0.96&w=200&h=300&filter=comic&roundedcorners=radius-26|bgcolor-fff" , urlString ) ;
2014-03-24 17:51:52 +00:00
}
2014-04-21 08:38:53 +01:00
/// <summary>
/// Test that if a crop alias has been specified that doesn't exist the method returns null
/// </summary>
[Test]
public void GetCropUrlNullTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , cropAlias : "Banner" , useCropDimensions : true ) ;
2014-04-21 08:38:53 +01:00
Assert . AreEqual ( null , urlString ) ;
}
/// <summary>
/// Test the GetCropUrl method on the ImageCropDataSet Model
/// </summary>
[Test]
public void GetBaseCropUrlFromModelTest ( )
{
2020-12-20 08:36:11 +01:00
ImageCropperValue cropDataSet = CropperJson1 . DeserializeImageCropperValue ( ) ;
2020-02-07 15:01:03 -08:00
var urlString = cropDataSet . GetCropUrl ( "thumb" , new TestImageUrlGenerator ( ) ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100" , urlString ) ;
2014-04-21 08:38:53 +01:00
}
2014-04-21 08:53:40 +01:00
/// <summary>
/// Test the height ratio mode with predefined crop dimensions
/// </summary>
[Test]
public void GetCropUrl_CropAliasHeightRatioModeTest ( )
{
2020-12-20 08:36:11 +01:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , cropAlias : "Thumb" , useCropDimensions : true , ratioMode : ImageCropRatioMode . Height ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&hr=1&w=100" , urlString ) ;
2014-04-21 08:53:40 +01:00
}
/// <summary>
/// Test the height ratio mode with manual width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_WidthHeightRatioModeTest ( )
{
2020-12-20 08:36:11 +01:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , ratioMode : ImageCropRatioMode . Height ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.80827067669172936x0.96&hr=0.5&w=300" , urlString ) ;
2014-04-21 08:53:40 +01:00
}
/// <summary>
/// Test the height ratio mode with width/height dimensions
/// </summary>
[Test]
public void GetCropUrl_HeightWidthRatioModeTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , ratioMode : ImageCropRatioMode . Width ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.80827067669172936x0.96&wr=2&h=150" , urlString ) ;
2014-04-21 08:53:40 +01:00
}
2014-04-27 22:06:48 +01:00
/// <summary>
/// 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 ( )
{
2020-02-07 15:01:03 -08:00
var urlStringMin = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , imageCropMode : ImageCropMode . Min ) ;
var urlStringBoxPad = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , imageCropMode : ImageCropMode . BoxPad ) ;
var urlStringPad = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , imageCropMode : ImageCropMode . Pad ) ;
2020-12-20 08:36:11 +01:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , imageCropMode : ImageCropMode . Max ) ;
2020-02-07 15:01:03 -08:00
var urlStringStretch = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : CropperJson1 , width : 300 , height : 150 , imageCropMode : ImageCropMode . Stretch ) ;
2017-07-20 11:21:28 +02:00
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=min&w=300&h=150" , urlStringMin ) ;
Assert . AreEqual ( MediaPath + "?m=boxpad&w=300&h=150" , urlStringBoxPad ) ;
Assert . AreEqual ( MediaPath + "?m=pad&w=300&h=150" , urlStringPad ) ;
Assert . AreEqual ( MediaPath + "?m=max&w=300&h=150" , urlString ) ;
Assert . AreEqual ( MediaPath + "?m=stretch&w=300&h=150" , urlStringStretch ) ;
2014-04-27 22:06:48 +01:00
}
2014-04-27 22:17:24 +01:00
/// <summary>
/// Test for upload property type
/// </summary>
[Test]
public void GetCropUrl_UploadTypeTest ( )
{
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , width : 100 , height : 270 , imageCropMode : ImageCropMode . Crop , imageCropAnchor : ImageCropAnchor . Center ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=crop&a=center&w=100&h=270" , urlString ) ;
2014-04-27 22:17:24 +01:00
}
2015-02-27 14:42:00 +00:00
/// <summary>
/// Test for preferFocalPoint when focal point is centered
/// </summary>
[Test]
public void GetCropUrl_PreferFocalPointCenter ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\":\"thumb\",\"width\": 100,\"height\": 100,\"coordinates\": {\"x1\": 0.58729977382575338,\"y1\": 0.055768992440203169,\"x2\": 0,\"y2\": 0.32457553600198386}}]}" ;
2015-02-27 14:42:00 +00:00
2020-12-20 08:36:11 +01:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , width : 300 , height : 150 , preferFocalPoint : true ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=defaultcrop&w=300&h=150" , urlString ) ;
2015-02-27 14:42:00 +00:00
}
2015-08-19 16:54:03 +01:00
2015-08-20 09:31:20 +01:00
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point in centre when a width parameter is passed
/// </summary>
2015-08-19 16:54:03 +01:00
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidth ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-19 16:54:03 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , cropAlias : "home" , width : 200 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=defaultcrop&hr=0.5962962962962962962962962963&w=200" , urlString ) ;
2015-08-19 16:54:03 +01:00
}
2015-08-20 09:31:20 +01:00
/// <summary>
/// Test to check if height ratio is returned for a predefined crop without coordinates and focal point is custom when a width parameter is passed
/// </summary>
2015-08-19 16:54:03 +01:00
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPoint ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-19 16:54:03 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , cropAlias : "home" , width : 200 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.41x0.4275&hr=0.5962962962962962962962962963&w=200" , urlString ) ;
2015-08-19 16:54:03 +01:00
}
2015-08-20 09:31:20 +01:00
/// <summary>
/// Test to check if crop ratio is ignored if useCropDimensions is true
/// </summary>
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPointIgnore ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-20 09:31:20 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , cropAlias : "home" , width : 200 , useCropDimensions : true ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?f=0.41x0.4275&w=270&h=161" , urlString ) ;
2015-08-20 09:31:20 +01:00
}
/// <summary>
/// Test to check if width ratio is returned for a predefined crop without coordinates and focal point in centre when a height parameter is passed
/// </summary>
2015-08-19 16:54:03 +01:00
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithHeight ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-19 16:54:03 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , cropAlias : "home" , height : 200 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=defaultcrop&wr=1.6770186335403726708074534161&h=200" , urlString ) ;
2015-08-19 16:54:03 +01:00
}
2015-08-20 10:22:15 +01:00
/// <summary>
/// Test to check result when only a width parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_WidthOnlyParameter ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-20 10:22:15 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , width : 200 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=defaultcrop&w=200" , urlString ) ;
2015-08-20 10:22:15 +01:00
}
/// <summary>
/// Test to check result when only a height parameter is passed, effectivly a resize only
/// </summary>
[Test]
public void GetCropUrl_HeightOnlyParameter ( )
{
2016-06-30 16:39:05 +02:00
const string cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2015-08-20 10:22:15 +01:00
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , imageCropperValue : cropperJson , height : 200 ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=defaultcrop&h=200" , urlString ) ;
2015-08-20 10:22:15 +01:00
}
2017-05-12 14:49:44 +02:00
/// <summary>
/// Test to check result when using a background color with padding
/// </summary>
[Test]
public void GetCropUrl_BackgroundColorParameter ( )
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"" + MediaPath + "\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}" ;
2020-02-07 15:01:03 -08:00
var urlString = MediaPath . GetCropUrl ( new TestImageUrlGenerator ( ) , 400 , 400 , cropperJson , imageCropMode : ImageCropMode . Pad , furtherOptions : "&bgcolor=fff" ) ;
2020-02-08 11:05:14 -08:00
Assert . AreEqual ( MediaPath + "?m=pad&w=400&h=400&bgcolor=fff" , urlString ) ;
2017-05-12 14:49:44 +02:00
}
2020-02-07 15:01:03 -08:00
2020-02-08 11:05:14 -08:00
internal class TestImageUrlGenerator : IImageUrlGenerator
2020-02-07 15:01:03 -08:00
{
2020-09-22 15:06:03 +02:00
public IEnumerable < string > SupportedImageFileTypes = > new [ ] { "jpeg" , "jpg" , "gif" , "bmp" , "png" , "tiff" , "tif" } ;
2020-02-07 15:01:03 -08:00
public string GetImageUrl ( ImageUrlGenerationOptions options )
{
var imageProcessorUrl = new StringBuilder ( options . ImageUrl ? ? string . Empty ) ;
if ( options . FocalPoint ! = null )
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl . Append ( "?f=" ) ;
2020-02-07 15:01:03 -08:00
imageProcessorUrl . Append ( options . FocalPoint . Top . ToString ( CultureInfo . InvariantCulture ) ) ;
2020-02-08 11:05:14 -08:00
imageProcessorUrl . Append ( "x" ) ;
2020-02-07 15:01:03 -08:00
imageProcessorUrl . Append ( options . FocalPoint . Left . ToString ( CultureInfo . InvariantCulture ) ) ;
}
else if ( options . Crop ! = null )
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl . Append ( "?c=" ) ;
2020-02-07 15:01:03 -08:00
imageProcessorUrl . Append ( options . Crop . X1 . ToString ( CultureInfo . InvariantCulture ) ) . Append ( "," ) ;
imageProcessorUrl . Append ( options . Crop . Y1 . ToString ( CultureInfo . InvariantCulture ) ) . Append ( "," ) ;
imageProcessorUrl . Append ( options . Crop . X2 . ToString ( CultureInfo . InvariantCulture ) ) . Append ( "," ) ;
imageProcessorUrl . Append ( options . Crop . Y2 . ToString ( CultureInfo . InvariantCulture ) ) ;
}
else if ( options . DefaultCrop )
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl . Append ( "?m=defaultcrop" ) ;
2020-02-07 15:01:03 -08:00
}
else
{
2020-02-08 11:05:14 -08:00
imageProcessorUrl . Append ( "?m=" + options . ImageCropMode . ToString ( ) . ToLower ( ) ) ;
2020-12-20 08:36:11 +01:00
if ( options . ImageCropAnchor ! = null )
{
imageProcessorUrl . Append ( "&a=" + options . ImageCropAnchor . ToString ( ) . ToLower ( ) ) ;
}
2020-02-07 15:01:03 -08:00
}
2020-02-08 11:05:14 -08:00
var hasFormat = options . FurtherOptions ! = null & & options . FurtherOptions . InvariantContains ( "&f=" ) ;
2020-12-20 08:36:11 +01:00
if ( options . Quality ! = null & & hasFormat = = false )
{
imageProcessorUrl . Append ( "&q=" + options . Quality ) ;
}
if ( options . HeightRatio ! = null )
{
imageProcessorUrl . Append ( "&hr=" + options . HeightRatio . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
}
if ( options . WidthRatio ! = null )
{
imageProcessorUrl . Append ( "&wr=" + options . WidthRatio . Value . ToString ( CultureInfo . InvariantCulture ) ) ;
}
if ( options . Width ! = null )
{
imageProcessorUrl . Append ( "&w=" + options . Width ) ;
}
if ( options . Height ! = null )
{
imageProcessorUrl . Append ( "&h=" + options . Height ) ;
}
if ( options . UpScale = = false )
{
imageProcessorUrl . Append ( "&u=no" ) ;
}
if ( options . AnimationProcessMode ! = null )
{
imageProcessorUrl . Append ( "&apm=" + options . AnimationProcessMode ) ;
}
if ( options . FurtherOptions ! = null )
{
imageProcessorUrl . Append ( options . FurtherOptions ) ;
}
if ( options . Quality ! = null & & hasFormat )
{
imageProcessorUrl . Append ( "&q=" + options . Quality ) ;
}
if ( options . CacheBusterValue ! = null )
{
imageProcessorUrl . Append ( "&r=" ) . Append ( options . CacheBusterValue ) ;
}
2020-02-07 15:01:03 -08:00
return imageProcessorUrl . ToString ( ) ;
}
2017-05-12 14:49:44 +02:00
}
2014-03-20 12:22:50 +00:00
}
2017-07-20 11:21:28 +02:00
}