Effortless Responsive & Lazy Images with LazySizes and Umbraco
Release Downloads
Prerelease Downloads
Note Slimsy v3.0.0+ requires Umbraco v8.1.0+
LazySizes.js used in conjunction with ImageProcessor.Web and the built-in Umbraco Image Cropper will make your responsive websites images both adaptive and "retina" quality (if supported by the client browser), the images are also be lazy loaded.
Slimsy includes lazysizes.min.js and picturefill.min.js and some helper methods.
In your master template add the Javascript files
<script src="/scripts/picturefill.min.js"></script>
<script src="/scripts/lazysizes.min.js" async=""></script>
You can of course bundle these together.
e.g.
img {
display:block;
}
3. Adjust your image elements, adding data-srcset
, data-src
, sizes="auto"
& class="lazyload"
attributes
Use the GetSrcSetUrls
UrlHelper extension method to generate your data-srcset
attributes. For these methods to function correctly your image property types should use the built-in Image Cropper.
Use this method for setting the crop dimensions in your Razor code, assumes your image cropper property alias is "umbracoFile"
e.g. An initial image size of 323 x 300. This example is looping people, each page has a media picker property that Models Builder is generating named "Photo"
<div class="employee-grid">
@foreach (ContentModels.Person person in Model.Children)
{
<div class="employee-grid__item">
<div class="employee-grid__item__image">
<img src="@Url.GetCropUrl(person.Photo, 323, 300, quality:30, furtherOptions:"&format=auto")"
data-srcset="@Url.GetSrcSetUrls(person.Photo, 323, 300)"
data-src="@Url.GetCropUrl(person.Photo, 323, 300)"
sizes="auto" class="lazyload" />
</div>
</div>
}
</div>
This example uses the LQIP (low quality image place holder) technique.
Url.GetSrcSetUrls(publishedContent, int width, int height, string propertyAlias, string outputFormat, int quality)
Url.GetSrcSetUrls(publishedContent, int width, int height, ImageCropMode? imageCropMode, string outputFormat)
Slimsy v3 allows you to define a predefined ratio for your image so you don't need to work out the math associated with it, first you instantiate a new built in class of AspectRatio and pass in two integer values, this will crop the image(s) to the desired ratio.
<div class="employee-grid">
@foreach (ContentModels.Person person in Model.Children)
{
var ratio = new AspectRatio(16, 9);
<div class="employee-grid__item">
<div class="employee-grid__item__image">
<img data-srcset="@Url.GetSrcSetUrls(person.Photo, ratio)" data-sizes="auto" class="lazyload" />
</div>
</div>
}
</div>
Use this method when you want to use a predefined crop, assumes your image cropper property alias is "umbracoFile".
In this example the crop name is "feature"
<div class="employee-grid">
@foreach (ContentModels.Person person in Model.Children)
{
<div class="employee-grid__item">
<div class="employee-grid__item__image">
<img data-srcset="@Url.GetSrcSetUrls(person.Photo, "feature")" data-src="@Url.GetCropUrl(person.Photo, "feature")" sizes="auto" class="lazyload" />
</div>
</div>
}
</div>
Html.ConvertImgToSrcSet(IPublishedContent publishedContent, string propertyAlias, bool generateLqip)
Use this method to convert images entered into TinyMce Rich Text editors to use img source set using generated paths
@Html.ConvertImgToSrcSet(Model, "richTextBody")
Use this method to convert images entered in a TinyMce Rich Text editor within the Grid to use img source set using generated paths. This method will also take care of parsing Umbraco links and Macros.
e.g. within Rte.chtml
found within the Partials/Grid/Editors
folder
@Slimsy.ConvertImgToSrcSet(Html, Model.value.ToString(), true)
Below is an example of how to use the <picture>
element to provide automated WebP versions of your images using the ImageProcessor WebP plugin, this example also implements a optional LQIP image.
<div class="employee-grid">
@foreach (ContentModels.Person person in Model.Children)
{
<div class="employee-grid__item">
<div class="employee-grid__item__image">
<picture>
<!--[if IE 9]><video style="display: none"><![endif]-->
<source data-srcset="@Url.GetSrcSetUrls(person.Photo, 323, 300, "umbracoFile", "webp", quality:80)" type="image/webp" data-sizes="auto" />
<source data-srcset="@Url.GetSrcSetUrls(person.Photo, 323, 300)" type="image/jpeg" data-sizes="auto" />
<!--[if IE 9]></video><![endif]-->
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
data-src="@Url.GetCropUrl(person.Photo, 323, 300)"
class="lazyload"
alt="image"
data-sizes="auto" />
</picture>
</div>
</div>
}
</div>
You can specify the default output format for all images.
<add key="Slimsy:Format" value="jpg"/>
You can specify the default background color by added another appsetting to web.config. As an example this setting is used if ImageProcessor is converting a png to a jpg and it as some transparency.
<add key="Slimsy:BGColor" value="fff"/>
You can specify the default quality for all images, unless specified via helper.
<add key="Slimsy:DefaultQuality" value="90"/>
You can specify the max width for the generated srcset sizes.
<add key="Slimsy:MaxWidth" value="2048"/>
You can specify the width step for the generated srcset sizes.
<add key="Slimsy:WidthStep" value="160"/>
Lazysizes.js is awesome and it's what makes Slimsy v2 so easy to implement. If you need to find out more information about it or how to hook into it's Javascript events be sure to check out it's GitHub
It may be useful to use a Razor Helper to render img
or picture
elements, there is an reusable example included in the test site which can be adapted to your own requirement. You can find it here and see it in use here
A test site is included in the solution, the username and password for Umbraco are [email protected]/password1234567890. By default the test site is configured to use full IIS (due to IIS Express SQL CE persistence issue) on the domain slimsy.local, you can change it to use IIS Express if you prefer.
Visual Studio 2017 is required for compiling the source code
This project includes LazySizes and Picturefill Both projects are MIT licensed.
Without the amazing ImageProcessor this package wouldn't exist, so many thanks go to James for creating ImageProcessor!
Many thanks to Douglas Robar for naming Slimsy.