Responsive Image WCMS Component

This document describes the responsive image WCMS component that is used in the responsive storefronts of SAP Commerce Cloud Accelerator.

Overview

Commerce Cloud Accelerator has a WCMS ImageComponent that is used for containing media, and can be configured for different image components corresponding to different UI experience levels. However, unlike the desktop or mobile UI experience, the image solution in a responsive UI experience requires multiple images to be uploaded for the same image component, where the best image is then selected based on the screen width. In this case, the component is mapped to a media container that holds different images, as well as to a JavaScript library that can detect the browser width and pixel density to select the appropriate image.

Abstract Responsive Banner Component

Accelerator provides the AbstractResponsiveBannerComponent, which is mapped to a media container that holds multiple images. The SimpleResponsiveBannerComponent is also provided by default as a concrete implementation of this abstract class.

The Imager.min.js JavaScript library handles the images and their respective sizes as key-value pairs. Media files are therefore mapped to the following media formats, which are specific to a responsive UI experience: widescreen, desktop, tablet, and mobile. It is important to note that there is not necessarily a one-to-one mapping between the formats and the breakpoints. If the Imager library determines that an image with a mobile format is more suitable to a tablet element, then this will be selected.

Responsive Image Populators

Accelerator provides two populators to handle responsive images: the ResponsiveMediaContainerPopulator and the ResponsiveImagePopulator. These populators extract a key-value pair, where the key is the image width, and the value is the image URL that is provided to the JavaScript implementation. The ResponsiveMediaContainerPopulator is responsible for sending the appropriate information to the ResponsiveImagePopulator, as well as for sorting the media widths after the images have been populated. This is done to conform with the Imager implementation, which is explained in greater detail in the Imager Implemenation section below.

Default Filename Parsing and Convention

Each media that is uploaded as part of the responsive image component is required to have a certain naming convention to identify the image size. The default implementation looks for -XXXx or [_][0-9]+[xX][0-9]+[_], where XXX is the width of the image. If the image name is sample_landing_home_page_150x70.png,the regex filters the filename as -150x70, and from this, 150 is selected as the width. Since each responsive image component is mapped to a media container, the ResponsiveMediaContainerPopulator and the ResponsiveImagePopulator are used to fetch the required information for the image width and the URL. The ResponsiveImagePopulator contains the parsing logic to fetch the width information from the media. It uses a regex pattern that can be customized to a particular implementation.

Fallback Parsing

If the default parsing logic fails, or you opt to use a fallback strategy in your customization, then the mapping in the example below will be used. Since each media has mediaFormat qualifier information, we map these media qualifiers to a predefined width in the spring.xml file. For images that occupy 100% or 50% of the browser width, the inflection point of the current browser width is used. This mapping is configured in the spring.xml file of the acceleratorfacades extension. This can be changed to breakpoint value, or screen width. Since the Imager JS looks for image width, we have used the breakpoint width.

<alias alias="responsiveImageFormats" name="responsiveImageFormats" />
    <util:map id="responsiveImageFormats" map-class="java.util.LinkedHashMap" value-type="java.lang.Integer">
        <entry key="widescreen" value="1200"/>
        <entry key="desktop" value="992"/>
        <entry key="tablet" value="768"/>
        <entry key="mobile" value="480"/>
    </util:map>

Sample Impex

Once all the configurations are completed, you must provide the appropriate resources to define a SimpleResponsiveBannerComponent. These resources include media files, media formats, media containers, as well as a concrete SimpleResponsiveBannerComponent. The following example show a sample impex to load sample responsive images:

INSERT_UPDATE Media;mediaFormat(qualifier);code[unique=true];@media[translator=de.hybris.platform.impex.jalo.media.MediaDataTranslator][forceWrite=true];realfilename;altText;mime[default='image/jpeg'];$contentCV[unique=true];folder(qualifier)[default=images];
;mobile;Elec_480x320_HomeSpeed_EN_01_480W.jpg;$siteResource/images/banners/homepage/responsive/Elec_480x320_HomeSpeed_EN_01_480W.jpg;Elec_480x320_HomeSpeed_EN_01_480W.jpg;"Fast Precise";
;tablet;Elec_770x350_HomeSpeed_EN_01_770W.jpg;$siteResource/images/banners/homepage/responsive/Elec_770x350_HomeSpeed_EN_01_770W.jpg;Elec_770x350_HomeSpeed_EN_01_770W.jpg;"Fast Precise";
;desktop;Elec_960x330_HomeSpeed_EN_01_960W.jpg;$siteResource/images/banners/homepage/responsive/Elec_960x330_HomeSpeed_EN_01_960W.jpg;Elec_960x330_HomeSpeed_EN_01_960W.jpg;"Fast Precise";
;widescreen;Elec_1400x440_HomeSpeed_EN_01_1400W.jpg;$siteResource/images/banners/homepage/responsive/Elec_1400x440_HomeSpeed_EN_01_1400W.jpg;Elec_1400x440_HomeSpeed_EN_01_1400W.jpg;"Fast Precise";

INSERT_UPDATE MediaContainer;qualifier[unique=true];$medias;$contentCV[unique=true]
;electronics-homepage-spalsh-en;Elec_480x320_HomeSpeed_EN_01_480W.jpg,Elec_770x350_HomeSpeed_EN_01_770W.jpg,Elec_960x330_HomeSpeed_EN_01_960W.jpg,Elec_1400x440_HomeSpeed_EN_01_1400W.jpg

INSERT_UPDATE SimpleResponsiveBannerComponent;$contentCV[unique=true];uid[unique=true];$mediaContainer
;;ElectronicsHompageSplashBannerComponent;electronics-homepage-spalsh-en

Markup and Lazy Loading

In general, images are rendered by <img> tags in HTML. This works when there is only one URL for the image. But in a responsive design, multiple images are required, and as a result, different URLs for the <img> tag's src attribute are also required.

Imager Implementation

Accelerator provides an Imager.min.js that is based on the BBC Imager, which selects the image based on the screen width and the parent element size. The Imager javascript looks for an <img> element with the class attribute js-responsive-image in the browser's DOM, and if it finds one, it gets the JSON data from the data-media attribute. The following is an example of a markup for the Imager JavaScript to render images:

<img class="js-responsive-image" data-media=
'{"100":"Image of width 100x50",
"200":"Image of width 200x50",
"400":"Image of width 400x50",
"600":"Image of width 600x50",
"800":"Image of width 800x50",
"1000":"Image of width 1000x50"}'
alt="">

The Imager.min.js processes the JSON and loads the key value to available widths. It then determines the size of the parent element and creates the src attribute for the image tag based on the selected key.

For more information on the BBC Imager, see https://github.com/BBC-News/Imager.js/Information published on non-SAP site.