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

Avatar

Avatar

Malarvizhi_Sara

Avatar

Malarvizhi_Sara

Malarvizhi_Sara

16-03-2018

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?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

abinayaSirius

Avatar

abinayaSirius

abinayaSirius

30-03-2018

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

Answers (9)

Answers (9)

Avatar

Avatar

uagnihotri

Avatar

uagnihotri

uagnihotri

19-03-2018

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

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

16-03-2018

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

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

21-03-2018

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

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

21-03-2018

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

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

16-03-2018

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

Avatar

Avatar

Rima_Mittal

Avatar

Rima_Mittal

Rima_Mittal

16-03-2018

Avatar

Avatar

venkateshvemula

Avatar

venkateshvemula

venkateshvemula

13-08-2019

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

Avatar

Malarvizhi_Sara

Avatar

Malarvizhi_Sara

Malarvizhi_Sara

19-03-2018

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

Avatar

Malarvizhi_Sara

Avatar

Malarvizhi_Sara

Malarvizhi_Sara

16-03-2018

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?