Get image size thru thru code

Karl515 27-01-2019


I',m importing an image to my custom component via "file", can I get it's resolution?

for example:


something like that?

htl way would be good.

Image is referenced at DAM

Answers (6)

Answers (6)

Karl515 29-01-2019

Created my java file, complete but apparently, I can't populate the "Image(resource)" with "fileReference" from cq:file as to I'm getting an error. a work around?


Karl515 28-01-2019

Thanks for the answers! But I think my question is not quiet direct.

I need to extract the img meta data (specifically height:width) to my ".html" file so that I can reference it.

if I must use java to do this, where will I write my java code?

that's where I want to extract asset size.


Ravi_Pampana 27-01-2019

Using below code, we can retrieve the image width and height in Java and use that in HTL


Image image = new Image(resource);

int imgWidth = 990; // Some default width

int imgHeight = 520;// Some default height

  try {

  if (image.getLayer(false, false, false) != null) {

  imgWidth = image.getLayer(false, false, false).getWidth();

  imgHeight = image.getLayer(false, false, false).getHeight();