/
FriendlyImageCropperTemplateExtensions.cs
350 lines (337 loc) · 15 KB
/
FriendlyImageCropperTemplateExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Routing;
namespace Umbraco.Extensions;
public static class FriendlyImageCropperTemplateExtensions
{
private static IImageUrlGenerator ImageUrlGenerator { get; } =
StaticServiceProvider.Instance.GetRequiredService<IImageUrlGenerator>();
private static IPublishedValueFallback PublishedValueFallback { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedValueFallback>();
private static IPublishedUrlProvider PublishedUrlProvider { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedUrlProvider>();
/// <summary>
/// Gets the underlying image processing service URL by the crop alias (from the "umbracoFile" property alias) on the
/// IPublishedContent item.
/// </summary>
/// <param name="mediaItem">The IPublishedContent item.</param>
/// <param name="cropAlias">The crop alias e.g. thumbnail.</param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this IPublishedContent mediaItem,
string cropAlias,
UrlMode urlMode = UrlMode.Default) =>
mediaItem.GetCropUrl(cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode);
/// <summary>
/// Gets the underlying image processing service URL by the crop alias (from the "umbracoFile" property alias in the
/// MediaWithCrops content item) on the MediaWithCrops item.
/// </summary>
/// <param name="mediaWithCrops">The MediaWithCrops item.</param>
/// <param name="cropAlias">The crop alias e.g. thumbnail.</param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(this MediaWithCrops mediaWithCrops, string cropAlias, UrlMode urlMode = UrlMode.Default)
=> mediaWithCrops.GetCropUrl(cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode);
/// <summary>
/// Gets the crop URL by using only the specified <paramref name="imageCropperValue" />.
/// </summary>
/// <param name="mediaItem">The media item.</param>
/// <param name="imageCropperValue">The image cropper value.</param>
/// <param name="cropAlias">The crop alias.</param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The image crop URL.
/// </returns>
public static string? GetCropUrl(
this IPublishedContent mediaItem,
ImageCropperValue imageCropperValue,
string cropAlias,
UrlMode urlMode = UrlMode.Default)
=> mediaItem.GetCropUrl(imageCropperValue, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode);
/// <summary>
/// Gets the underlying image processing service URL by the crop alias using the specified property containing the
/// image cropper JSON data on the IPublishedContent item.
/// </summary>
/// <param name="mediaItem">The IPublishedContent item.</param>
/// <param name="propertyAlias">The property alias of the property containing the JSON data e.g. umbracoFile.</param>
/// <param name="cropAlias">The crop alias e.g. thumbnail.</param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this IPublishedContent mediaItem,
string propertyAlias,
string cropAlias,
UrlMode urlMode = UrlMode.Default) =>
mediaItem.GetCropUrl(propertyAlias, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode);
/// <summary>
/// Gets the underlying image processing service URL by the crop alias using the specified property containing the
/// image cropper JSON data on the MediaWithCrops content item.
/// </summary>
/// <param name="mediaWithCrops">The MediaWithCrops item.</param>
/// <param name="propertyAlias">The property alias of the property containing the JSON data e.g. umbracoFile.</param>
/// <param name="cropAlias">The crop alias e.g. thumbnail.</param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(this MediaWithCrops mediaWithCrops, string propertyAlias, string cropAlias, UrlMode urlMode = UrlMode.Default)
=> mediaWithCrops.GetCropUrl(propertyAlias, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode);
/// <summary>
/// Gets the underlying image processing service URL from the IPublishedContent item.
/// </summary>
/// <param name="mediaItem">The IPublishedContent item.</param>
/// <param name="width">The width of the output image.</param>
/// <param name="height">The height of the output image.</param>
/// <param name="propertyAlias">Property alias of the property containing the JSON data.</param>
/// <param name="cropAlias">The crop alias.</param>
/// <param name="quality">Quality percentage of the output image.</param>
/// <param name="imageCropMode">The image crop mode.</param>
/// <param name="imageCropAnchor">The image crop anchor.</param>
/// <param name="preferFocalPoint">
/// Use focal point, to generate an output image using the focal point instead of the
/// predefined crop.
/// </param>
/// <param name="useCropDimensions">
/// Use crop dimensions to have the output image sized according to the predefined crop
/// sizes, this will override the width and height parameters.
/// </param>
/// <param name="cacheBuster">
/// Add a serialized date of the last edit of the item to ensure client cache refresh when
/// updated.
/// </param>
/// <param name="furtherOptions">
/// These are any query string parameters (formatted as query strings) that the underlying image processing service
/// supports. For example:
/// <example><![CDATA[
/// furtherOptions: "bgcolor=fff"
/// ]]></example>
/// </param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this IPublishedContent mediaItem,
int? width = null,
int? height = null,
string propertyAlias = Constants.Conventions.Media.File,
string? cropAlias = null,
int? quality = null,
ImageCropMode? imageCropMode = null,
ImageCropAnchor? imageCropAnchor = null,
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string? furtherOptions = null,
UrlMode urlMode = UrlMode.Default)
=> mediaItem.GetCropUrl(
ImageUrlGenerator,
PublishedValueFallback,
PublishedUrlProvider,
width,
height,
propertyAlias,
cropAlias,
quality,
imageCropMode,
imageCropAnchor,
preferFocalPoint,
useCropDimensions,
cacheBuster,
furtherOptions,
urlMode);
/// <summary>
/// Gets the underlying image processing service URL from the MediaWithCrops item.
/// </summary>
/// <param name="mediaWithCrops">The MediaWithCrops item.</param>
/// <param name="width">The width of the output image.</param>
/// <param name="height">The height of the output image.</param>
/// <param name="propertyAlias">Property alias of the property containing the JSON data.</param>
/// <param name="cropAlias">The crop alias.</param>
/// <param name="quality">Quality percentage of the output image.</param>
/// <param name="imageCropMode">The image crop mode.</param>
/// <param name="imageCropAnchor">The image crop anchor.</param>
/// <param name="preferFocalPoint">
/// Use focal point, to generate an output image using the focal point instead of the
/// predefined crop.
/// </param>
/// <param name="useCropDimensions">
/// Use crop dimensions to have the output image sized according to the predefined crop
/// sizes, this will override the width and height parameters.
/// </param>
/// <param name="cacheBuster">
/// Add a serialized date of the last edit of the item to ensure client cache refresh when
/// updated.
/// </param>
/// <param name="furtherOptions">
/// These are any query string parameters (formatted as query strings) that the underlying image processing service
/// supports. For example:
/// <example><![CDATA[
/// furtherOptions: "bgcolor=fff"
/// ]]></example>
/// </param>
/// <param name="urlMode">The url mode.</param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this MediaWithCrops mediaWithCrops,
int? width = null,
int? height = null,
string propertyAlias = Constants.Conventions.Media.File,
string? cropAlias = null,
int? quality = null,
ImageCropMode? imageCropMode = null,
ImageCropAnchor? imageCropAnchor = null,
bool preferFocalPoint = false,
bool useCropDimensions = false,
bool cacheBuster = true,
string? furtherOptions = null,
UrlMode urlMode = UrlMode.Default)
=> mediaWithCrops.GetCropUrl(
ImageUrlGenerator,
PublishedValueFallback,
PublishedUrlProvider,
width,
height,
propertyAlias,
cropAlias,
quality,
imageCropMode,
imageCropAnchor,
preferFocalPoint,
useCropDimensions,
cacheBuster,
furtherOptions,
urlMode);
/// <summary>
/// Gets the underlying image processing service URL from the image path.
/// </summary>
/// <param name="imageUrl">The image URL.</param>
/// <param name="width">The width of the output image.</param>
/// <param name="height">The height of the output image.</param>
/// <param name="imageCropperValue">The JSON data from the Umbraco Core Image Cropper property editor.</param>
/// <param name="cropAlias">The crop alias.</param>
/// <param name="quality">Quality percentage of the output image.</param>
/// <param name="imageCropMode">The image crop mode.</param>
/// <param name="imageCropAnchor">The image crop anchor.</param>
/// <param name="preferFocalPoint">
/// Use focal point to generate an output image using the focal point instead of the
/// predefined crop if there is one.
/// </param>
/// <param name="useCropDimensions">
/// Use crop dimensions to have the output image sized according to the predefined crop
/// sizes, this will override the width and height parameters.
/// </param>
/// <param name="cacheBusterValue">
/// Add a serialized date of the last edit of the item to ensure client cache refresh when
/// updated.
/// </param>
/// <param name="furtherOptions">
/// These are any query string parameters (formatted as query strings) that the underlying image processing service
/// supports. For example:
/// <example><![CDATA[
/// furtherOptions: "bgcolor=fff"
/// ]]></example>
/// </param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this string imageUrl,
int? width = null,
int? height = null,
string? imageCropperValue = null,
string? cropAlias = null,
int? quality = null,
ImageCropMode? imageCropMode = null,
ImageCropAnchor? imageCropAnchor = null,
bool preferFocalPoint = false,
bool useCropDimensions = false,
string? cacheBusterValue = null,
string? furtherOptions = null)
=> imageUrl.GetCropUrl(
ImageUrlGenerator,
width,
height,
imageCropperValue,
cropAlias,
quality,
imageCropMode,
imageCropAnchor,
preferFocalPoint,
useCropDimensions,
cacheBusterValue,
furtherOptions);
/// <summary>
/// Gets the underlying image processing service URL from the image path.
/// </summary>
/// <param name="imageUrl">The image URL.</param>
/// <param name="cropDataSet">The crop data set.</param>
/// <param name="width">The width of the output image.</param>
/// <param name="height">The height of the output image.</param>
/// <param name="cropAlias">The crop alias.</param>
/// <param name="quality">Quality percentage of the output image.</param>
/// <param name="imageCropMode">The image crop mode.</param>
/// <param name="imageCropAnchor">The image crop anchor.</param>
/// <param name="preferFocalPoint">
/// Use focal point to generate an output image using the focal point instead of the
/// predefined crop if there is one.
/// </param>
/// <param name="useCropDimensions">
/// Use crop dimensions to have the output image sized according to the predefined crop
/// sizes, this will override the width and height parameters.
/// </param>
/// <param name="cacheBusterValue">
/// Add a serialized date of the last edit of the item to ensure client cache refresh when
/// updated.
/// </param>
/// <param name="furtherOptions">
/// These are any query string parameters (formatted as query strings) that the underlying image processing service
/// supports. For example:
/// <example><![CDATA[
/// furtherOptions: "bgcolor=fff"
/// ]]></example>
/// </param>
/// <returns>
/// The URL of the cropped image.
/// </returns>
public static string? GetCropUrl(
this string imageUrl,
ImageCropperValue cropDataSet,
int? width = null,
int? height = null,
string? cropAlias = null,
int? quality = null,
ImageCropMode? imageCropMode = null,
ImageCropAnchor? imageCropAnchor = null,
bool preferFocalPoint = false,
bool useCropDimensions = false,
string? cacheBusterValue = null,
string? furtherOptions = null)
=> imageUrl.GetCropUrl(
ImageUrlGenerator,
cropDataSet,
width,
height,
cropAlias,
quality,
imageCropMode,
imageCropAnchor,
preferFocalPoint,
useCropDimensions,
cacheBusterValue,
furtherOptions);
}