Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,730
Community Members
 
Community Events
165
Community Groups

Insert images via user marco

Hi, we're documenting various physical parts in Confluence. Form our CAD system we're able to export images, which are added to one Confluence page as attachments. Each images is named according the page title.

For each part we have one Confluence page. 

I wrote a user macro to show the matching image on each page.

## @noparams
## Short description
## Show most recent part image based on part number and name
##

## @noparams
#set ($title = $content.title)
#set ($title = $title.replace(" ","_"))
#set ($title = $title.toLowerCase())
#set ($title = "https://confluence.lan/download/attachments/104431729/"+$title + ".jpg?api=v2")


<img width=200px" src=$title></img>
##$title

So far so good.

However it would be nice to mimic the image macro, so that one can access the image in a larger resolution.

Any hints how to achieve this?

 

1 answer

0 votes
Dominic Lagger Community Leader Jun 21, 2022

Hi @Martin Ziegler 

I'm not sure if I understand you correct, but you just want to open the preview when you click on the image?

Look at the HTML of other images:

<img class="confluence-embedded-image image-center" 
height="220" width="220"
src="/download/attachments/2687044/CKB.png?version=2&amp;modificationDate=1588014783000&amp;api=v2"
data-image-src="/download/attachments/2687044/CKB.png?version=2&amp;modificationDate=1588014783000&amp;api=v2"
[...]
>

The class "confluence-embedded-image" will do this. Try to add this.

Regards, Dominic

Hi @Dominic Lagger 

You understood the question correctly.

 

The code does not seem to work without the version and modificationDate in the URL and the data-linked-resource-id and data-linked-resource-container-id as attributes.
I have no clue how I could get these values.

 

This is the HTML code generated by my current macro (not working)

<span class="confluence-embedded-file-wrapper confluence-embedded-manual-size conf-macro output-inline" data-hasbody="true" data-macro-name="partimage">
<img class="confluence-embedded-image confluence-thumbnail" height="200" src="/download/attachments/104431729/103200_bu_housing.jpg?api=v2" data-image-src="/download/attachments/104431729/103200_bu_housing.jpg?api=v2" data-linked-resource-type="attachment" data-linked-resource-default-alias="/download/attachments/104431729/103200_bu_housing.jpg" data-base-url="https://confluence.htkb.helbling.ch" data-linked-resource-content-type="image/jpg>
</span>
"></span>

 

This is the HTML code if the image macro is used:

<span class="confluence-embedded-file-wrapper confluence-embedded-manual-size">
<img class="confluence-embedded-image confluence-thumbnail" width="200" src="/download/thumbnails/104431729/103200_bu_housing.jpg?version=3&amp;modificationDate=1655732648000&amp;api=v2" data-image-src="/download/attachments/104431729/103200_bu_housing.jpg?version=3&amp;modificationDate=1655732648000&amp;api=v2" data-unresolved-comment-count="0" data-linked-resource-id="106038747" data-linked-resource-version="3" data-linked-resource-type="attachment" data-linked-resource-default-alias="103200_bu_housing.jpg" data-base-url="https://confluence.htkb.helbling.ch" data-linked-resource-content-type="image/jpeg" data-linked-resource-container-id="104431729" data-linked-resource-container-version="64"></span>
Dominic Lagger Community Leader Jun 23, 2022

I just made some tests and here is my code.

You can try with this code. Let me know if this helped:

#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $page = $pageManager.getPage($content.getId()) )

#set( $attachment = $page.getAttachmentNamed("Naehkaestchen.jpg"))
$attachment.getDownloadPath()
<hr>
$attachment.getUrlPath()

Here you can find all possible methods of the attachment

Regards, Dominic

Like Martin Ziegler likes this

@Dominic Lagger Thanks so much for your hints!

 

Here is my working code

## @noparams
## Confluence version: Server 7.4.7
## Short description
## Show most recent part image based on part number and name
##

## @noparams
#set( $title = $content.title )
#set( $title = $title.replace(" ","_") )
#set( $title = $title.toLowerCase() )
#set( $title = $title + ".jpg" )
#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $pageParent = $pageManager.getPage($content.getParent().getId()) )
#set( $page = $pageManager.getPage($content.getId()) )
#set( $attachment = $pageParent.getAttachmentNamed($title))
#set( $parentID = $content.getParent().getIdAsString() )
#set( $ImageID = $attachment.getIdAsString() )

<span class="confluence-embedded-file-wrapper confluence-embedded-manual-size">
<img class="confluence-embedded-image confluence-thumbnail" width="200" src="$attachment.getDownloadPath()" data-image-src="$attachment.getDownloadPath()" data-unresolved-comment-count="0" data-linked-resource-id="$ImageID" data-linked-resource-version="1" data-linked-resource-type="attachment" data-linked-resource-default-alias="$title" data-base-url="https://wiki.local" data-linked-resource-content-type="image/jpeg" data-linked-resource-container-id="$parentID" data-linked-resource-container-version="64">
</span>

 

All images are attached to the parent page. Direct child page refer to one image of parent page based on title of child page.

Like Dominic Lagger likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Confluence: Where work and wellness meet

Feeling overwhelmed by the demands of work and life? With a 25% increase in the prevalence of anxiety and depression worldwide during the pandemic, for most of us, it’s a resounding yes . 🙋‍♀️ ...

762 views 5 21
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you