One option is to use sizes
srcset
on your <img>
element. I've recently been a project where this was the approach.
Set it up like this. Example from a Razor View.
<img src="@Model.ImageUrl" sizes="(max-width: 764px) 50vw, 25vw" srcset="@Model.ImageSrcSet" />
While this technically does not give you options for mobile devices, it does give you something along the lines of what you are asking.
More information about sizes
and srcset
here: Responsive Images - The srcset and sizes Attributes
On the Sitecore side of things, you need to generate a list of Image Urls for the image. With different maxWidth
constraints. One way to achieve that, is something like this:
ImageField imageField = item.Fields[imageFieldName];
if (imageField?.MediaItem != null /*&& imageField.MediaItem.Versions.Count > 0*/)
{
var imageUrl = HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = maxWidth}));
// ReSharper disable once UseObjectOrCollectionInitializer
var urls = new List<string>();
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 100})) + " 100w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 200})) + " 200w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 300})) + " 300w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 400})) + " 400w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 500})) + " 500w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 600})) + " 600w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 1150})) + " 1150w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 1290})) + " 1290w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 1440})) + " 1440w");
urls.Add(HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions {MaxWidth = 1920})) + " 1920w");
return new Tuple<string, string>(imageUrl, string.Join(",", urls));
}
Be sure to call in with a reasonable maxwidth as default, something like 300 or whatever is appropriate.
The upside to this; the device will try and download the most appropriate image resolution for the display device. Which is good.
The downside to this; many mobile phones have very high resolution displays on a very small surface area. This approach can still end up selecting a too high resolution image for a 3G connection.
That said; even if you DID do a 2nd field for "mobile images" - you would still need to determine "mobile". If you're going to do that based on device parameters - well then the above solution will do just as well. If - however - you aim to download the "mobile" image when the phone (or any device) is on a slow(ish) connection - well then you need something different. I'm not even sure how you could detect this.