Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to create a custom rendition to the images in the AEM DAM programatically using java in AEM6.3?

Avatar

Level 2

I have a requirement where I need to create a custom rendition for the images in the AEM DAM that has already being uploaded in the DAM and store those custom renditions in the same path of the image in the DAM programmatically using java in aem6.3.

Any idea/approach to proceed as solution to the above questions?

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi,

I would suggest create a workflow process and make use of this RenditionMaker api and Assethandler Api to create our own renditions for a particular image asset.

Kindly check out this code. It would be much helpful.

@Properties({

@Property(name = "process.label", value = "Custom Image Rendition Process") })

@Component

@Service

public class CustomImageRendition implements WorkflowProcess {

  private static final Logger logger = Logger.getLogger(CustomImageRendition.class.getName());

  @Reference

  protected ResourceResolverFactory resourceResolverFactory;

  @Reference

  RenditionMaker renditionMaker;

  @Reference

  AssetHandler assetHandler;

  ResourceResolver resourceResolver;

  @Override

  public void execute(WorkItem workItem, WorkflowSession session, MetaDataMap args) throws WorkflowException {

  try

      {

    String assetPath = null;

    // Get the resource resolver using system user and user mapper service

        Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver");

          resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);

          String payloadString = workItem.getWorkflowData().getPayload().toString();

       logger.info("payload path"+payloadString);

    

        // convert the payload path into a Resource

        Resource damResource = resourceResolver.resolve(payloadString);

      

        if (damResource != null){

        logger.info("the damResource is exists .. "+damResource);

       

        // further convert the resource into Dam asset

        Asset damAsset =  damResource.adaptTo(Asset.class);

        if(damAsset !=null)

        {

        logger.info("dam asset exists .. "+ damAsset);

       

        // create a Rendition Template using Rendition Maker Api and give the width, height, quality, mimietype for your template

        int width = 250;

        int height = 250;

        int quality = 100;

            String mimeType = "image/jpeg";

            String[] mimeTypesToKeep ={ "image/jpeg","image/png"};

            RenditionTemplate renditionTemplate = renditionMaker.createWebRenditionTemplate(damAsset, width, height, quality,

                     mimeType,mimeTypesToKeep);

           

            // Using the rendition template created above , generate the renditions

        List<Rendition> renditionList = renditionMaker.generateRenditions(damAsset, renditionTemplate);

      

        // using Asset Handler Api create thumbnails using the rendition for the asset.

        Collection<ThumbnailConfig> configs = null;

        for(Rendition rendition : renditionList ){

        assetHandler.createThumbnails( damAsset, rendition, configs);

        }

       

        // Just to check if our rendition got added.

        for (Rendition rendition2 : damAsset.getRenditions()) {

        logger.info(rendition2.getName() + " " + rendition2.getPath()+"\n");

      }

       

        resourceResolver.commit();

        }

        }

      }

  catch(LoginException  |IOException e){

  e.printStackTrace();

  }

  }

}

You can create a launcher to make this workflow process to run. Hope this might help.

Thanks and regards

Abinaya

View solution in original post

14 Replies

Avatar

Community Advisor

Why are you not adding these renditions to the workflow launcher ?

Avatar

Level 2

I need to create custom renditions when the DAM update asset workflow is triggered i.e; when the image is uploaded into the DAM (something like custom workflow step in the above workflow process) programmatically..Can you pl help me how to access the above workflow and include the custom step to add custom rendition for the image uploaded in the DAM?

Avatar

Community Advisor

Hi Malar

     I don't know if this is what you are looking for. If the requirement is only to generate a custom rendition when user uploads an image , then you don't need to write any custom code for that as there is already a workflow available which handles the same. 

You can go to workflow manager and DAM Update Asset workflow ; /etc/workflow/models/dam/update_asset.html

1443706_pastedImage_5.png

Double click "Process Thumbnails" step

1443911_pastedImage_6.png

Add your Custom rendition in requested format in the below field .

1443920_pastedImage_8.png

Once set, save your workflow and upload a new image Check the renditions below the uploaded asset and you shud see your new rendition

I have not set it , below image is just for reference to mention that the new rendition will be available in this format.

1443913_pastedImage_7.png

Hope this is exactly what you are looking for . Happy Friday

Avatar

Level 3

Hi Veena,

I have created custom Audio Component in Asset share commons in AEM ,everything is working fine except i am not able to get the thumbnail in audio file(.mp3 and. wav).I have done some R&D then I found that Audio files does not have any rendition except original one.So my requirement is that is there anyway that we can add thumbnail in audio file.

PFB Screenshot of .mp3 node in crx.

kkhan_123_0-1590758576421.png

kkhan_123_1-1590758791133.png

 

see in above picture there is no rendition except original one that's why it is coming as without thumbnail,When I copy any image and paste it here(manually) then thumbnail is appear in audio files.

kkhan_123_2-1590759106356.pngkkhan_123_3-1590759139121.png

but i don't want to do it manually this thing,Can you please suggest some way how can I solve this issue.It will be very helpful for me.

Avatar

Community Advisor
You may have to write some custom workflow for this I believe. Also I am not sure how you can generate the thumbnail for an audio, if you can figure that out may be the best approach is to write custom workflow

Avatar

Community Advisor
If you still have doubt , I would say post this as a question in the forum. We don't get any notification when someone replies to a post.

