Skip to content

Instantly share code, notes, and snippets.

@vipervf1
Last active August 29, 2015 14:21
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 vipervf1/9a7068d66c2144954958 to your computer and use it in GitHub Desktop.
Save vipervf1/9a7068d66c2144954958 to your computer and use it in GitHub Desktop.
Gist's for the Marcel Digital Blog post about creating a ImageGen HTML Helper.
<img src='@Umbraco.ImageGenUrl(Model.Content.GetPropertyValue<int>("image"), new { width = 200, height = 270, crop = "resize" })'
alt="Image" />
<img src='@Umbraco.ImageGenUrl(Model.Content.GetPropertyValue<int>("image"), new { text = "Marcel%20Digital", fontcolor = "aqua", align = "center" })'
alt="Image" />
<img src='@Umbraco.ImageGenUrl(Model.Content.GetPropertyValue<int>("image"), new { rotate = 30 })'
alt="Image" />
<img src="/imagegen.ashx?image=/img/image.png&width=580&height=270&crop=resize"
alt="Image"/>
<img src='@Umbraco.ImageGenUrl(Model.Content.GetPropertyValue<int>("image"), new { width = 580, height = 270, crop = "resize" })'
alt="Image"/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using umbraco.NodeFactory;
using Umbraco.Core.Models;
using Umbraco.Web;
namespace MarcelDigital.Umbraco.Helpers
{
public static class UmbracoHelpers
{
/// <summary>
/// Creates the url from the media id to run an image through the image gen plugin
/// </summary>
/// <param name="helper"></param>
/// <param name="imageId">Umbraco media id of the image</param>
/// <param name="options">Options to pass to the image gen plugin</param>
/// <returns></returns>
public static String ImageGenUrl(this UmbracoHelper helper, int imageId,
object options) {
string imageUrl = helper.Media(imageId).Url;
return helper.ImageGenUrl(imageUrl, options);
}
/// <summary>
/// Creates the url to run an image through the image gen plugin
/// </summary>
/// <param name="helper"></param>
/// <param name="imageUrl">Url to the image that needs to be proccessed</param>
/// <param name="options">Options to pass to the image gen plugin</param>
/// <returns></returns>
public static String ImageGenUrl(this UmbracoHelper helper, string imageUrl,
object options) {
var baseUrl = "/imagegen.ashx?";
var optionsDictionary = new RouteValueDictionary(options);
optionsDictionary.Add("image", HttpUtility.UrlEncode(imageUrl));
return baseUrl + String.Join("&", optionsDictionary.Select(o => String.Concat(o.Key, "=", o.Value)).ToList());
}
}
}
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
...
<add namespace="MarcelDigital.Umbraco.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment