|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Globalization;
|
| 4 | +using System.Linq; |
3 | 5 | using System.Text;
|
| 6 | +using SixLabors.ImageSharp; |
4 | 7 | using Umbraco.Cms.Core.Media;
|
5 | 8 | using Umbraco.Cms.Core.Models;
|
6 |
| -using Umbraco.Extensions; |
7 | 9 |
|
8 | 10 | namespace Umbraco.Cms.Infrastructure.Media
|
9 | 11 | {
|
| 12 | + /// <summary> |
| 13 | + /// Exposes a method that generates an image URL based on the specified options that can be processed by ImageSharp. |
| 14 | + /// </summary> |
| 15 | + /// <seealso cref="Umbraco.Cms.Core.Media.IImageUrlGenerator" /> |
10 | 16 | public class ImageSharpImageUrlGenerator : IImageUrlGenerator
|
11 | 17 | {
|
12 |
| - public IEnumerable<string> SupportedImageFileTypes => new[] { "jpeg", "jpg", "gif", "bmp", "png" }; |
| 18 | + /// <inheritdoc /> |
| 19 | + public IEnumerable<string> SupportedImageFileTypes { get; } |
13 | 20 |
|
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="ImageSharpImageUrlGenerator" /> class. |
| 23 | + /// </summary> |
| 24 | + /// <param name="configuration">The ImageSharp configuration.</param> |
| 25 | + public ImageSharpImageUrlGenerator(Configuration configuration) |
| 26 | + : this(configuration.ImageFormats.SelectMany(f => f.FileExtensions).ToArray()) |
| 27 | + { } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the <see cref="ImageSharpImageUrlGenerator" /> class. |
| 31 | + /// </summary> |
| 32 | + /// <param name="supportedImageFileTypes">The supported image file types/extensions.</param> |
| 33 | + /// <remarks> |
| 34 | + /// This constructor is only used for testing. |
| 35 | + /// </remarks> |
| 36 | + internal ImageSharpImageUrlGenerator(IEnumerable<string> supportedImageFileTypes) => SupportedImageFileTypes = supportedImageFileTypes; |
| 37 | + |
| 38 | + /// <inheritdoc/> |
14 | 39 | public string GetImageUrl(ImageUrlGenerationOptions options)
|
15 | 40 | {
|
16 |
| - if (options == null) return null; |
| 41 | + if (options == null) |
| 42 | + { |
| 43 | + return null; |
| 44 | + } |
17 | 45 |
|
18 |
| - var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty); |
| 46 | + var imageUrl = new StringBuilder(options.ImageUrl); |
19 | 47 |
|
20 |
| - if (options.FocalPoint != null) AppendFocalPoint(imageProcessorUrl, options); |
21 |
| - else if (options.Crop != null) AppendCrop(imageProcessorUrl, options); |
22 |
| - else if (options.DefaultCrop) imageProcessorUrl.Append("?anchor=center&mode=crop"); |
23 |
| - else |
| 48 | + bool queryStringHasStarted = false; |
| 49 | + void AppendQueryString(string value) |
24 | 50 | {
|
25 |
| - imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? ImageCropMode.Crop).ToString().ToLower()); |
| 51 | + imageUrl.Append(queryStringHasStarted ? '&' : '?'); |
| 52 | + queryStringHasStarted = true; |
26 | 53 |
|
27 |
| - if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToString().ToLower()); |
| 54 | + imageUrl.Append(value); |
28 | 55 | }
|
| 56 | + void AddQueryString(string key, params IConvertible[] values) |
| 57 | + => AppendQueryString(key + '=' + string.Join(",", values.Select(x => x.ToString(CultureInfo.InvariantCulture)))); |
29 | 58 |
|
30 |
| - var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format="); |
| 59 | + if (options.FocalPoint != null) |
| 60 | + { |
| 61 | + AddQueryString("rxy", options.FocalPoint.Left, options.FocalPoint.Top); |
| 62 | + } |
31 | 63 |
|
32 |
| - //Only put quality here, if we don't have a format specified. |
33 |
| - //Otherwise we need to put quality at the end to avoid it being overridden by the format. |
34 |
| - if (options.Quality.HasValue && hasFormat == false) imageProcessorUrl.Append("&quality=").Append(options.Quality); |
35 |
| - if (options.HeightRatio.HasValue) imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture)); |
36 |
| - if (options.WidthRatio.HasValue) imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture)); |
37 |
| - if (options.Width.HasValue) imageProcessorUrl.Append("&width=").Append(options.Width); |
38 |
| - if (options.Height.HasValue) imageProcessorUrl.Append("&height=").Append(options.Height); |
39 |
| - if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false"); |
40 |
| - if (!string.IsNullOrWhiteSpace(options.AnimationProcessMode)) imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode); |
41 |
| - if (!string.IsNullOrWhiteSpace(options.FurtherOptions)) imageProcessorUrl.Append(options.FurtherOptions); |
| 64 | + if (options.Crop != null) |
| 65 | + { |
| 66 | + AddQueryString("cc", options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom); |
| 67 | + } |
42 | 68 |
|
43 |
| - //If furtherOptions contains a format, we need to put the quality after the format. |
44 |
| - if (options.Quality.HasValue && hasFormat) imageProcessorUrl.Append("&quality=").Append(options.Quality); |
45 |
| - if (!string.IsNullOrWhiteSpace(options.CacheBusterValue)) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue); |
| 69 | + if (options.ImageCropMode.HasValue) |
| 70 | + { |
| 71 | + AddQueryString("rmode", options.ImageCropMode.Value.ToString().ToLowerInvariant()); |
| 72 | + } |
46 | 73 |
|
47 |
| - return imageProcessorUrl.ToString(); |
48 |
| - } |
| 74 | + if (options.ImageCropAnchor.HasValue) |
| 75 | + { |
| 76 | + AddQueryString("ranchor", options.ImageCropAnchor.Value.ToString().ToLowerInvariant()); |
| 77 | + } |
49 | 78 |
|
50 |
| - private void AppendFocalPoint(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options) |
51 |
| - { |
52 |
| - imageProcessorUrl.Append("?center="); |
53 |
| - imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(","); |
54 |
| - imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture)); |
55 |
| - imageProcessorUrl.Append("&mode=crop"); |
56 |
| - } |
| 79 | + if (options.Width.HasValue) |
| 80 | + { |
| 81 | + AddQueryString("width", options.Width.Value); |
| 82 | + } |
57 | 83 |
|
58 |
| - private void AppendCrop(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options) |
59 |
| - { |
60 |
| - imageProcessorUrl.Append("?crop="); |
61 |
| - imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(","); |
62 |
| - imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(","); |
63 |
| - imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(","); |
64 |
| - imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture)); |
65 |
| - imageProcessorUrl.Append("&cropmode=percentage"); |
| 84 | + if (options.Height.HasValue) |
| 85 | + { |
| 86 | + AddQueryString("height", options.Height.Value); |
| 87 | + } |
| 88 | + |
| 89 | + if (options.Quality.HasValue) |
| 90 | + { |
| 91 | + AddQueryString("quality", options.Quality.Value); |
| 92 | + } |
| 93 | + |
| 94 | + if (string.IsNullOrWhiteSpace(options.FurtherOptions) == false) |
| 95 | + { |
| 96 | + AppendQueryString(options.FurtherOptions.TrimStart('?', '&')); |
| 97 | + } |
| 98 | + |
| 99 | + if (string.IsNullOrWhiteSpace(options.CacheBusterValue) == false) |
| 100 | + { |
| 101 | + AddQueryString("rnd", options.CacheBusterValue); |
| 102 | + } |
| 103 | + |
| 104 | + return imageUrl.ToString(); |
66 | 105 | }
|
67 | 106 | }
|
68 | 107 | }
|
0 commit comments