Search code examples
tridiontridion-2011

Images are getting published with TCM id appended with the image name


Mode of publishing - static

I'm trying to publish images, but the issue is whenever I publish those images, their TCM URI is appended to their name (i.e if image name is example and its TCM URI is like tcm:1-115, image filename becomes example_tcm1-115).

I have written the following code:

public void Transform(Engine engine, Package package)
{
    Filter MMCompFilter = new Filter();
    MMCompFilter.Conditions["ItemType"] = Tridion.ContentManager.ItemType.Component;
    Folder folder = engine.GetObject("tcm:1-1-2") as Folder;

    foreach (Component MMcomp in folder.GetItems(MMCompFilter))
    {
        Binary binary = engine.PublishingContext.RenderedItem.AddBinary(MMcomp);
        String binaryurl = binary.Url;
        char[] array = binaryurl.ToCharArray();
        Array.Reverse(array);
        string obj = new string(array);
        string final = newImagepath(obj);
        char[] array2 = final.ToCharArray();
        Array.Reverse(array2);
        string obj2 = new string(array2);

        package.PushItem("Image", package.CreateHtmlItem(obj2));
    }

    public string newImagepath(string filePath)
    {
        int formatIndex =filePath.IndexOf(".");
        string format= filePath.Substring(0,formatIndex);
        int finalPath=filePath.IndexOf("_");
        string newPath=filePath.Substring((finalPath+1));
        return (format+"."+newPath);
    }
}

I want to publish images without the TCM URI appended to it. Plz suggest how can it be done.


Solution

  • Simplest is always best.

    In your TBB, just push the individual images to the package:

    package.PushItem(package.CreateMultimediaItem(component.Id));
    

    Then use the "PublishBinariesInPackage" TBB to publish these images to your presentation server.