Introduce Image URL Generator abstraction

This commit is contained in:
Benjamin Carleski
2020-02-07 15:01:03 -08:00
parent 02a9645ed1
commit a7ff20de6e
22 changed files with 747 additions and 269 deletions

View File

@@ -5,6 +5,7 @@ using Umbraco.Core.Dictionary;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PackageActions;
using Umbraco.Core.Packaging;
@@ -208,6 +209,8 @@ namespace Umbraco.Core.Composing
public static IVariationContextAccessor VariationContextAccessor
=> Factory.GetInstance<IVariationContextAccessor>();
public static IImageUrlGenerator ImageUrlGenerator
=> Factory.GetInstance<IImageUrlGenerator>();
#endregion
}
}

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Core.Models
{
public interface IImageUrlGenerator
{
string GetImageUrl(ImageUrlGenerationOptions options);
}
}

View File

@@ -0,0 +1,35 @@
namespace Umbraco.Core.Models
{
public class ImageUrlGenerationOptions
{
public string ImageUrl { get; set; }
public int? Width { get; set; }
public int? Height { get; set; }
public decimal? WidthRatio { get; set; }
public decimal? HeightRatio { get; set; }
public int? Quality { get; set; }
public string ImageCropMode { get; set; }
public string ImageCropAnchor { get; set; }
public bool DefaultCrop { get; set; }
public FocalPointPosition FocalPoint { get; set; }
public CropCoordinates Crop { get; set; }
public string CacheBusterValue { get; set; }
public string FurtherOptions { get; set; }
public bool UpScale { get; set; } = true;
public string AnimationProcessMode { get; set; }
public class FocalPointPosition
{
public decimal Left { get; set; }
public decimal Top { get; set; }
}
public class CropCoordinates
{
public decimal X1 { get; set; }
public decimal Y1 { get; set; }
public decimal X2 { get; set; }
public decimal Y2 { get; set; }
}
}
}

View File

@@ -106,13 +106,14 @@ namespace Umbraco.Core.Models
//use the custom avatar
var avatarUrl = Current.MediaFileSystem.GetUrl(user.Avatar);
var urlGenerator = Current.ImageUrlGenerator;
return new[]
{
avatarUrl + "?width=30&height=30&mode=crop",
avatarUrl + "?width=60&height=60&mode=crop",
avatarUrl + "?width=90&height=90&mode=crop",
avatarUrl + "?width=150&height=150&mode=crop",
avatarUrl + "?width=300&height=300&mode=crop"
urlGenerator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = avatarUrl, ImageCropMode = "crop", Width = 30, Height = 30 }),
urlGenerator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = avatarUrl, ImageCropMode = "crop", Width = 60, Height = 60 }),
urlGenerator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = avatarUrl, ImageCropMode = "crop", Width = 90, Height = 90 }),
urlGenerator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = avatarUrl, ImageCropMode = "crop", Width = 150, Height = 150 }),
urlGenerator.GetImageUrl(new ImageUrlGenerationOptions { ImageUrl = avatarUrl, ImageCropMode = "crop", Width = 300, Height = 300 })
};
}

View File

