Insert images via user marco
Asked by Martin Ziegler:

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?


Answers:
author: Dominic Lagger
body: Hi [@Martin Ziegler](/t5/user/viewprofilepage/user-id/1400321)

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

comments: author: Martin Ziegler
body: Hi [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600)

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>
```


author: Dominic Lagger
body: 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](https://docs.atlassian.com/ConfluenceServer/javadoc/7.2.0/com/atlassian/confluence/pages/Attachment.html)

Regards, Dominic


author: Martin Ziegler
body: [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) 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.  


Tags: confluence-server

image

server

user-macro