示例#1
0
        public static MvcHtmlString ResizedPicture(this HtmlHelper helper,
                                                   ContentReference image,
                                                   PictureProfile profile,
                                                   string fallbackUrl = null,
                                                   ResizedPictureViewModel pictureModel = null)
        {
            if (pictureModel == null)
            {
                pictureModel = new ResizedPictureViewModel();
            }

            var imageFound = ServiceLocator.Current.GetInstance <IContentLoader>()
                             .TryGet <IContentData>(image, new LoaderOptions {
                LanguageLoaderOption.FallbackWithMaster()
            },
                                                    out var content) &&
                             content is IImage;

            var baseUrl = imageFound
                ? ResolveImageUrl(image)
                : fallbackUrl;

            if (!pictureModel.ImgElementAttributes.ContainsKey("alt"))
            {
                var alternateText = imageFound
                    ? ((IImage)content).Description
                    : string.Empty;
                pictureModel.ImgElementAttributes.Add("alt", alternateText);
            }

            return(GenerateResizedPicture(baseUrl, profile, content as IResponsiveImage, pictureModel));
        }
示例#2
0
        private static MvcHtmlString GeneratePictureElement(PictureProfile profile,
                                                            string imgUrl,
                                                            IResponsiveImage focalPoint, ResizedPictureViewModel pictureModel)
        {
            var sourceElements = profile.Sources?.Select(x => CreateSourceElement(imgUrl, x, focalPoint,
                                                                                  profile.MaxImageDimension, profile.Format));

            var pictureElement = new TagBuilder("picture")
            {
                InnerHtml =
                    string.Join(string.Empty,
                                sourceElements?.Select(x => x.ToString(TagRenderMode.SelfClosing)) ?? new string[0]) +
                    CreateImgElement(
                        BuildResizedImageUrl(imgUrl, profile.DefaultWidth, ScaleMode.Default, AspectRatio.Original,
                                             null, focalPoint, null, profile.Format).ToString(), pictureModel.ImgElementAttributes)
                    .ToString(TagRenderMode.SelfClosing)
            };

            foreach (var kv in pictureModel.PictureElementAttributes)
            {
                pictureElement.Attributes.Add(kv.Key, kv.Value);
            }

            return(new MvcHtmlString(pictureElement.ToString()));
        }
示例#3
0
 private static MvcHtmlString GenerateResizedPicture(string imageBaseUrl,
                                                     PictureProfile profile, IResponsiveImage image, ResizedPictureViewModel pictureModel)
 {
     return(GeneratePictureElement(profile, imageBaseUrl, image, pictureModel));
 }