0

I am having one multimedia component with a jpeg image, and I inserted that image in my banner component. I need to have a TBB, which should renders and resize the image according to the layout(Mobile, Tablet and Desktop).

Note: I don't want to have three images for three different layouts.

  • Welcome to Tridion StackExchange!, Please explain bit more detail about your implementation is it uses DXA or web farm? – Velmurugan May 13 at 9:16
  • Tridion razor templating, web forms. – user1405 May 13 at 10:08
  • If you are looking for on the fly image resize then you need to consider to write an Image Handler in web app side to handle. In CMS side you can achieve by writing custom resize cropping image TBB based on your custom width and heights but it will generate multiple images for different layouts for a single source image – Velmurugan May 13 at 10:29
  • @Velmurugan: Yes boss, I will have one Image comp, I will insert it in component, while rendering component according to the layout I need the image to be resized. How to achieve it in TBB?, it is not possible to handle from web app. – user1405 May 13 at 10:32
2

To create and use Custom Dreamweaver Functions from a Razor Mediator.

Refer to this below example, you can create one specific to your case.

  1. Create a custom class in C# using the TOM.NET API based on ITemplate and IFunctionSource
[TemplateCallable]
public String GetThumbnail(String compURI, String Prefix, int Width, int Height)
{
    Component comp = mEngine.GetObject(compURI) as Component;

    if (comp != null && comp.BinaryContent != null)
    {
        try
        {
            // Output the existing image to a memory stream
            using (MemoryStream stream = new MemoryStream())
            {
                comp.BinaryContent.WriteToStream(stream);

                using (Image image = Image.FromStream(stream))
                {
                    int newWidth = Width;
                    int newHeight = Height;

                    if (image.Height > image.Width)
                    {
                        newWidth = (int)Math.Round(((double)image.Width / (double)image.Height) * newHeight);
                    }
                    else
                    {
                        newHeight = (int)Math.Round(((double)image.Height / (double)image.Width) * newWidth);
                    }

                    using (Bitmap bitmap = new Bitmap((int)newWidth, (int)newHeight, image.PixelFormat))
                    {
                        using (Graphics graphic = Graphics.FromImage(bitmap))
                        {
                            graphic.SmoothingMode = SmoothingMode.HighQuality;
                            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                            graphic.DrawImage(image, 0, 0, bitmap.Width, bitmap.Height);
                        }

                        using (MemoryStream streamOut = new MemoryStream())
                        {
                            bitmap.Save(streamOut, ImageFormat.Jpeg);

                            Binary binary = Engine.PublishingContext.RenderedItem.AddBinary(streamOut, String.Format("{0}_thumb_tcm{1}_{2}.jpg", Path.GetFileNameWithoutExtension(comp.BinaryContent.Filename), comp.Id.PublicationId, comp.Id.ItemId), Prefix, comp, "image/jpeg");

                            return binary.Url;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Error(ex.Message);
        }
    }

    return String.Empty;
}
  1. Create the Strong-name and add it to the GAC using the gacutil.exe.

  2. Edit your environment Tridion.ContentManager.config and find the element and inside it, the element. Add your class dll reference.

    <tridion.templating>
        <functionSources>
            <functionSource type="YourCompany.Tridion.Templating.YourCustomClassExtensions" assembly="MyCustomClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddfc8957d6e5ee6b" />
        </functionSources>
    </tridion.templating>
  1. Restart SDL Tridion services and COM+

  2. In your TBB Razor template, you can call your template callable function as below

    @{ 
     var dw = new YourCompany.Tridion.Templating.YourCustomClassExtensions();
     dw.Initialize(TridionHelper.Engine, TridionHelper.Package);  
    }
    @dw.GetThumbnail(cp.Component.Fields.Media.Id,"thumb",100,100)

I hope it helps.

  • Thank you boss will try it. – user1405 May 14 at 8:30
1

As Velu says, these days it's much more common to do this in the web application, perhaps using Tridion's Contextual Image Delivery

If you want to do it in templating, this is also possible. In .NET you can make use of System.Drawing.Bitmap to do things like resizing. Tridion's AddBinary method has overloads that support variants, which allows you to make different variations on a single item available to the Content Delivery APIs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.