Rashmi Ks
Rashmi Ks

Reputation: 35

DAM Asset Renditions - Getting cq5dam.thumbnail.48.48.png rendition but need the original rendition

String itemPath = "/content/dam/global-abc/mcos/benefra/nl/custom/product-images/25-nl-107855/fact3.jpg";

String thumbnailUrl = itemPath;

if (assetManager != null) {                        
   Asset asset = assetManager.getAsset(itemPath);

   thumbnailUrl = asset.listRenditions().hasNext() ?
                  asset.listRenditions().next().getPath() : itemPath;

}

asset.listRenditions().next().getPath() gives /content/dam/global-abc/mcos/benefra/nl/custom/product-images/25-nl-107855/fact3.jpg/jcr:content/renditions/cq5dam.thumbnail.48.48.png which will reduce the clarity of the image at UI end. Need to increase the resolution or get original image

Upvotes: 0

Views: 1556

Answers (1)

Jens
Jens

Reputation: 21510

Disclaimer:

I am only going to mention the "old" Asset API in com.day.cq.dam.api. There is a newer API in com.adobe.granite.asset.api which is different and lacks some features, which is why I usually prefer to use the "old" API.


There are several ways to get renditions in AEM and all of them come with advantages and disadvantages.

Before I start to explain a few ways to get renditions, there are two rules you should follow to make your live easier:

  1. Never expect thumbnail or web renditions to exist.
  2. Never use the original rendition except for downloads.

You wrote:

Need to increase the resolution or get original image.

If you just want to get the original rendition, you can simply use:

Rendition rendition = asset.getOriginal();

But if you want to get a thumbnail or web rendition you should use one of the RenditionPicker.

To get a thumbnail rendition you can use:

PrefixRenditionPicker picker = new PrefixRenditionPicker(DamConstants.PREFIX_ASSET_THUMBNAIL, true)
Rendition rendition = picker.getRendition(asset);

This will give you the first thumbnail rendition that the picker will find. In theory you could force a certain thumbnail rendition by extending the code like this:

PrefixRenditionPicker picker = new PrefixRenditionPicker(DamConstants.PREFIX_ASSET_THUMBNAIL + ".319", true)
Rendition rendition = picker.getRendition(asset);

This would return the 319px thumbnail rendition with the name cq5dam.thumbnail.319.319.png. But remember, there is no guarantee that this rendition exists. If there is no rendition with this name the RenditionPicker from above will simply return the original rendition (because we used true as second parameter).

The same process can be used to get the web rendition. You can either use the rendition picker from above and just use another constant:

PrefixRenditionPicker picker = new PrefixRenditionPicker(DamConstants.PREFIX_ASSET_WEB, true)
Rendition rendition = picker.getRendition(asset);

Or you use the the dedicated WCMRenditionPicker:

WCMRenditionPicker picker = new WCMRenditionPicker();
Rendition rendition = picker.getRendition(asset);

But as before, this will return the first rendition that is found or null if none is found.

Depending on your use case you might want to create your own RenditionPicker by implementing the following interface:

com.day.cq.dam.api.RenditionPicker

There is also another API I want to mention which is com.day.cq.dam.commons.util.UIHelper. This class contains a few interesting methods like:

getBestfitRendition(Asset asset, int width)

This method would allow you to specify a desired width and it will try to find the nearest best fitting rendition. This can be also quite handy in certain situations.

Links:

  1. Asset Javadoc
  2. Rendition Javadoc
  3. PrefixRenditionPicker Javadoc
  4. WCMRenditionPicker Javadoc
  5. PREFIX_ASSET_THUMBNAIL constant Javadoc
  6. PREFIX_ASSET_WEB constant Javadoc
  7. UIHelper Javadoc

Upvotes: 1

Related Questions