public void AddingFocusAreaShouldSetCorrectQueryString(ImageFocusArea focusArea)
        {
            //Arrange
            var builder = new ImageUrlBuilder();
            //Act
            var result = builder.SetFocusArea(focusArea).Build();

            //Assert
            Assert.Equal($"?f={focusArea.ToString().ToLower()}", result);
        }
Example #2
0
        /// <summary>
        /// Sets what focus area should be used when resizing.
        /// </summary>
        /// <param name="focusArea">The area to focus the image on.</param>
        /// <returns>The <see cref="ImageUrlBuilder"/> instance.</returns>
        public ImageUrlBuilder SetFocusArea(ImageFocusArea focusArea)
        {
            if (focusArea == ImageFocusArea.Default)
            {
                return(this);
            }

            _querystringValues.Add(new KeyValuePair <string, string>("f", focusArea.ToString().ToLower()));
            return(this);
        }
Example #3
0
        public void AddingFocusAreaShouldSetCorrectQueryString(ImageFocusArea focusArea, string expected)
        {
            //Arrange
            var builder = new ImageUrlBuilder();
            //Act
            var result = builder.SetFocusArea(focusArea).Build();

            //Assert
            Assert.Equal(expected, result);
        }
        /// <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}"" />");
        }