Skip to content

Instantly share code, notes, and snippets.

@komainu85
Created February 24, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komainu85/80c2a1873c5f0b67fa8d to your computer and use it in GitHub Desktop.
Save komainu85/80c2a1873c5f0b67fa8d to your computer and use it in GitHub Desktop.
Sitecore Get Image URL with Dimensions
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