A convenience Symfony bundle tailored for current responsive images needs of the webfactory GmbH, without any claims on reusability outside of this scope. Ye be warned.
composer req webfactory/responsive-image-bundle
Activate the bundle, e.g. in src/bundles.php
:
<?php
return [
// ...
Webfactory\ResponsiveImageBundle\WebfactoryResponsiveImageBundle::class => ['all' => true],
];
If you intend to use lazyloading for images (which is the default), require "lazysizes": "^5.3"
in your /package.json
.
responsiveImg(image, options)
with:
image
: an array with the following keys:url
(mandatory): the URL of the imagealt
(optional): the alternative text for the image. Defaults to empty string.title
(optional): the title text for the image. Defaults to empty string.
options
: an array with the following keys:sizes
(optional): a string that sets thesizes
-Attribut. Defaults toauto
, or to100%
if lazyloading is disabled and sizes is empty.transformations_to_widths
(optional): an object with thumbor transformation names as keys and image widths as values. Sets thesrcset
-Attribute, with the thumbor image URLs based on the given transformations as the source. Defaults to asrcset
with keys fromimagex_xxxs
toimage_xl
respectively to values from160w
to1600w
.class
(optional): CSS classes to add to the image. Defaults to empty string.data_attributes
(optional): an iterable object with data-attribute names (= the part afterdata-
) and values as key/value pairs. Defaults to empty string.placeholder_filter
: a string with the name of the thumbor transformation (i.e.image_lqip
) which is added to the placeholder image when lazyloading the image. Should match the target dimensions of the image. A list of default transformations is provided with this bundle (see below) and can be overwritten / extended in the application configuration, e.g. the config.yml.lazyload
(optional): Use lazyloading. Defaults totrue
.lqip
(optional): Use the LQIP-method (original concept (2013), current popular LQIP options) of always loading a low-quality placeholder image. Defaults totrue
.
Example:
{% import '@WebfactoryResponsiveImage/Macros/responsiveImg.html.twig' as rImg %}
{{ rImg.responsiveImg(
{
'url': s3_cachable_url(artist, 'photo'),
'alt': 'Portrait of ' ~ artistEntity.name
},
{
'sizes': '100vw',
'transformations_to_widths': {
'image_xxs': '320w',
'image_md': '992w'
},
'data_attributes': {'credits': '© P. H. O'Tographer'},
}
) }}
macro responsivePicture(formats, options)
with:
formats
: Array of objects, with each object describing a different format variant of the motive/picture by the following keys:image_url
(mandatory): the URL of the image in this formatoptions
(optional): an array with the following keys that parametrize this format variant:sizes
(optional): a string that sets thesizes
-Attribut. Defaults toauto
, or to100%
if lazyloading is disabled and sizes is empty.transformations_to_widths
(optional): an object with thumbor transformation names as keys and image widths as values. Sets thesrcset
-Attribute, with the thumbor image URLs based on the given transformations as the source. Defaults to asrcset
with keys fromimagex_xxxs
toimage_xl
respectively to values from160w
to1600w
.media_query
(optional): a string that sets the media query for the source element
options
: an array with the following keys:class
: Optional class to add to the img-tag. Defaults to empty class attribute is no value is given to keep the code readable.alt
(optional): the alternative text for the image. Defaults to empty string.title
(optional): the title text for the image. Defaults to empty string.data_attributes
(optional): an iterable object with data-attribute names (= the part afterdata-
) and values as key/value pairs.placeholder_filter
: a string with the name of the thumbor transformation (i.e.image_lqip
) which is added to the placeholder image when lazyloading the image. Should match the target dimensions of the image. A list of default transformations is provided with this bundle (see below) and can be overwritten / extended in the application configuration, e.g. the config.yml.lazyload
(optional): Use lazyloading. Defaults totrue
.lqip
(optional): Use the LQIP-method (original concept (2013), current popular LQIP options) of always loading a low-quality placeholder image. Defaults totrue
.
Example:
{% import '@WebfactoryResponsiveImage/Macros/responsiveImg.html.twig' as rImg %}
{{ rImg.responsivePicture(
[
{
'image_url': cachable_s3_url(artistEntity, 'photoInPortraitFormat'),
'options': {
'sizes': '100vw',
'transformations_to_widths': {
'image_xxs' : '320w',
'image_md' : '992w'
},
'media_query': '(min-width: 768px)'
}
},
{
'image_url': cachable_s3_url(artistEntity, 'photoInSquareFormat'),
'options': {
'sizes': '100vw',
'transformations_to_widths': {
'image_xxs' : '320w',
'image_md' : '992w'
},
'media_query': '(min-width: 640px)'
}
}
],
{
'lazyload': true,
'class': 'js-object-fit',
'alt': 'Portrait of ' ~ artistEntity.name,
'data_attributes': {'credits': '© P. H. O\'Tographer'},
}
) }}
responsiveBackgroundVideo(video, options)
with:
video
: an array with the following keys:url
(mandatory): the URL of the image
options
: an array with the following keys:transformations
(optional): an object with thumbor transformation names as keys and MIME-Types as values. Defaults to{ 'video_hevc_720p' : 'video/mp4; codecs=hevc', 'video_webm_720p' : 'video/webm; codecs=vp9', 'video_mp4_720p': 'video/mp4' }
class
(optional): CSS classes to add to the image. Defaults to empty string.data_attributes
(optional): an iterable object with data-attribute names (= the part afterdata-
) and values as key/value pairs. Defaults to empty string.
Example:
{% import '@WebfactoryResponsiveImage/Macros/responsiveVideo.html.twig' as rVid %}
{{ rVid.responsiveBackgroundVideo(
{
'url': s3_cachable_url(entity, 'video')
},
{
'class': 'background-video',
'data_attributes': {'credits': '© P. H. O\'Tographer'},
}
) }}
This bundle configures the JbPhumborBundle via the Resources/config/jb_phumbor-default-config*.yaml
files, setting
some default transformations and the Thumbor server settings. You can overwrite any jb_phumbor
-setting in your
application config.