Skip to content

Commit 2980ff9

Browse files
Add GetCropUrl overloads to better handle local crops only, improve null checks
1 parent 384646b commit 2980ff9

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

src/Umbraco.Web/ImageCropperTemplateCoreExtensions.cs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,25 @@ public static string GetCropUrl(this MediaWithCrops mediaWithCrops, string cropA
3333
return mediaWithCrops.GetCropUrl(imageUrlGenerator, cropAlias: cropAlias, useCropDimensions: true);
3434
}
3535

36-
[Obsolete("This method does not get the crops or cache buster value from the media item.")]
36+
[Obsolete("Use the GetCropUrl overload with the updated parameter order and note this implementation has changed to get the URL from the media item.")]
3737
public static string GetCropUrl(this IPublishedContent mediaItem, string cropAlias, IImageUrlGenerator imageUrlGenerator, ImageCropperValue imageCropperValue)
3838
{
39-
return mediaItem.Url().GetCropUrl(imageUrlGenerator, imageCropperValue, cropAlias: cropAlias, useCropDimensions: true);
39+
return mediaItem.GetCropUrl(imageCropperValue, cropAlias, imageUrlGenerator);
40+
}
41+
42+
/// <summary>
43+
/// Gets the crop URL by using only the specified <paramref name="imageCropperValue" />.
44+
/// </summary>
45+
/// <param name="mediaItem">The media item.</param>
46+
/// <param name="imageCropperValue">The image cropper value.</param>
47+
/// <param name="cropAlias">The crop alias.</param>
48+
/// <param name="imageUrlGenerator">The image URL generator.</param>
49+
/// <returns>
50+
/// The image crop URL.
51+
/// </returns>
52+
public static string GetCropUrl(this IPublishedContent mediaItem, ImageCropperValue imageCropperValue, string cropAlias, IImageUrlGenerator imageUrlGenerator)
53+
{
54+
return mediaItem.GetCropUrl(imageUrlGenerator, imageCropperValue, true, cropAlias: cropAlias, useCropDimensions: true);
4055
}
4156

