Skip to main content

Publishing Images as Variants

For several versions (was it 5.3?) it is possible to publish Multimedia Components as variants. It means that in template code, we can choose to publish a representation of the MMComponent instead of the original. When unpublishing a MMComponent, all its variants are unpublished as well.

A big use case of variants is publishing images with different resolutions. In Tridion, we keep only one version of the MMComponent (i.e. the one with highest resolution), and during publishing we can produce as many variants as we need via templating. This post details this use case.

I wrote a TBB that performs the resize. The TBB takes 4 parameters:

  • Width - the new width (in pixels) of the image (optional);
  • Height - the new height (in pixels) of the image (optional);
  • Aspect Ratio - Yes/No whether to keep the original's aspect ratio (boolean, optional);
  • ImageTcmUri - the TcmUri of the image (mandatory);
When Aspect Ratio = Yes, either Width or Height need be specified. Otherwise, both Width and Height are required.

Read Parameters
string val = m_Package.GetValue("Width");
width = String.IsNullOrEmpty(val) ? -1 : Convert.ToInt32(val);

val = m_Package.GetValue("Height");
height = String.IsNullOrEmpty(val) ? -1 : Convert.ToInt32(val);

imageTcmUri = m_Package.GetValue("ImageTcmUri");
if (String.IsNullOrEmpty(imageTcmUri))
     throw new Exception("Mandatory parameter ImageTcmUri cannot be empty");
else if (!TcmUri.IsValid(imageTcmUri))
     imageTcmUri = m_Package.GetValue(imageTcmUri); // double lookup
     if (String.IsNullOrEmpty(imageTcmUri) || !TcmUri.IsValid(imageTcmUri))
          throw new Exception("Invalid parameter ImageTcmUri");

aspectRatio = "Yes".Equals(m_Package.GetValue("AspectRatio"));

Main Logic

First, I read the MMComponent from Engine, get its BinaryContent, then create a bitmap (System.Drawing.Bitmap) containing the original image.

For simplicity sake, I removed a lot of checks from the code below. Obviously, the code should be safe, so do implement checks like mmc != null, binaryContent != null, MMC is actually an image (check its mime-type) before loading it into a Bitmap.

Next, the original bitmap is resized. I will present this method in the following section. The resized bitmap is then saved into a Stream. Make sure to implement the other mime-types here as well!

Finally, the resized image is then published using the RenderedItem.AddBinary() method, which takes as input the new image stream, filename, variant id, MMComponent and mime-type.

Sample code:
Component mmc = m_Engine.GetObject(imageTcmUri) as Component;
BinaryContent binaryContent = mmc.BinaryContent;
byte[] binaryBytes = binaryContent.GetByteArray();
Bitmap bitmap = new Bitmap(new MemoryStream(binaryBytes));

Bitmap resizedBitmap = ResizeBitmap(bitmap);

MemoryStream resizedStream = new MemoryStream();
resizedBitmap.Save(resizedStream, ImageFormat.Jpeg);

string newFilename = GetNewFilename();
string variantId = GetVariant();
Binary binary = m_Engine.PublishingContext.RenderedItem.AddBinary(
    resizedStream, newFilename, variantId, mmc,

Resize Bitmap

This method is the one having nothing to do with Tridion. Is simply using the .NET framework System.Drawing namespaces to produce a new resized Bitmap.

The interesting parts are just the last 6 lines of code, anything before that is just computation of the new image width and height.

The good part about this function is that it preserves the alpha channel on transparent PNGs, thus no special treatment case is necessary. I tested this function on JPG, GIF, PNG and BMP.

private Bitmap ResizeBitmap(Bitmap sourceBitmap)
     int destWidth = width;
     int destHeight = height;

     if (aspectRatio)
          int sourceWidth = sourceBitmap.Width;
          int sourceHeight = sourceBitmap.Height;

          float nPercent = 0;
          float nPercentW = 0;
          float nPercentH = 0;

          nPercentW = ((float)width / (float)sourceWidth);
          nPercentW = nPercentW < 0 ? 1 : nPercentW;
          nPercentH = ((float)height / (float)sourceHeight);
          nPercentH = nPercentH < 0 ? 1 : nPercentH;

          if (nPercentH < nPercentW)
              nPercent = nPercentH;
              nPercent = nPercentW;

          destWidth = (int)(sourceWidth * nPercent);
          destHeight = (int)(sourceHeight * nPercent);
          if (width < 0)
              destWidth = sourceBitmap.Width;
          if (height < 0)
              destHeight = sourceBitmap.Height;

     Bitmap resultBitmap = new Bitmap(destWidth, destHeight);
     Graphics graphicsd = Graphics.FromImage((Image)resultBitmap);
     graphicsd.InterpolationMode = InterpolationMode.HighQualityBicubic;

     graphicsd.DrawImage(sourceBitmap, 0, 0, destWidth, destHeight);

     return resultBitmap;


Popular posts from this blog

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio

A Implementation - Custom Binary Publisher

The default way to publish binaries in DD4T is implemented in class DD4T.Templates.Base.Utils.BinaryPublisher and uses method RenderedItem.AddBinary(Component) . This produces binaries that have their TCM URI as suffix in their filename. In my recent project, we had a requirement that binary file names should be clean (without the TCM URI suffix). Therefore, it was time to modify the way DD4T was publishing binaries. The method in charge with publishing binaries is called PublishItem and is defined in class BinaryPublisher . I therefore extended the BinaryPublisher and overrode method PublishItem. public class CustomBinaryPublisher : BinaryPublisher { private Template currentTemplate; private TcmUri structureGroupUri; In its simplest form, method PublishItem just takes the item and passes it to the AddBinary. In order to accomplish the requirement, we must specify a filename while publishing. This is the file name part of the binary path of Component.BinaryConten

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the