Its been a busy few months, so I have not had much time to blog
but I have been building up a list of short and (hopefully)
helpful little blog posts.
I have been really getting into Umbraco Razor recently, and on a
site we have just finished where we had a lot of images we were
pulling out dynamically from the media section and they had to be
resized differently. So I created this little image helper
class which uses a mixture of Examine & Imagegen to make a
really easy way to get images without too much of a performance
hit.
NOTE: You need
to have Imagegen installed to use this
It allows you to optionally resize if you want, also greyscale
if you want (Greyscale is an imagegen pro option) and all you pass
in is the Media ID - You simple call it in your Razor Macros like
so
Get Image Fast - No Resize Or GreyScale
@ImageHelper.ReturnResizedImage(Convert.ToInt32(MEDIAID), null)
Get Resized Image To 300px width
@ImageHelper.ReturnResizedImage(Convert.ToInt32(MEDIAID), 300)
Get Resized Image To 300px & GreyScale
It
@ImageHelper.ReturnResizedImage(Convert.ToInt32(MEDIAID), 300, true)
The Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.XPath;
using Examine;
using umbraco;
public static class ImageHelper
{
/// <summary>
/// Returns a resized image quickly, optional resize and also optional greyscales it
/// </summary>
/// <param name="mediaid">Umbraco Media ID</param>
/// <param name="size">Optional width if you want to resize</param>
/// <param name="greyscale">Optional greyscale</param>
/// <returns>Full Url of image with imagegen parameters</returns>
public static string ReturnResizedImage(int mediaid, int? size, bool greyscale = false)
{
if (mediaid <= 0) return null;
const string imageResizeFormat = "/ImageGen.ashx?image={0}";
const string grayScaleFormat = "&ColorMode=Grayscale";
var widthFormat = string.Concat("&width=", size);
var newImageUrl = GetUmbracoMedia(mediaid).Values["umbracoFile"];
if (size == null & !greyscale)
{
return newImageUrl;
}
if (size != null)
{
newImageUrl = string.Concat(string.Format(imageResizeFormat, newImageUrl), widthFormat);
}
if (greyscale)
{
newImageUrl = string.Concat(newImageUrl, grayScaleFormat);
}
return newImageUrl;
}
/// <summary>
/// Fastest way to get media from Umbraco
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private static MediaValues GetUmbracoMedia(int id)
{
//first check in Examine as this is WAY faster
var criteria = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"].CreateSearchCriteria("media");
var filter = criteria.Id(id);
var results = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"].Search(filter.Compile());
if (results.Any())
{
return new MediaValues(results.First());
}
var media = library.GetMedia(id, false);
if (media != null && media.Current != null)
{
media.MoveNext();
return new MediaValues(media.Current);
}
return null;
}
}
#region Helper Media Values Class - Taken from Shannons blog (I think)
class MediaValues
{
public MediaValues(XPathNavigator xpath)
{
if (xpath == null) throw new ArgumentNullException("xpath");
Name = xpath.GetAttribute("nodeName", "");
Values = new Dictionary<string, string>();
var result = xpath.SelectChildren(XPathNodeType.Element);
while (result.MoveNext())
{
if (result.Current != null && !result.Current.HasAttributes)
{
Values.Add(result.Current.Name, result.Current.Value);
}
}
}
public MediaValues(SearchResult result)
{
if (result == null) throw new ArgumentNullException("result");
Name = result.Fields["nodeName"];
Values = result.Fields;
}
public string Name { get; private set; }
public IDictionary<string, string> Values { get; private set; }
}
#endregion