2020-09-22 15:06:03 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
2020-02-07 15:01:03 -08:00
|
|
|
|
using System.Text;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Cms.Core.Media;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-02-09 11:26:22 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2020-02-07 15:01:03 -08:00
|
|
|
|
|
2021-02-12 12:33:52 +01:00
|
|
|
|
namespace Umbraco.Cms.Infrastructure.Media
|
2020-02-07 15:01:03 -08:00
|
|
|
|
{
|
2020-03-30 21:27:35 +02:00
|
|
|
|
public class ImageSharpImageUrlGenerator : 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" };
|
|
|
|
|
|
|
2020-02-07 15:01:03 -08:00
|
|
|
|
public string GetImageUrl(ImageUrlGenerationOptions options)
|
|
|
|
|
|
{
|
2020-02-08 11:05:14 -08:00
|
|
|
|
if (options == null) return null;
|
|
|
|
|
|
|
2020-02-07 15:01:03 -08:00
|
|
|
|
var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);
|
|
|
|
|
|
|
2020-02-09 11:12:29 -08:00
|
|
|
|
if (options.FocalPoint != null) AppendFocalPoint(imageProcessorUrl, options);
|
|
|
|
|
|
else if (options.Crop != null) AppendCrop(imageProcessorUrl, options);
|
|
|
|
|
|
else if (options.DefaultCrop) imageProcessorUrl.Append("?anchor=center&mode=crop");
|
2020-02-07 15:01:03 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-05-20 17:39:07 +02:00
|
|
|
|
imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? ImageCropMode.Crop).ToString().ToLower());
|
2020-02-07 15:01:03 -08:00
|
|
|
|
|
2020-05-20 17:39:07 +02:00
|
|
|
|
if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToString().ToLower());
|
2020-02-07 15:01:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");
|
|
|
|
|
|
|
|
|
|
|
|
//Only put quality here, if we don't have a format specified.
|
|
|
|
|
|
//Otherwise we need to put quality at the end to avoid it being overridden by the format.
|
2020-03-31 16:28:13 +02:00
|
|
|
|
if (options.Quality.HasValue && hasFormat == false) imageProcessorUrl.Append("&quality=").Append(options.Quality);
|
|
|
|
|
|
if (options.HeightRatio.HasValue) imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
if (options.WidthRatio.HasValue) imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
if (options.Width.HasValue) imageProcessorUrl.Append("&width=").Append(options.Width);
|
|
|
|
|
|
if (options.Height.HasValue) imageProcessorUrl.Append("&height=").Append(options.Height);
|
2020-02-08 11:05:14 -08:00
|
|
|
|
if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false");
|
2020-03-31 16:28:13 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.AnimationProcessMode)) imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.FurtherOptions)) imageProcessorUrl.Append(options.FurtherOptions);
|
2020-02-07 15:01:03 -08:00
|
|
|
|
|
|
|
|
|
|
//If furtherOptions contains a format, we need to put the quality after the format.
|
2020-03-31 16:28:13 +02:00
|
|
|
|
if (options.Quality.HasValue && hasFormat) imageProcessorUrl.Append("&quality=").Append(options.Quality);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.CacheBusterValue)) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
|
2020-02-07 15:01:03 -08:00
|
|
|
|
|
|
|
|
|
|
return imageProcessorUrl.ToString();
|
|
|
|
|
|
}
|
2020-02-09 11:12:29 -08:00
|
|
|
|
|
|
|
|
|
|
private void AppendFocalPoint(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
|
|
|
|
|
|
{
|
2021-07-08 14:11:30 +02:00
|
|
|
|
imageProcessorUrl.Append("?rxy=");
|
2020-02-09 11:12:29 -08:00
|
|
|
|
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(",");
|
|
|
|
|
|
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AppendCrop(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
imageProcessorUrl.Append("?crop=");
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
2020-02-07 15:01:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|