Before you proceed read How to work with images using damfn.
Why use image variations?
The image assets for Eric's cars are rather large. The components on the page require smaller images. Even if Bootstrap CSS resizes the images to fit the layout, the browser still loads the big images. We want to render small images on the page but we want to keep the big original images in the Assets app in case we use them elsewhere.
Configuring a theme
Keep in mind that you can only have one site with Magnolia Community Edition. In this tutorial we add a new theme and reference it in the existing
travel site definition.
A theme can be configured in YAML in any module and must reside in the
themes folder of the module.
themes folder in
Defining image variations in a theme
my-first-website theme definition with three image variations :
Referencing a theme
Open the Site app and change the value of the
/theme/name property to
Using image variations
Although we did not mention it, the code in the previous pages already uses imaging variations. The damfn templating functions return resized images when you pass the desired variation name as an argument:
damfn.getRendition(itemKey, renditionName)returns an
damfn.getAssetLink(itemKey, renditionName)returns a link for the same.
To render the background image we pass the variation name
To render a car image we use the
Image in the textImage component
The macro that renders Eric's profile image uses the
large-square variations depending on what the user has selected in the dialog:
What happens if no variation is defined?
But what happens if the image variation is not defined? Will Magnolia throw an error? No, it just logs a warning and returns the original asset:
If a variation cannot be found but the original asset exists, the
damfn templating function returns the original asset. So the website looks OK but serves large original images, which is slower and wastes bandwidth.
Testing image variations
How can you test that the variations are working? Does Magnolia now resize the images?
Reload the page and inspect the image in your browser.
Example: The original Fiat Cinquecento image is 1024 x 741 pixels. Magnolia resizes it to 730 pixels as defined in the
large variation. Boostrap resizes the image further to fit the parent element in a responsive way.
You created a website with Magnolia. You know how to manage content in an app and how to display it on the site. You created your first light module and learned the basics of working with images.
See Tutorials for your next learning goal.