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,
    binaryContent.MultimediaType.MimeType);

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;
          }
          else
          {
              nPercent = nPercentW;
          }

          destWidth = (int)(sourceWidth * nPercent);
          destHeight = (int)(sourceHeight * nPercent);
     }
     else
     {
          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);
     graphicsd.Dispose();

     return resultBitmap;
}


Comments

Walter said…
Loved reading this, thanks