4257
/// <summary>
@@ -134,7 +149,7 @@ public static string GetCropUrl(
134149
ImageCropRatioMode? ratioMode = null,
135150
bool upScale = true)
136151
{
137-
return mediaItem.GetCropUrl(imageUrlGenerator, null, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
152+
return mediaItem.GetCropUrl(imageUrlGenerator, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
138153
}
139154

140155
public static string GetCropUrl(
@@ -154,28 +169,31 @@ public static string GetCropUrl(
154169
ImageCropRatioMode? ratioMode = null,
155170
bool upScale = true)
156171
{
157-
return mediaWithCrops.MediaItem.GetCropUrl(imageUrlGenerator, mediaWithCrops.LocalCrops, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
172+
if (mediaWithCrops == null) throw new ArgumentNullException(nameof(mediaWithCrops));
173+
174+
return mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
158175
}
159176

160177
private static string GetCropUrl(
161178
this IPublishedContent mediaItem,
162179
IImageUrlGenerator imageUrlGenerator,
163180
ImageCropperValue localCrops,
164-
int? width,
165-
int? height,
166-
string propertyAlias,
167-
string cropAlias,
168-
int? quality,
169-
ImageCropMode? imageCropMode,
170-
ImageCropAnchor? imageCropAnchor,
171-
bool preferFocalPoint,
172-
bool useCropDimensions,
173-
bool cacheBuster,
174-
string furtherOptions,
175-
ImageCropRatioMode? ratioMode,
176-
bool upScale)
181+
bool localCropsOnly,
182+
int? width = null,
183+
int? height = null,
184+
string propertyAlias = Constants.Conventions.Media.File,
185+
string cropAlias = null,
186+
int? quality = null,
187+
ImageCropMode? imageCropMode = null,
188+
ImageCropAnchor? imageCropAnchor = null,
189+
bool preferFocalPoint = false,
190+
bool useCropDimensions = false,
191+
bool cacheBuster = true,
192+
string furtherOptions = null,
193+
ImageCropRatioMode? ratioMode = null,
194+
bool upScale = true)
177195
{
178-
if (mediaItem == null) throw new ArgumentNullException("mediaItem");
196+
if (mediaItem == null) throw new ArgumentNullException(nameof(mediaItem));
179197

180198
var cacheBusterValue = cacheBuster ? mediaItem.UpdateDate.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture) : null;
181199

@@ -184,8 +202,8 @@ private static string GetCropUrl(
184202

185203
var mediaItemUrl = mediaItem.MediaUrl(propertyAlias: propertyAlias);
186204

187-
// Only get crops when used
188-
if (imageCropMode == ImageCropMode.Crop || imageCropMode == null)
205+
// Only get crops from media when required and used
206+
if (localCropsOnly == false && (imageCropMode == ImageCropMode.Crop || imageCropMode == null))
189207
{
190208
// Get the default cropper value from the value converter
191209
var cropperValue = mediaItem.Value(propertyAlias);

src/Umbraco.Web/ImageCropperTemplateExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ public static class ImageCropperTemplateExtensions
3232

3333
public static string GetCropUrl(this MediaWithCrops mediaWithCrops, string cropAlias) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaWithCrops, cropAlias, Current.ImageUrlGenerator);
3434

35-
[Obsolete("This method does not get the crops or cache buster value from the media item.")]
36-
public static string GetCropUrl(this IPublishedContent mediaItem, string cropAlias, ImageCropperValue imageCropperValue) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, cropAlias, Current.ImageUrlGenerator, imageCropperValue);
35+
[Obsolete("Use the GetCropUrl overload with the updated parameter order and note this implementation has changed to get the URL from the media item.")]
36+
public static string GetCropUrl(this IPublishedContent mediaItem, string cropAlias, ImageCropperValue imageCropperValue) => mediaItem.GetCropUrl(imageCropperValue, cropAlias);
37+
38+
/// <summary>
39+
/// Gets the crop URL by using only the specified <paramref name="imageCropperValue" />.
40+
/// </summary>
41+
/// <param name="mediaItem">The media item.</param>
42+
/// <param name="imageCropperValue">The image cropper value.</param>
43+
/// <param name="cropAlias">The crop alias.</param>
44+
/// <returns>
45+
/// The image crop URL.
46+
/// </returns>
47+
public static string GetCropUrl(this IPublishedContent mediaItem, ImageCropperValue imageCropperValue, string cropAlias) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, imageCropperValue, cropAlias, Current.ImageUrlGenerator);
3748

3849
/// <summary>
3950
/// Gets the ImageProcessor URL by the crop alias using the specified property containing the image cropper Json data on the IPublishedContent item.

src/Umbraco.Web/UrlHelperRenderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
283283

284284
public static IHtmlString GetCropUrl(this UrlHelper urlHelper, ImageCropperValue imageCropperValue, string cropAlias, bool htmlEncode = true)
285285
{
286-
if (imageCropperValue == null) return EmptyHtmlString;
286+
if (imageCropperValue == null || string.IsNullOrEmpty(imageCropperValue.Src)) return EmptyHtmlString;
287287

288288
var url = imageCropperValue.Src.GetCropUrl(imageCropperValue, cropAlias: cropAlias, useCropDimensions: true);
289289

@@ -306,7 +306,7 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
306306
bool upScale = true,
307307
bool htmlEncode = true)
308308
{
309-
if (imageCropperValue == null) return EmptyHtmlString;
309+
if (imageCropperValue == null || string.IsNullOrEmpty(imageCropperValue.Src)) return EmptyHtmlString;
310310

311311
var url = imageCropperValue.Src.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale);
312312

0 commit comments

Comments
 (0)