There is no out of the box way to do this as of SXA 1.8.1 or below.
With the following code you can extend the existing Rendering Variant section renderer to include this logic when it encounters an Image field.
First, override the built-in RenderSection pipeline:
public class RenderSection : Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection
{
protected override string GetAttributeTokenValue(string fieldName, Item item)
{
var field = FieldTypeManager.GetField(item.Fields[fieldName]);
if (field is ImageField)
{
return item.ImageFieldUrl(fieldName);
}
return base.GetAttributeTokenValue(fieldName, item);
}
}
Patch it as follows
<pipelines>
<renderVariantField>
<processor type="Your.Foundation.Variants.Pipelines.RenderSection, Your.Foundation.Variants"
patch:instead="processor[@type='Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection, Sitecore.XA.Foundation.RenderingVariants']"
resolve="true" />
</renderVariantField>
</pipelines>
And you'll need to put this in a static class (e.g. ItemExtensions)
public static string ImageFieldUrl(this Item item, string fieldName)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
if (string.IsNullOrEmpty(fieldName))
{
throw new ArgumentNullException(nameof(fieldName));
}
var field = (ImageField) item.Fields[fieldName];
if (field == null)
{
return string.Empty;
}
else
{
var urlOptions = new MediaUrlOptions();
if (int.TryParse(field.Height, out var height) && int.TryParse(field.Width, out var width))
{
urlOptions.Width = width;
urlOptions.Height = height;
}
return HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(field.MediaItem, urlOptions));
}
}