Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Insert images via user marco

Martin Ziegler June 20, 2022

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 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

Martin Ziegler June 22, 2022

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 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
Martin Ziegler June 23, 2022

@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
AUG Leaders

Atlassian Community Events