Get image size thru thru code | Community
Skip to main content
Karl515
Level 5
Jan 27, 2019 1:41 PM UTC
Solved

Get image size thru thru code

Hi!

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

for example:

${properties.image.getWidth()}

something like that?

htl way would be good.

Image is referenced at DAM

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Karl515
Karl515Accepted solution
Level 5
Feb 5, 2019 3:13 AM UTC

For anyone looking. here's the answer.

Get Asset metadata (height and width)

7 replies

Karl515
Karl515AuthorAccepted solution
Level 5
Feb 5, 2019 3:13 AM UTC

For anyone looking. here's the answer.

Get Asset metadata (height and width)

Translate
Karl515
Karl515Author
Level 5
Jan 29, 2019 8:52 AM UTC

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?

Translate
Karl515
Karl515Author
Level 5
Jan 29, 2019 4:41 AM UTC

Still haven't figure out above problem.
here is a someple code of what I'm trying to achieve

ex.

Translate
Karl515
Karl515Author
Level 5
Jan 29, 2019 2:42 AM UTC

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.

Thanks!

Translate
smacdonald2008
Level 10
Jan 28, 2019 3:04 PM UTC

As Ravi correctly points out - you can use Java to get the information that you require.

Translate
Ravi_Pampana
New Participant
Jan 27, 2019 5:54 PM UTC

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

import com.day.cq.wcm.foundation.Image;

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();

}

Translate