Exemplo n.º 1
0
        /// <summary>
        /// Generates an IMG tag for an inline image.
        /// </summary>
        /// <param name="htmlHelper">HTML helper.</param>
        /// <param name="image">Inline image.</param>
        /// <param name="sizes">Media conditions mapping screen width to image size</param>
        public static IHtmlContent InlineImage(this IHtmlHelper htmlHelper, IConfiguration configuration, IInlineImage image, ResponsiveImageSizes sizes = null)
        {
            if (image == null)
            {
                return(new HtmlString(string.Empty));
            }

            var  imageTag = new TagBuilder("img");
            bool responsiveImagesEnabled = configuration.GetSection(nameof(AppConfiguration)).Get <AppConfiguration>().ResponsiveImagesEnabled;

            if (responsiveImagesEnabled)
            {
                imageTag.MergeAttribute("srcset", GenerateSrcsetValue(image.Src, configuration));

                if (sizes != null)
                {
                    imageTag.MergeAttribute("sizes", sizes.GenerateSizesValue());
                }
            }

            imageTag.MergeAttribute("src", image.Src);
            imageTag.MergeAttribute("alt", image.AltText);
            imageTag.TagRenderMode = TagRenderMode.SelfClosing;

            string result;

            using (var writer = new StringWriter())
            {
                imageTag.WriteTo(writer, HtmlEncoder.Default);
                result = writer.ToString();
            }

            return(new HtmlString(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates an IMG tag for an image file.
        /// </summary>
        /// <remarks>Should replace it with the asp.net core's img TagHelper in each view</remarks>
        /// <param name="htmlHelper">HTML helper.</param>
        /// <param name="asset">Asset</param>
        /// <param name="title">Title</param>
        /// <param name="cssClass">CSS class</param>
        /// <param name="width">Optional width size</param>
        /// <param name="height">Optional height size</param>
        /// <param name="sizes">Media conditions mapping screen width to image size</param>
        public static IHtmlContent AssetImage(this IHtmlHelper htmlHelper, IConfiguration configuration, Asset asset, string title = null, string cssClass = "", int?width = null, int?height = null, ResponsiveImageSizes sizes = null)
        {
            if (asset == null)
            {
                return(new HtmlString(string.Empty));
            }

            var imageUrlBuilder = new ImageUrlBuilder(asset.Url);
            var image           = new TagBuilder("img");

            if (width.HasValue)
            {
                image.MergeAttribute("width", width.ToString());
                imageUrlBuilder = imageUrlBuilder.WithWidth(Convert.ToDouble(width));
            }

            if (height.HasValue)
            {
                image.MergeAttribute("height", height.ToString());
                imageUrlBuilder = imageUrlBuilder.WithHeight(Convert.ToDouble(height));
            }

            bool responsiveImagesEnabled = configuration.GetSection(nameof(AppConfiguration)).Get <AppConfiguration>().ResponsiveImagesEnabled;

            if (responsiveImagesEnabled && !width.HasValue && !height.HasValue)
            {
                image.MergeAttribute("srcset", GenerateSrcsetValue(asset.Url, configuration));

                if (sizes != null)
                {
                    image.MergeAttribute("sizes", sizes.GenerateSizesValue());
                }
            }

            image.MergeAttribute("src", $"{imageUrlBuilder.Url}");
            image.AddCssClass(cssClass);
            string titleToUse = title ?? asset.Description ?? string.Empty;

            image.MergeAttribute("alt", titleToUse);
            image.MergeAttribute("title", titleToUse);
            image.TagRenderMode = TagRenderMode.SelfClosing;

            string result;

            using (var writer = new StringWriter())
            {
                image.WriteTo(writer, HtmlEncoder.Default);
                result = writer.ToString();
            }

            return(new HtmlString(result));
        }
        /// <summary>
        /// Generates an IMG tag for an image file.
        /// </summary>
        /// <param name="htmlHelper">HTML helper.</param>
        /// <param name="asset">Asset</param>
        /// <param name="title">Title</param>
        /// <param name="cssClass">CSS class</param>
        /// <param name="width">Optional width size</param>
        /// <param name="height">Optional height size</param>
        /// <param name="sizes">Media conditions mapping screen width to image size</param>
        public static MvcHtmlString AssetImage(this HtmlHelper htmlHelper, Asset asset, string title = null, string cssClass = "", int?width = null, int?height = null, ResponsiveImageSizes sizes = null)
        {
            if (asset == null)
            {
                return(MvcHtmlString.Empty);
            }

            var imageUrlBuilder = new ImageUrlBuilder(asset.Url);
            var image           = new TagBuilder("img");

            if (width.HasValue)
            {
                image.MergeAttribute("width", width.ToString());
                imageUrlBuilder = imageUrlBuilder.WithWidth(Convert.ToDouble(width));
            }

            if (height.HasValue)
            {
                image.MergeAttribute("height", height.ToString());
                imageUrlBuilder = imageUrlBuilder.WithHeight(Convert.ToDouble(height));
            }

            if (AppSettingProvider.ResponsiveImagesEnabled && !width.HasValue && !height.HasValue)
            {
                image.MergeAttribute("srcset", GenerateSrcsetValue(asset.Url));

                if (sizes != null)
                {
                    image.MergeAttribute("sizes", sizes.GenerateSizesValue());
                }
            }

            image.MergeAttribute("src", $"{imageUrlBuilder.Url}");
            image.AddCssClass(cssClass);
            string titleToUse = title ?? asset.Description ?? string.Empty;

            image.MergeAttribute("alt", titleToUse);
            image.MergeAttribute("title", titleToUse);

            return(MvcHtmlString.Create(image.ToString(TagRenderMode.SelfClosing)));
        }
        /// <summary>
        /// Generates an IMG tag for an inline image.
        /// </summary>
        /// <param name="htmlHelper">HTML helper.</param>
        /// <param name="image">Inline image.</param>
        /// <param name="sizes">Media conditions mapping screen width to image size</param>
        public static MvcHtmlString InlineImage(this HtmlHelper htmlHelper, IInlineImage image, ResponsiveImageSizes sizes = null)
        {
            if (image == null)
            {
                return(MvcHtmlString.Empty);
            }

            var imageTag = new TagBuilder("img");

            if (AppSettingProvider.ResponsiveImagesEnabled)
            {
                imageTag.MergeAttribute("srcset", GenerateSrcsetValue(image.Src));

                if (sizes != null)
                {
                    imageTag.MergeAttribute("sizes", sizes.GenerateSizesValue());
                }
            }

            imageTag.MergeAttribute("src", image.Src);
            imageTag.MergeAttribute("alt", image.AltText);

            return(MvcHtmlString.Create(imageTag.ToString(TagRenderMode.SelfClosing)));
        }