Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

How to get Rendition image height and width programmatically? (Other than OOTB Renditions)

Avatar

Level 4

I have a scenario where I have to read each of the manually added renditions height and width. 
Rendition rendition = resource.adaptTo(Rendition.class);// rendition  obj would only give size of the rendition (rendition.getSize()) not height and width.
For generated renditions, we would at least read it from the node name (String Operations) (cq5dam.thumbnail.140.100.png).

But how to get the height and width of the renditions which are other than ootb renditions and doesnt have height and width values in the filename.

for ex: cq5dam.fpo.jpeg

mrudul_0-1654127390697.png

@DEBAL_DAS 
@P_V_Nair 

13 Replies

Avatar

Community Advisor

Hi @mrudul 

You can try the Asset class to get the details of a particular DAM Asset.

e.g.
Image Width

public int getWidth() { 
Asset asset = resourceResolver.getResource("imagepath").adaptTo(Asset.class);
Rendition rendition = asset.getRendition("cq5dam.thumbnail.48.48.png");
try {
BufferedImage bimg = ImageIO.read(rendition.getStream());
return bimg.getWidth();
} catch (Exception e) {
}
return 0;
}

Image Height

public int getWidth() { 
Asset asset = resourceResolver.getResource("imagepath").adaptTo(Asset.class);
Rendition rendition = asset.getRendition("cq5dam.thumbnail.48.48.png");
try {
BufferedImage bimg = ImageIO.read(rendition.getStream());
return bimg.getHeight();
} catch (Exception e) {
}
return 0;
}

Hope that helps!

Regards,
Santosh

Avatar

Level 4

@SantoshSai : Thanks for your time. 

I tried this but no luck. This is what I added to my class. and getting Exception as below.


mrudul_0-1654132029978.png

 

Just to cross check i am using below imports

javax.imageio.ImageIO
java.awt.image.BufferedImage



BufferedImage bimg;
bimg = ImageIO.read(rendition.getStream());
width = bimg.getWidth();
height = bimg.getHeight();

 

 

From the initial screenshot, u can see the properties of a particular resource I am trying to access.

Avatar

Community Advisor

@mrudul 

Imports looks correct, can you check in parameter(image rendition) passing 

Rendition rendition = asset.getRendition("cq5dam.thumbnail.48.48.png");

 is exists in repo?

Avatar

Community Advisor

@mrudul 

import javax.imageio.ImageIO;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.Rendition;
import java.awt.image.BufferedImage;
    public int getWidth() {
        Asset asset = resourceResolver.getResource("/content/dam/we-retail/en/features/cart.png").adaptTo(Asset.class);
        Rendition rendition = asset.getRendition("cq5dam.thumbnail.48.48.png");
        try {
            BufferedImage bimg = ImageIO.read(rendition.getStream());
            return bimg.getHeight();
        } catch (Exception e) {
        }
        return 0;
    }  

 Screen Shot 2022-06-02 at 12.03.44 AM.pngScreen Shot 2022-06-02 at 12.07.23 AM.png

It returns the width as expected.


Avatar

Community Advisor

HI @mrudul 

 

The rendition is also an asset and you can get the length and width using the metadata.

Int length = resourceResolver.getResource("<<path to resource>>").adapto(Asset.class).getMetadataValue(tiff:ImageLength)

 

Kiran_Vedantam_0-1654129796197.png

Hope this helps!

 

Thanks,
Kiran Vedantam.

Avatar

Community Advisor

Hi @Kiran_Vedantam 

metadata will only be generated for the original uploaded asset, there will be no metadata for generated rendition images

Screen Shot 2022-06-01 at 8.45.00 PM.png

If we wish to get height and width for generated rendition image we can try the above solution which I posted.

Regards,
Santosh

Avatar

Community Advisor

Hi @SantoshSai 

 

I request you to please have a look at the rendition's metadata under the jcr:content

Kiran_Vedantam_0-1654131845867.png

 

Thanks,

Kiran Vedantam.

Avatar

Level 4

Hi @Kiran_Vedantam 
But unfortunately, I don't have a metadata node for the renditions. the only original image has. 

Avatar

Community Advisor

Even I don't have such metadata- Verified existing uploaded image and uploaded new image though

Screen Shot 2022-06-01 at 9.15.10 PM.png

Avatar

Community Advisor

The metadata folder for rendition you will see if the renditions are not excluded from DAM Asset workflow which triggered with create and update.

 

metadata for renditions should be be created, since all metadata info already stored in the original.



Arun Patidar

Avatar

Community Advisor

Hi @mrudul , Can't you get is just from rendition file name. These renditions are created by width and height only. If you check documentation of any thumbnail creating workflow step, These are created by defining width and height i.e. file name itself contains width and height(other then original or if something customized)


you can original's width and height can be as mention by others. 
Try this if works.