Skip to main content

Handling images and on the fly re-sizing in DD4T

When it comes with DD4T we have plenty of control on the data and we can decided on how we wanted the page to be rendered.
The traditional approach of having difference size of same image is replaced in DD4T to have one single image and generate the thumbnail on the fly. In this approach, we will have only one image published in tridion, and rest will be created on the fly based on the request.

Just by adding below configuration on the web.config modules session will enable this feature
<modules runAllManagedModulesForAllRequests="true">
  <add name="BinaryModule" 
       type="DD4T.Web.Binaries.BinaryDistributionModule" />
 </modules>
The BinaryDistributionModule process the request as below
  1. Process the Binary Request
  2. Check if the file is already in the file system.
    1. If file is available compare the last published date of the binary against the broker.
    2. If the file is updated, update the existing file in the file system.
    3. If the file is removed, delete the existing file in the file system.
  3. If the file is not there in the file system.
    1. Query the broker and create the file on local file system
  4. Deliver the file.

The below Html helper method helps to generate thumbnails as below.
<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url.ResizeToWidth(150)" />
<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url.ResizeToWidthAndHeight(150,150)" />
This will generate the images as
<img src=" /images/sample_image_tcm4-102_w150.jpg" />
<img src=" /images/sample_image_tcm4-102_w150_h150.jpg" />

The width and height feature has a bug in the framework, you can extract the BinaryDistributionModule and BinaryFileManager Class from the DD4T.Web project and bit customization on the method "private string StripDimensions(string path, out Dimensions dimensions)" will solve the issue, This method can be found in BinaryFileManager.cs. Update this method as in below would work for both width and height.

Later update the web.config to use the updated custom Binary Module, instead of default one,

Comments

Popular posts from this blog

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1

Create Document Dynamically (Umbraco)

You can create document and publish the content during run time, Below code snippets helps you to do it. public class UmbracoUtils { /// <summary> /// Create and publish the document programatically /// </summary> /// <param name="nodeName"></param> /// <param name="properties"></param> /// <param name="documentType"></param> /// <param name="parentId"></param> /// <returns>node id</returns> public static int CreateDocument(string nodeName, Dictionary<string, string> properties, int documentType, int parentId, List<string> roles = null) { DocumentType dt = new DocumentType(documentType); umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0); // Create the document Document d = Document.MakeNew(nodeName, dt, u, parentId); // Add values...

Beyond Solo Assistants: Google's Vision for AI Teamwork (Agent-to-Agent Collaboration)

We talk a lot about AI assistants like Google Assistant or chatbots answering our questions. They're pretty smart on their own, right? But imagine if they could team up, combine their unique skills, and tackle really complex problems together, just like a human team does. That's the core idea behind a super exciting area Google and others in the AI world are exploring: Agent-to-Agent (A2A) communication and collaboration. Think of it less as a single product called "Agent2Agent" and more as the science and engineering of building AI teams. Ready to explore why this is such a big deal? Let's break it down! First Off: What Even is an AI Agent? Think of an AI agent as a specialized digital helper. It's a piece of software designed to: Perceive: Understand its environment (text, images, data, user requests). Reason: Figure out the best course of action based on its goals and knowledge. Act: Perform tasks (answer questions, writ...