Avatar

Level 2

Thanks Veena!!

The above solution gave an understanding, but my requirement is to add custom rendition once the image is uploaded in dam the dam update asset workflow should include the custom rendition image. This should be done programmatically in java(to update the workflow process).

Avatar

Level 3

Hi Malar,

Veena's method is  accurate and is the best approach. The rendition is created by java process used by one of the process step. You can replace this java process with your own classes. 

I am bit fuzzy what you exactly need. If you want to duplicate this process from scratch then do the following:

- Disable the existing OOTB create and update via workflow launcher window and then create your own workflow model by copying the following model.

/etc/workflow/models/dam/update_asset.html

- You can add step programatically in your custom workflow model. The step could use your own Java process to create the rendition.

- AEM uses imagemagick API to create renditions. You could use the same API or use your own.

- You can trigger the workflow either by workflow launcher or by adding a listener.

Hope this helps

Avatar

Community Advisor

Malar,

     Same as uagnihotri , I am not sure what your exact requirement is .

  • If you requirement is creating a custom rendition for any asset uploaded . let's say you need all your assets to have 80:100 rendition , then if you add this in above workflow , as soon as an asset is uploaded to DAM , the event listener will listen the upload and trigger the workflow Launcher which will run the DAM Update Asset workflow

  • But if your requirement is something like , each asset should be created using different rendition , which you decide on run-time , then you should follow uagnihotri recommendations and create your own custom process step and attach it to the above workflow (which I won't recommend) or create a new workflow and launcher to listen to your DAM asset .

If you can give me the exact requirement you are trying to achieve may be I can give you a better solution

Thanks

Veena

Avatar

Community Advisor

To answer your question in reply (highlighted) , dam update asset workflow is the one who creates the renditions when you upload any DAM asset. So if you follow the above steps, as soon as you upload the image , it will take care of the rendition creation.

     Why don't you do a POC and confirm if this is not what exactly you are looking for ? Once POC is done you will have an idea on exactly what you need .

Avatar

Correct answer by
Level 2

Hi,

I would suggest create a workflow process and make use of this RenditionMaker api and Assethandler Api to create our own renditions for a particular image asset.

Kindly check out this code. It would be much helpful.

@Properties({

@Property(name = "process.label", value = "Custom Image Rendition Process") })

@Component

@Service

public class CustomImageRendition implements WorkflowProcess {

  private static final Logger logger = Logger.getLogger(CustomImageRendition.class.getName());

  @Reference

  protected ResourceResolverFactory resourceResolverFactory;

  @Reference

  RenditionMaker renditionMaker;

  @Reference

  AssetHandler assetHandler;

  ResourceResolver resourceResolver;

  @Override

  public void execute(WorkItem workItem, WorkflowSession session, MetaDataMap args) throws WorkflowException {

  try

      {

    String assetPath = null;

    // Get the resource resolver using system user and user mapper service

        Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver");

          resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);

          String payloadString = workItem.getWorkflowData().getPayload().toString();

       logger.info("payload path"+payloadString);

    

        // convert the payload path into a Resource

        Resource damResource = resourceResolver.resolve(payloadString);

      

        if (damResource != null){

        logger.info("the damResource is exists .. "+damResource);

       

        // further convert the resource into Dam asset

        Asset damAsset =  damResource.adaptTo(Asset.class);

        if(damAsset !=null)

        {

        logger.info("dam asset exists .. "+ damAsset);

       

        // create a Rendition Template using Rendition Maker Api and give the width, height, quality, mimietype for your template

        int width = 250;

        int height = 250;

        int quality = 100;

            String mimeType = "image/jpeg";

            String[] mimeTypesToKeep ={ "image/jpeg","image/png"};

            RenditionTemplate renditionTemplate = renditionMaker.createWebRenditionTemplate(damAsset, width, height, quality,

                     mimeType,mimeTypesToKeep);

           

            // Using the rendition template created above , generate the renditions

        List<Rendition> renditionList = renditionMaker.generateRenditions(damAsset, renditionTemplate);

      

        // using Asset Handler Api create thumbnails using the rendition for the asset.

        Collection<ThumbnailConfig> configs = null;

        for(Rendition rendition : renditionList ){

        assetHandler.createThumbnails( damAsset, rendition, configs);

        }

       

        // Just to check if our rendition got added.

        for (Rendition rendition2 : damAsset.getRenditions()) {

        logger.info(rendition2.getName() + " " + rendition2.getPath()+"\n");

      }

       

        resourceResolver.commit();

        }

        }

      }

  catch(LoginException  |IOException e){

  e.printStackTrace();

  }

  }

}

You can create a launcher to make this workflow process to run. Hope this might help.

Thanks and regards

Abinaya

Avatar

Level 1

Hi Veena,

     Is there any option to rename the renditions created using OOTB workflow , for Ex: cq5dam.thumbnail.100.150.png to mobile.png ..?

Avatar

Adobe Champion

@VeenaVikraman  if the requirement is to create a rendition, only to a particular sub folder images, and all other default renditions to all images across the dam, do you have any insights on how should we solution it?

 In this case, can we use the same dam update workflow with a new OOB process thumbnail step(but is it possible to add a branching for just a folder here with OOB steps)? Or it should be a custom process step to generate rendition programmatically wherein from the code we can check for folder path as well.