Image.srcset ViewHelper <f:image.srcset>
ViewHelper to generate a list of image URLs and their corresponding srcset descriptors to be used in a srcset attribute of an or tag.
Responsive images can either be defined based on absolute widths or relative pixel densities.
Width descriptors use the "w" unit, which refers to the width of the image file (not to be confused with "px", which refers to so-called CSS pixels in the HTML document). The srcset attribute in combination with the sizes attribute provide hints to the browser which one of the provided image files should be used. The browser is free to consider other factors, such as network speed, user preferences, or client-side caching status.
Density descriptors use the "x" unit and target client devices based on the pixel density of their screens. "1x" will be loaded for low-density devices, while "2x", "3x"... target higher densities, also referred to as "High DPI" or "Retina".
According to the HTML standard, "w" and "x" units cannot be mixed. Instead, browsers will usually consider the pixel density, even if "w" units are used. In practice, "x" is only relevant for fixed-width images across device sizes (e. g. an icon that always has the same visual size). In all other cases, "w" should be preferred.
Examples
Width descriptors
<source srcset="{f:image.srcset(image: imageObject, srcset: '1000w, 1200w, 1400w', cropVariant: 'desktop')}" media="(min-width: 1000px)" sizes="100vw" />
Output::
<source srcset="/path/to/csm_myimage_1000.jpg 1000w, /path/to/csm_myimage_1200.jpg 1200w, /path/to/csm_myimage_1400.jpg 1400w" media="(min-width: 1000px)" sizes="100vw" />
Density descriptors
Output::
Go to the source code of this ViewHelper: Image\SrcsetViewHelper.php (GitHub).
Arguments
The following arguments are available for the image.srcset ViewHelper:
absolute
-
- Type
- bool
- Default
- false
Force absolute URL for generated images
crop
-
- Type
- string|bool|array
Overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
cropVariant
-
- Type
- string
- Default
- 'default'
Select a cropping variant, in case multiple croppings have been specified or stored in FileReference
fileExtension
-
- Type
- string
Use the specified target file extension for generated images; files will be converted if necessary
image
-
- Type
- TYPO3\CMS\Core\Resource\FileInterface
A FAL object (\TYPO3\CMS\Core\Resource\File or \TYPO3\CMS\Core\Resource\FileReference); if not specified, ViewHelper children will be used as a fallback.
referenceWidth
-
- Type
- int
Image width that will be used as base (1x) when calculating srcset with pixel density descriptors (e. g. 2x). This is irrelevant for width descriptors.
srcset
-
- Type
- string
- Required
- 1
Comma-separated list of width descriptors (e. g. 200w) or pixel density descriptors (e. g. 2x).