/// <summary> /// Builds a url to a contentful image using the specified properties. /// </summary> /// <returns>The url.</returns> public async Task <string> BuildUrl() { if (Asset != null) { Url = Asset.File?.Url; if (string.IsNullOrEmpty(Url)) { AssetId = Asset.SystemProperties?.Id; } } if (string.IsNullOrEmpty(Url)) { var asset = await _client.GetAssetAsync(AssetId, ""); Url = asset.File.Url; } var queryBuilder = new ImageUrlBuilder(); if (Width > 0) { queryBuilder.SetWidth(Width); } if (Height > 0) { queryBuilder.SetHeight(Height); } if (JpgQuality.HasValue) { queryBuilder.SetJpgQuality(JpgQuality.Value); } if (CornerRadius.HasValue) { queryBuilder.SetCornerRadius(CornerRadius.Value); } if (!string.IsNullOrEmpty(BackgroundColor)) { queryBuilder.SetBackgroundColor(BackgroundColor); } queryBuilder.SetFocusArea(FocusArea); queryBuilder.SetResizingBehaviour(ResizeBehaviour); queryBuilder.SetFormat(Format); if (ProgressiveJpg) { queryBuilder.UseProgressiveJpg(); } return($"{Url}{queryBuilder.Build()}"); }
/// <summary> /// Executes the taghelper. /// </summary> /// <param name="context"></param> /// <param name="output"></param> /// <returns></returns> public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (string.IsNullOrEmpty(Url)) { var asset = await _client.GetAssetAsync(AssetId, ""); Url = asset.File.Url; } var queryBuilder = new ImageUrlBuilder(); if (Width > 0) { queryBuilder.SetWidth(Width); } if (Height > 0) { queryBuilder.SetHeight(Height); } if (JpgQuality.HasValue) { queryBuilder.SetJpgQuality(JpgQuality.Value); } if (CornerRadius.HasValue) { queryBuilder.SetCornerRadius(CornerRadius.Value); } if (!string.IsNullOrEmpty(BackgroundColor)) { queryBuilder.SetBackgroundColor(BackgroundColor); } queryBuilder.SetFocusArea(FocusArea); queryBuilder.SetResizingBehaviour(ResizeBehaviour); queryBuilder.SetFormat(Format); if (ProgressiveJpg) { queryBuilder.UseProgressiveJpg(); } output.TagName = "img"; output.Attributes.Add("src", Url + queryBuilder.Build()); }
/// <summary> /// Executes the taghelper. /// </summary> /// <param name="context"></param> /// <param name="output"></param> /// <returns></returns> public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (string.IsNullOrEmpty(AssetId)) { return; } var queryBuilder = QueryBuilder <Asset> .New; if (!string.IsNullOrEmpty(Locale)) { queryBuilder = queryBuilder.LocaleIs(Locale); } var asset = await _client.GetAssetAsync(AssetId, queryBuilder); output.Attributes.RemoveAll("asset-id"); output.Attributes.RemoveAll("locale"); output.Attributes.SetAttribute("href", asset.File.Url); }