简体   繁体   English

如何在 Magnolia CMS 中获取图像尺寸

[英]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.我有一个绝对定位的图像,我需要在其父容器上设置高度,但现在我在 magnolia API 中找不到允许我获取资产/项目宽度或高度的方法。

Is there a way to get Image Dimensions (height, width) of an Asset/Item in Magnolia CMS using only the native API?有没有办法仅使用本机 API 在 Magnolia CMS 中获取资产/项目的图像尺寸(高度、宽度)?

If not, how would you do it?如果不是,你会怎么做?

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.有关更多信息,请参阅MagnoliaAssetMetadata javadoc 和/或源(和相关类)。

I have done something like this, base on what you have suggested.我已经做了这样的事情,根据你的建议。 I am using damTemplatingFunctions to get the asset using the JCR id.我正在使用damTemplatingFunctions来使用 JCR id 获取资产。

Node node = nodes.nextNode();
String imageJcrId = node.getProperty("image").getValue().getString();
Asset asset = damTemplatingFunctions.getAsset(imageJcrId);
MagnoliaAssetMetadata metadata1 = asset.getMetadata(MagnoliaAssetMetadata.class);
System.out.println(metadata1.getHeight());
System.out.println(metadata1.getWidth());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM