public void AddingResizeBehaviourShouldAddCorrectQueryString(ImageResizeBehaviour behaviour, string expected) { //Arrange var builder = new ImageUrlBuilder(); //Act var result = builder.SetResizingBehaviour(behaviour).Build(); //Assert Assert.Equal(expected, result); }
/// <summary> /// Sets how an image should be resized to fit inside the height and width specified. /// </summary> /// <param name="resizeBehaviour">The resizebehaviour.</param> /// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns> public ImageUrlBuilder SetResizingBehaviour(ImageResizeBehaviour resizeBehaviour) { if (resizeBehaviour == ImageResizeBehaviour.Default) { return(this); } _querystringValues.Add(new KeyValuePair <string, string>("fit", resizeBehaviour.ToString().ToLower())); return(this); }
/// <summary> /// Creates an image tag for an asset. /// </summary> /// <param name="doc">The IDocument.</param> /// <param name="token">The Json token representing the asset.</param> /// <param name="alt">The alt text of the image. Will default to the title of the asset if null.</param> /// <param name="width">The width of the image.</param> /// <param name="height">The height of the image.</param> /// <param name="jpgQuality">The quality of the image.</param> /// <param name="resizeBehaviour">How the image should resize to conform to the width and height.</param> /// <param name="format">The format of the image, jpg,png or webp.</param> /// <param name="cornerRadius">The corner radius of the image.</param> /// <param name="focus">The focus area of the image when resizing.</param> /// <param name="backgroundColor">The background color of any padding that is added to the image.</param> /// <returns>The image tag as a string.</returns> public static string ImageTagForAsset(this IDocument doc, JToken token, string alt = null, int?width = null, int?height = null, int?jpgQuality = null, ImageResizeBehaviour resizeBehaviour = ImageResizeBehaviour.Default, ImageFormat format = ImageFormat.Default, int?cornerRadius = 0, ImageFocusArea focus = ImageFocusArea.Default, string backgroundColor = null) { if (token["sys"] == null || token["sys"]["id"] == null) { return(null); } return(ImageTagForAsset(doc, token["sys"]["id"].ToString(), alt, width, height, jpgQuality, resizeBehaviour, format, cornerRadius, focus, backgroundColor)); }
/// <summary> /// Creates an image tag for an asset. /// </summary> /// <param name="doc">The IDocument.</param> /// <param name="assetId">The id of the asset.</param> /// <param name="alt">The alt text of the image. Will default to the title of the asset if null.</param> /// <param name="width">The width of the image.</param> /// <param name="height">The height of the image.</param> /// <param name="jpgQuality">The quality of the image.</param> /// <param name="resizeBehaviour">How the image should resize to conform to the width and height.</param> /// <param name="format">The format of the image, jpg,png or webp.</param> /// <param name="cornerRadius">The corner radius of the image.</param> /// <param name="focus">The focus area of the image when resizing.</param> /// <param name="backgroundColor">The background color of any padding that is added to the image.</param> /// <returns>The image tag as a string.</returns> public static string ImageTagForAsset(this IDocument doc, string assetId, string alt = null, int?width = null, int?height = null, int?jpgQuality = null, ImageResizeBehaviour resizeBehaviour = ImageResizeBehaviour.Default, ImageFormat format = ImageFormat.Default, int?cornerRadius = 0, ImageFocusArea focus = ImageFocusArea.Default, string backgroundColor = null) { var asset = doc.List <Asset>(ContentfulKeys.IncludedAssets)?.FirstOrDefault(c => c.SystemProperties.Id == assetId); if (asset == null) { return(string.Empty); } var locale = doc.Get <string>(ContentfulKeys.EntryLocale); var imageUrlBuilder = ImageUrlBuilder.New(); if (width.HasValue) { imageUrlBuilder.SetWidth(width.Value); } if (height.HasValue) { imageUrlBuilder.SetHeight(height.Value); } if (jpgQuality.HasValue) { imageUrlBuilder.SetJpgQuality(jpgQuality.Value); } if (cornerRadius.HasValue) { imageUrlBuilder.SetCornerRadius(cornerRadius.Value); } imageUrlBuilder.SetResizingBehaviour(resizeBehaviour).SetFormat(format).SetFocusArea(focus).SetBackgroundColor(backgroundColor); if (alt == null && !string.IsNullOrEmpty(asset.TitleLocalized[locale])) { alt = asset.TitleLocalized[locale]; } return($@"<img src=""{asset.FilesLocalized[locale].Url + imageUrlBuilder.Build()}"" alt=""{alt}"" height=""{height}"" width=""{width}"" />"); }