Files
Umbraco-CMS/src/Umbraco.Infrastructure/Media/ImageSharpImageUrlGenerator.cs

96 lines
3.2 KiB
C#
Raw Normal View History

using System;
2021-08-06 12:59:56 +02:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SixLabors.ImageSharp;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Infrastructure.Media
{
/// <summary>
/// Exposes a method that generates an image URL based on the specified options that can be processed by ImageSharp.
/// </summary>
/// <seealso cref="Umbraco.Cms.Core.Media.IImageUrlGenerator" />
public class ImageSharpImageUrlGenerator : IImageUrlGenerator
{
private static readonly string[] s_supportedImageFileTypes = Configuration.Default.ImageFormats.SelectMany(f => f.FileExtensions).ToArray();
/// <inheritdoc />
/// <remarks>
/// This uses the default instance of the ImageSharp configuration, so we need to ensure we don't new up a different instance when configuring the middleware.
/// </remarks>
public IEnumerable<string> SupportedImageFileTypes { get; } = s_supportedImageFileTypes;
/// <inheritdoc/>
public string GetImageUrl(ImageUrlGenerationOptions options)
{
2021-08-06 14:22:27 +02:00
if (options == null)
{
return null;
}
2020-02-08 11:05:14 -08:00
2021-08-06 14:22:27 +02:00
var imageUrl = new StringBuilder(options.ImageUrl);
bool queryStringHasStarted = false;
void AppendQueryString(string value)
2021-08-06 14:22:27 +02:00
{
imageUrl.Append(queryStringHasStarted ? '&' : '?');
queryStringHasStarted = true;
imageUrl.Append(value);
}
void AddQueryString(string key, params IConvertible[] values)
=> AppendQueryString(key + '=' + string.Join(",", values.Select(x => x.ToString(CultureInfo.InvariantCulture))));
if (options.FocalPoint != null)
2021-08-06 14:22:27 +02:00
{
AddQueryString("rxy", options.FocalPoint.Left, options.FocalPoint.Top);
2021-08-06 14:22:27 +02:00
}
2020-02-09 11:12:29 -08:00
if (options.Crop != null)
2021-08-06 14:22:27 +02:00
{
AddQueryString("cc", options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom);
2021-08-06 14:22:27 +02:00
}
2020-02-09 11:12:29 -08:00
if (options.ImageCropMode.HasValue)
2021-08-06 14:22:27 +02:00
{
AddQueryString("rmode", options.ImageCropMode.Value.ToString().ToLowerInvariant());
2021-08-06 14:22:27 +02:00
}
if (options.ImageCropAnchor.HasValue)
2021-08-06 14:22:27 +02:00
{
AddQueryString("ranchor", options.ImageCropAnchor.Value.ToString().ToLowerInvariant());
2021-08-06 14:22:27 +02:00
}
if (options.Width.HasValue)
2021-08-06 14:22:27 +02:00
{
AddQueryString("width", options.Width.Value);
2021-08-06 14:22:27 +02:00
}
if (options.Height.HasValue)
2021-08-06 14:22:27 +02:00
{
AddQueryString("height", options.Height.Value);
2021-08-06 14:22:27 +02:00
}
if (options.Quality.HasValue)
2021-08-06 14:22:27 +02:00
{
AddQueryString("quality", options.Quality.Value);
2021-08-06 14:22:27 +02:00
}
if (string.IsNullOrWhiteSpace(options.FurtherOptions) == false)
2021-08-06 14:22:27 +02:00
{
AppendQueryString(options.FurtherOptions.TrimStart('?', '&'));
2021-08-06 14:22:27 +02:00
}
if (string.IsNullOrWhiteSpace(options.CacheBusterValue) == false)
2021-08-06 14:22:27 +02:00
{
AddQueryString("rnd", options.CacheBusterValue);
2021-08-06 14:22:27 +02:00
}
return imageUrl.ToString();
2020-02-09 11:12:29 -08:00
}
}
}