Adobe CQ5.5-How to display Page Thumbnail using API

為{幸葍}努か 提交于 2019-12-10 11:37:59

问题


I have configured image for my page using sidekick >Page properties>Images tab. Now I want to fetch my this page image(thumbnail) in one of my jsp. Can someone give me pointers or code snippet for api class and method that I can use to achieve this.

Thanks, Rajeev


回答1:


I would suggest using the default image component as an example - /libs/foundation/components/image.

If you're putting your code into a component for your specific page type though, your code should be something like this:

if (currentNode.hasNode("image")) {
    String imagePath = currentNode.getNode("image").getPath();
    Resource imageRes = resourceResolver.getResource(imagePath);
    image = new Image(imageRes);

    image.loadStyleData(currentStyle);
    image.setSelector(".img");

    if (!currentDesign.equals(resourceDesign)) {
        image.setSuffix(currentDesign.getId());
    }
    image.draw(out);
}

Keep in mind though, even though you set an image, it does NOT mean it will show up - if you're using the default page dialog for page properties, it will only show a broken image. That's because there is a bug in CQ where the sling:resourceType property of the image doesn't get set, and thus it won't show up. This is because the .img selector that gets put on the image doesn't know what to do, unless it get's pointed to a resource type with a definition for the .img selector, so it can properly render the image.

I've uploaded a package that you can use as a hotfix for the issue with the default /libs/foundation/components/page component dialog, so that it will actually set the resource type when you upload an image. You can find/download the package from my Google Drive

Hopefully that helps. Let me know if you need more help.

EDIT

If you're trying to get the page properties image from one page on another page, you just need to use a resource resolver. You should have one available to you in CQ, so this would essentially be the code:

Resource imageRes = resourceResolver.getResource(pathFromYourDialog);
Image image = new Image(imageRes);

The rest would be the same - you're just giving it a different path to start from.




回答2:


I think Nicholaus was more on point with his EDIT answer to your immediate need. If the user is providing you a path to the thumbnail via the dialog (i.e. a DAM image).

You can simply create the image, or if it has DAM information you can load it as a DAM Asset and pull the necessary information.

Image image = new Image();
Resource imageResource = resourceResolver.getResource(imageUrl);
Asset imageAsset = imageResource.adaptTo(Asset.class);

Map<String, Object> valueMap = imageAsset.getMetadata();

long width = Long.parseLong(valueMap.get("tiff:ImageWidth").toString());
long height = Long.parseLong(valueMap.get("tiff:ImageLength").toString());
Object titleObject = valueMap.get("tiff:ImageTitle");
String title = (titleObject == null) ? null : titleObject.toString();

if (title != null)
{
  image.setTitle(title);
}

image.setWidth(width);
image.setHeight(height);
image.setUrl(imageUrl);

This is a little long hand for what Nicholaus had suggested, the Image class will create itself based off the Resource you pass it. (actually upvoted Nicholaus for that, have some optimizations we can make).

Another, simpler option would be to just use the src that the user passes through, in the event that all you're doing is setting a thumbnail. I'm guessing you could be safe in doing something like:

in java:

String thumbSrc = properties.get("thumbSrc", "defaultThumbnail.path");
if (!thumbSrc.isEmpty())
{
  pageContext("thumbSrc", thumbSrc);
}

in jsp:

<img alt="thumbnail" src="${thumbSrc}"/>

or if you don't want to do anything in the java you could just do something like

<c:if test="${not empty properties.thumbSrc}">
  <img alt="thumbnail" src="${properties.thumbSrc}"/>
</c:if>

In order to get the same result as the first part in just jsp, you'd need to wrap it in a choose, because passing it through some processing before sending to view makes it easier to set default values.



来源:https://stackoverflow.com/questions/16074449/adobe-cq5-5-how-to-display-page-thumbnail-using-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!