Magnolia 6.1 reached end of life on March 31, 2021. This branch is no longer supported, see End-of-life policy.
This page explains how to work with images using
imgfn
templating functions in template scripts. With imgfn
you can get an image from any JCR workspace.
If you manage your images with the Magnolia Digital Asset Management module, we recommend using damfn
. For more information, see working with images using damfn
.
If you use a Magnolia bundle, the required and optional modules are already included.
If you created a custom webapp, add the following modules in the pom file of the webapp of your bundle. The modules may have dependencies to other modules but these dependencies are handled by Maven dependency management.
Modules required for basic image operations.
artifactId | version |
---|---|
magnolia-module-imaging | 3.5.8 |
magnolia-imaging-support | 3.5.8 |
Modules required when using size variations of an image. Recommended.
artifactId | version |
---|---|
magnolia-site | 1.4.4 |
magnolia-templating-essentials-imaging | 2.0.3 |
With a basic upload field you can upload an image directly to the node of your context. When saving the data of the dialog, Magnolia creates a subnode of the node of your context and stores the binary data of the image as property of the subnode.
Example - code fragment from a dialog definition
Form: tabs: - name: tabImage fields: - name: image class: info.magnolia.ui.form.field.definition.BasicUploadFieldDefinition binaryNodeName: myNode
In the dialog definition, you can define the value of the binaryNodeName
property (in the example the value is myNode
). Keep in mind that you must know the name of this subnode to render the image later on.
You can check the JCR node structure in in the JCR Browser app.
Using imgfn
, you can create a link to an image stored in any workspace as long as you know the path to the node which stores the binary data of the image.
Look at the screenshot above showing the JCR browser. It highlights the subnode of a component node. Within the rendering context of a component template script you have the content
object which is the component node. In the screenshot above, the node with the name 0
is the content object.
To display an image, you must get the subnode of content
and pass it as argument to the imgfn
templating function. You can use the same method to get the original version of the image and to get a variation.
[#assign binaryNode = cmsfn.asJCRNode(content).getNode('myNode') /] [#assign imgLink = imgfn.getImageVariationLinkFromBinary(binaryNode,'original') /] [#assign imgLink240 = imgfn.getImageVariationLinkFromBinary(binaryNode,'240') /] <div> <span>The default image</span><br/> <img src="${imgLink}"><br/> <span>Image variation 240</span><br/> <img src="${imgLink240}"> </div>
myNode
in the example.original
to get the original size of the uploaded image.240
to get a link to an image variation named 240.<my-theme>/imaging/variations
. Using imgfn.getImageVariationLinkFromBinary(binaryContent, variation)
is not compatible with SVG images. As an alternative, you can save the image in the Resources app and refer to it through CSS.
Here is the rendered result of the template script snippet above: