Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Get Asset metadata (height and width)

Avatar

Level 5

Hello, I've read lots but still haven't got a way to get asset metadata specifically height x width.

I get asset data by copy pasting "file" to my cq:dialog.

1683701_pastedImage_2.png

I'm using Java models (as what I have read) to get asset like this.

1683702_pastedImage_3.png

it returns a string to my DAM asset ex.

/content/assets/myfolder/asset.jpg.

how can I get it's metadata?

Previous thread:

Get image size thru thru code

1683703_pastedImage_5.png

This example seems cool but I can't populate the image resource field.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Try this--

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.Self;

import com.day.cq.dam.api.Asset;

@Model(adaptables = SlingHttpServletRequest.class)   // map it to resource as applicable

public class HelloWorldModel {

@Self

protected SlingHttpServletRequest request;

@PostConstruct

protected void init() {

Resource resource = request.getResourceResolver()

.getResource("/content/dam/we-retail/en/activities/biking/cycling_1.jpg");

Asset asset = resource.adaptTo(Asset.class);

String width = asset.getMetadataValue("tiff:ImageWidth");

String height = asset.getMetadataValue("tiff:ImageLength");

// do something

}

}

View solution in original post

4 Replies

Avatar

Level 8

https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/co...

Asset is created by AssetManager and can be retrieved either via AssetManager or by adapting a asset Resource to an Asset.

     eg: // to create an asset AssetManager assetManager = resolver.adaptTo(AssetManager.class); Asset newAsset = assetManager.createAsset("/path/to/asset/document.pdf");  // to get an existing asset Asset asset = assetManager.getAsset("/path/to/asset/document.pdf");  // to get asset by adapting Asset asset = resource.adaptTo(Asset.class); 

Asset properties can be retrieved or set by adapting it to a ValueMap.

     eg: // to get Asset properties value map ValueMap map = asset.adaptTo(ValueMap.class);

You can also use DAMUtil to get asset properties

String assetPath = DamUtil.getAssetFromMetaRes(resource.getPath());

Avatar

Level 5

Here's my updated code

@Model(adaptables = SlingHttpServletRequest.class)

public class SlingModel {

    @ValueMapValue

    public String fileReference;

     @Self

    protected SlingHttpServletRequest request;

    

    @PostConstruct

    protected void init() {

          final Resource assetResource = request.getResourceResolver().getResource(fileReference);

          Asset asset = assetResource.adaptTo(Asset.class);

          AssetMetadata assetMetadata = asset.getAssetMetadata();

          ValueMap properties = asset.adaptTo(ValueMap.class);

          //output ValueMap.

     }

}

But how can I get the Metadata? this is what I'm currently getting

com.adobe.granite.asset.core.impl.AssetImpl$1@74126052

I want to get width and height. or if possible all JCR metadata. thanks!

Avatar

Correct answer by
Level 10

Try this--

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.Self;

import com.day.cq.dam.api.Asset;

@Model(adaptables = SlingHttpServletRequest.class)   // map it to resource as applicable

public class HelloWorldModel {

@Self

protected SlingHttpServletRequest request;

@PostConstruct

protected void init() {

Resource resource = request.getResourceResolver()

.getResource("/content/dam/we-retail/en/activities/biking/cycling_1.jpg");

Asset asset = resource.adaptTo(Asset.class);

String width = asset.getMetadataValue("tiff:ImageWidth");

String height = asset.getMetadataValue("tiff:ImageLength");

// do something

}

}