@@ -7,6 +7,8 @@ using System.Runtime.Serialization;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Serialization;
namespace Umbraco.Core.PropertyEditors.ValueConverters
@@ -59,38 +61,34 @@ namespace Umbraco.Core.PropertyEditors.ValueConverters
: Crops.FirstOrDefault(x => x.Alias.InvariantEquals(alias));
}
internal void AppendCropBaseUrl(StringBuilder url, ImageCropperCrop crop, bool defaultCrop, bool preferFocalPoint)
internal ImageUrlGenerationOptions GetCropBaseOptions(string url, ImageCropperCrop crop, bool defaultCrop, bool preferFocalPoint)
{
if (preferFocalPoint && HasFocalPoint()
|| crop != null && crop.Coordinates == null && HasFocalPoint()
|| defaultCrop && HasFocalPoint())
{
url.Append("?center=");
url.Append(FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
url.Append(",");
url.Append(FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
url.Append("&mode=crop");
return new ImageUrlGenerationOptions { ImageUrl = url, FocalPoint = new ImageUrlGenerationOptions.FocalPointPosition { Left = FocalPoint.Left, Top = FocalPoint.Top } };
}
else if (crop != null && crop.Coordinates != null && preferFocalPoint == false)
{
url.Append("?crop=");
url.Append(crop.Coordinates.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
url.Append(crop.Coordinates.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
url.Append(crop.Coordinates.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
url.Append(crop.Coordinates.Y2.ToString(CultureInfo.InvariantCulture));
url.Append("&cropmode=percentage");
return new ImageUrlGenerationOptions { ImageUrl = url, Crop = new ImageUrlGenerationOptions.CropCoordinates { X1 = crop.Coordinates.X1, X2 = crop.Coordinates.X2, Y1 = crop.Coordinates.Y1, Y2 = crop.Coordinates.Y2 } };
}
else
{
url.Append("?anchor=center");
url.Append("&mode=crop");
return new ImageUrlGenerationOptions { ImageUrl = url, DefaultCrop = true };
}
}
/// <summary>
/// Gets the value image url for a specified crop.
/// </summary>
public string GetCropUrl(string alias, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null)
[Obsolete("Use the overload that takes an IImageUrlGenerator")]
public string GetCropUrl(string alias, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null) => GetCropUrl(alias, Current.ImageUrlGenerator, useCropDimensions, useFocalPoint, cacheBusterValue);
/// <summary>
/// Gets the value image url for a specified crop.
/// </summary>
public string GetCropUrl(string alias, IImageUrlGenerator imageUrlGenerator, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null)
{
var crop = GetCrop(alias);
@@ -98,38 +96,37 @@ namespace Umbraco.Core.PropertyEditors.ValueConverters
if (crop == null && !string.IsNullOrWhiteSpace(alias))
return null;
var url = new StringBuilder();
AppendCropBaseUrl(url, crop, string.IsNullOrWhiteSpace(alias), useFocalPoint);
var options = GetCropBaseOptions(string.Empty, crop, string.IsNullOrWhiteSpace(alias), useFocalPoint);
if (crop != null && useCropDimensions)
{
url.Append("&width=").Append(crop.Width);
url.Append("&height=").Append(crop.Height);
options.Width = crop.Width;
options.Height = crop.Height;
}
if (cacheBusterValue != null)
url.Append("&rnd=").Append(cacheBusterValue);
options.CacheBusterValue = cacheBusterValue;
return url.ToString();
return imageUrlGenerator.GetImageUrl(options);
}
/// <summary>
/// Gets the value image url for a specific width and height.
/// </summary>
public string GetCropUrl(int width, int height, bool useFocalPoint = false, string cacheBusterValue = null)
[Obsolete("Use the overload that takes an IImageUrlGenerator")]
public string GetCropUrl(int width, int height, bool useFocalPoint = false, string cacheBusterValue = null) => GetCropUrl(width, height, Current.ImageUrlGenerator, useFocalPoint, cacheBusterValue);
/// <summary>
/// Gets the value image url for a specific width and height.
/// </summary>
public string GetCropUrl(int width, int height, IImageUrlGenerator imageUrlGenerator, bool useFocalPoint = false, string cacheBusterValue = null)
{
var url = new StringBuilder();
var options = GetCropBaseOptions(string.Empty, null, true, useFocalPoint);
AppendCropBaseUrl(url, null, true, useFocalPoint);
options.Width = width;
options.Height = height;
options.CacheBusterValue = cacheBusterValue;
url.Append("&width=").Append(width);
url.Append("&height=").Append(height);
if (cacheBusterValue != null)
url.Append("&rnd=").Append(cacheBusterValue);
return url.ToString();
return imageUrlGenerator.GetImageUrl(options);
}
/// <summary>

View File

@@ -132,6 +132,8 @@
<Compile Include="Migrations\Upgrade\V_8_0_0\Models\PropertyDataDto80.cs" />
<Compile Include="Migrations\Upgrade\V_8_0_0\Models\PropertyTypeDto80.cs" />
<Compile Include="Migrations\Upgrade\V_8_6_0\AddMainDomLock.cs" />
<Compile Include="Models\IImageUrlGenerator.cs" />
<Compile Include="Models\ImageUrlGenerationOptions.cs" />
<Compile Include="Runtime\IMainDomLock.cs" />
<Compile Include="Runtime\MainDomSemaphoreLock.cs" />
<Compile Include="Runtime\SqlMainDomLock.cs" />