javacontent-management-systemmagnoliadam

How to get image dimensions in Magnolia CMS


I have an absolute positioned image whose parent container I need to set a height on, but now I can't find a method in magnolia API that allows me to get the width or height of an asset/item.

Is there a way to get Image Dimensions (height, width) of an Asset/Item in Magnolia CMS using only the native API?

If not, how would you do it?


Solution

  • I would write it somehow like

    import info.magnolia.dam.api.metadata.MagnoliaAssetMetadata;
    // ...
    if (asset.supports(MagnoliaAssetMetadata.class)) {
      MagnoliaAssetMetadata metadata = asset.getMetadata(MagnoliaAssetMetadata.class);
      long width = metadata.getWidth();
      long height = metadata.getHeight();
      // do whatever you need with the image dimensions
    } else {
      // handle non-image asset
    }
    

    See MagnoliaAssetMetadata javadoc and/or sources (and related classes) for more info.