You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

A site definition allows you to add features such as image variations to your site. This page explains how to configure image variations in a theme, part of the site definition.

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.

Best practice

If you need different sizes of the same image, do NOT upload multiple copies (variations) of the same image to your DAM repository. Instead, upload the image only once and define image size variations in your theme.

Defining image variations in a theme

Open the Site app. It may already contain a site configuration, for instance for the travel demo. (Keep in mind that you can have only one site with Magnolia Community Edition.) In this tutorial we add a new theme to an existing site definition.

Go to the Themes tab and build the following structure:

Node name

 
my-first-website

 
imaging

 
variations

Add three image variations : largelarge-square and xxlarge.

Node nameValue

 
variations

 

 
large

 

 
class

info.magnolia.templating.imaging.variation.SimpleResizeVariation

 
width

730

 
large-square

 

 
class

info.magnolia.templating.imaging.variation.SimpleResizeVariation

 
width

730

 
height

730

 
xxlarge

 

 
class

info.magnolia.templating.imaging.variation.SimpleResizeVariation

 
width

 1140

In the Site Definitions tab configure the site definition to use the new theme:

Node nameValue

 
theme

 

 
name

my-first-website

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 AssetRendition for the itemKey and renditionName (variation name).
  • damfn.getAssetLink(itemKey, renditionName) returns a link for the same.

Background image

To render the background image we pass the variation name xxlarge:

templates/pages/main.ftl
    <style>
        [#if content.introBgImage?has_content]
            [#assign assetRendition = damfn.getRendition(content.introBgImage, "xxlarge")! /]
            [#if assetRendition?has_content]
            .intro-section {
                background: url(${assetRendition.getLink()}) no-repeat center center;
                background-size: cover;
            }
            [/#if]
        [/#if][#-- eof: introBgImage --]
    </style>

Product image

To render a car image we use the large variation:

templates/components/content-items-list.ftl (fragment)
                                        <div class="big-box">
                                            [#if product.image?has_content]
                                                [#assign imgRef = damfn.getAssetLink(product.image, "large")!]
                                                [#if imgRef?has_content]
                                                    <img class="img-responsive img-rounded" src="${imgRef}" alt="${productTitle!}">
                                                [/#if]
                                            [/#if]
                                        </div>

Image in the textImage component

The macro that renders Eric's profile image uses the large and large-square variations depending on what the user has selected in the dialog:

templates/components/textImage.ftl (fragment)
            [#if "roundedEdges"==content.imageStyle!]
              <img class="img-responsive img-rounded" src="${damfn.getRendition(asset, "large").getLink()!}"/>
            [#elseif "circle"==content.imageStyle!]
              <img class="img-responsive img-circle" src="${damfn.getRendition(asset, "large-square").getLink()!}"/>
            [#else]
              <img class="img-responsive" src="${damfn.getRendition(asset, "large").getLink()!}"/>
            [/#if]

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:

WARN  ia.templating.imaging.VariationAwareImagingSupport: 
Supplied variation [xxlarge] could not be found. Please update your configuration.

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.

 

Congratulations! (big grin)

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. 

 

#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels