Effortless Responsive & Lazy Images with LazySizes and Umbraco
Slimsy v2 is not compatible with Slimsy v1 at all, if you upgrade you will have to refactor all of your code!
Release Downloads
Prerelease Downloads
Note Slimsy v2.0.0+ requires Umbraco v7.6.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. If you don't already have JavaScript bundling in place you should take a look at the Optimus package, it will allow you to bundle them together in minutes.
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 270 x 161. This example is looping pages, each page has a media picker with property alias "Image"
@foreach (var feature in featuredPages)
{
var featureImage = Umbraco.TypedMedia(feature.GetPropertyValue<int>("image"));
<div class="3u">
<!-- Feature -->
<section class="is-feature">
<img src="@Url.GetCropUrl(featureImage, 270, 161, quality:30, furtherOptions:"&format=auto")" data-srcset="@Url.GetSrcSetUrls(featureImage, 270, 161)" data-src="@Url.GetCropUrl(featureImage, 270, 161)" sizes="auto" class="lazyload" />
</section>
</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 v2 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.
@foreach (var feature in featuredPages)
{
var ratio = new AspectRatio(16, 9);
<div class="3u">
<section class="is-feature">
@if (feature.HasValue("image"))
{
var featureImage = Umbraco.TypedMedia(feature.GetPropertyValue<int>("image"));
<a href="@feature.Url" class="image image-full">
<img data-srcset="@Url.GetSrcSetUrls(featureImage, ratio)" data-src="@Url.GetCropUrl(featureImage, 270, 161)" sizes="auto" class="lazyload" />
</a>
}
<h3><a href="@feature.Url">@feature.Name</a></h3>
@Umbraco.Truncate(feature.GetPropertyValue<string>("bodyText"), 100)
</section>
</div>
}
Use this method when you want to use a predefined crop, assumes your image cropper property alias is "umbracoFile".
@foreach (var feature in featuredPages)
{
<div class="3u">
<section class="is-feature">
@if (feature.HasValue("image"))
{
var featureImage = Umbraco.TypedMedia(feature.GetPropertyValue<int>("image"));
<a href="@feature.Url" class="image image-full">
<img data-srcset="@Url.GetSrcSetUrls(featureImage, "home", "umbracoFile")" data-src="@Url.GetCropUrl(featureImage, "umbracoFile", "home")" sizes="auto" class="lazyload"/>
</a>
}
<h3><a href="@feature.Url">@feature.Name</a></h3>
@Umbraco.Truncate(feature.GetPropertyValue<string>("bodyText"), 100)
</section>
</div>
}
Html.ConvertImgToSrcSet(IPublishedContent publishedContent, string propertyAlias, bool generateLqip, bool removeStyleAttribute, bool roundWidthHeight)
Use this method to convert images entered into TinyMce Rich Text editors to use img source set using generated paths
@Html.ConvertImgToSrcSet(Model.Content, "bodyText", true, true)
Html.ConvertImgToSrcSet(string html, bool generateLqip, bool removeStyleAttribute, bool removeUdiAttribute)
Html.ConvertImgToSrcSet(string html, bool generateLqip, bool removeStyleAttribute, bool removeUdiAttribute)
NOTE this method is obsolete in Slimsy v2.1+, use the above method instead
Html.ConvertImgToSrcSet(IHtmlString html, bool generateLqip, bool removeStyleAttribute, bool removeUdiAttribute)
Html.ConvertImgToSrcSet(IHtmlString html, bool generateLqip, bool removeStyleAttribute, bool removeUdiAttribute)
NOTE this method is obsolete in Slimsy v2.1+, use the above method instead
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.
foreach (var caseStudyImage in caseStudyImagesCollection)
{
var imgSrcSet = Url.GetSrcSetUrls(caseStudyImage, 650, 0);
var imgSrcSetWebP = Url.GetSrcSetUrls(caseStudyImage, 650, 0, Constants.Conventions.Media.File, "webp", quality:70);
var imgSrc = Url.GetCropUrl(caseStudyImage, 650, 0);
var imgLqip = Url.GetCropUrl(caseStudyImage, 650, 0, quality: 30, furtherOptions: "&format=auto");
<div class="picture-box">
<picture>
<!--[if IE 9]><video style="display: none"><![endif]-->
<source data-srcset="@imgSrcSetWebP" srcset="@imgLqip" type="image/webp" data-sizes="auto"/>
<source data-srcset="@imgSrcSet" srcset="@imgLqip" type="image/jpeg" data-sizes="auto"/>
<!--[if IE 9]></video><![endif]-->
<img
src="@imgLqip"
data-src="@imgSrc"
class="lazyload"
data-sizes="auto"
alt="@caseStudyImage.Name" />
</picture>
</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 admin/password. 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 2015 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.