Created
February 24, 2015 18:41
-
-
Save komainu85/80c2a1873c5f0b67fa8d to your computer and use it in GitHub Desktop.
Sitecore Get Image URL with Dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GetImageUrl(this MediaItem mediaItem, int maxWidth = 0, int maxHeight = 0) | |
{ | |
return GetMediaUrl(mediaItem) + CreateImageSizeQuery(maxWidth, maxHeight); | |
} | |
public static string GetMediaUrl(this MediaItem mediaItem) | |
{ | |
var mediaUrl = MediaManager.GetMediaUrl(mediaItem); | |
return mediaUrl.Contains(MediaManager.MediaLinkPrefix) | |
? mediaUrl | |
: MediaManager.MediaLinkPrefix + mediaUrl; | |
} | |
private static string CreateImageSizeQuery(int width = 0, int height = 0) | |
{ | |
var heightStr = ""; | |
var widthStr = ""; | |
if (width > 0) | |
{ | |
widthStr = "mw=" + width; | |
} | |
if (height > 0) | |
{ | |
heightStr = widthStr.Length > 0 ? "&mh=" + height : "mh=" + height; | |
} | |
return "?" + widthStr + heightStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment