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

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: I stumbled upon the following post from 2008 (!!!), , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a