Sitecore Image Parameters

The Sitecore media library stores (per default) all assets (images, documents, …) in its own database rather than on disk. All assets are then streamed using an HttpHandler. All image links goes to an *.ashx handler which then returns the image. You can apply parameters to this handler, allowing you to modify the output (sizing, scaling, …).

The possibility to resize the output is especially useful as you allow users to upload images in any size and still stream the image in the size you need.

Scaled Image

Same image scaled to different sizes.

The example above demonstrates how to get the same uploaded image in different sizes. Sitecore caches the modified image so sizing only has a minor performance impact the first time you request the image.

You can apply parameters directly to the URL.

Here is an example with using an image called “brian.ashx”:
This will give you the original image: http://yourwebsiite/~/media/brian.ashx
This will give you the image with width = 50 px: http://yourwebsiite/~/media/brian.ashx?w=50

The most common parameters are:

  • w = Image width
  • h = Image height
  • as = Allow stretch. Set as=1 to allow image to be larger than original. Set as=0 to disallow image to be upscaled, filling any  space with a color (default is black).
  • bc = The background color of the fill when as=0. bc=000000 is black and bc=ffffff is white.
  • sc = scale image. Use decimal numbers. sc=1 is the original size. sc=1.5 is 150% above normal size.

You can see all parameters here: http://sdn.sitecore.net/Articles/XSL/5%203%20Enhancements/Image%20Enhancements.aspx

If you use XSLT to render your images, you can apply the parameters on the sc:image directly:

<sc:image field="MyImage" width="150" height="100" bc="ffffff"/>

If you use UserControls (.ascx) to render your images, you can register the following tagprefix to access sc:Image and sc:FieldRenderer:

<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

The namespace Sitecore.Web.UI.WebControls contains controls for rendering many different Sitecore types directly, including images.

Unfortunately the The sc:Image only supports MaxWidth, MaxHeight and Scale:

<%@ Control Language="c#" AutoEventWireup="true" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

<div>
  <sc:Image ID="image" Field="MyImageField" MaxWidth="100" runat="server" />
</div>

But you can use the sc:FieldRenderer and apply the parameters to the Parameters field:

<%@ Control Language="c#" AutoEventWireup="true" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>



<div>
  <sc:FieldRenderer ID="field" FieldName="MyImageField" Parameters="w=100&h=200&as=1" runat="server" />
</div>

Advertisement

4 Responses to “Sitecore Image Parameters”

  1. Bryan Says:

    Hey Brian, how do I get Sitecore to NOT put black borders on a scaled-down image? I’ve got an image about 800×400… and I want to display a thumbnail that is 35×35. But my scaled down image keeps the aspect ration, even when I set as=1. Suggestion?

  2. briancaos Says:

    Sitecore will only display images in the original aspect ratio, and will therefore add a black border where the ratio does not match.
    Your only option (besides replacing the Sitecore.Resources.Media.ResizeProcessor in the getMediaStream pipeline with your own resizer) is to apply the correct color as background with the “bc” parameter.

  3. Sitecore Validators: Validating image width, height and aspect ratio « Brian Pedersen’s Sitecore and .NET Blog Says:

    [...] Posts Create a Google style paging component in XSLTSitecore Image ParametersUsing Sitecore Field ValidatorsThe .ashx extension – Writing your own HttpHandlerGoogle Maps [...]

  4. Sean Holmesby Says:

    Hey Brian,
    Just wanted to add that our company, Igloo Digital, has a shared source module for image cropping, which is also a handy image parameter. Details here: http://blog.igloo.com.au/?p=3431
    Cheers.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 72 other followers