public static HtmlTag PictureImgThumbWithLink(PictureAsset pictureAsset, string linkTo)
        {
            if (pictureAsset == null)
            {
                return(HtmlTag.Empty());
            }

            var imgTag = PictureImgTagWithSmallestDefaultSrc(pictureAsset);

            if (imgTag.IsEmpty())
            {
                return(HtmlTag.Empty());
            }

            imgTag.AddClass(pictureAsset.SmallPicture.Height > pictureAsset.SmallPicture.Width
                ? "thumb-vertical"
                : "thumb-horizontal");

            if (string.IsNullOrWhiteSpace(linkTo))
            {
                return(imgTag);
            }

            var outerLink = new LinkTag(string.Empty, linkTo);

            outerLink.Children.Add(imgTag);

            return(outerLink);
        }
        public static HtmlTag PictureImgTag(PictureAsset pictureDirectoryInfo, string sizes,
                                            bool willHaveVisibleCaption)
        {
            var imageTag = new HtmlTag("img").AddClass("single-photo")
                           .Attr("srcset", pictureDirectoryInfo.SrcSetString())
                           .Attr("src", pictureDirectoryInfo.DisplayPicture.SiteUrl)
                           .Attr("height", pictureDirectoryInfo.DisplayPicture.Height)
                           .Attr("width", pictureDirectoryInfo.DisplayPicture.Width).Attr("loading", "lazy");

            if (!string.IsNullOrWhiteSpace(sizes))
            {
                imageTag.Attr("sizes", sizes);
            }
            else
            {
                imageTag.Attr("sizes", "100vw");
            }

            if (!string.IsNullOrWhiteSpace(pictureDirectoryInfo.DisplayPicture.AltText))
            {
                imageTag.Attr("alt", pictureDirectoryInfo.DisplayPicture.AltText);
            }

            if (!willHaveVisibleCaption && string.IsNullOrWhiteSpace(pictureDirectoryInfo.DisplayPicture.AltText) &&
                !string.IsNullOrWhiteSpace(((ITitleSummarySlugFolder)pictureDirectoryInfo.DbEntry).Summary))
            {
                imageTag.Attr("alt", ((ITitleSummarySlugFolder)pictureDirectoryInfo.DbEntry).Summary);
            }

            return(imageTag);
        }
Exemple #3
0
        public static HtmlTag PictureEmailImgTag(PictureAsset pictureDirectoryInfo, bool willHaveVisibleCaption)
        {
            var emailSize = pictureDirectoryInfo.SrcsetImages.Where(x => x.Width < 800).OrderByDescending(x => x.Width)
                            .First();

            var stringWidth = "94%";

            if (emailSize.Width < emailSize.Height)
            {
                stringWidth = emailSize.Height > 600
                    ? ((int)(600M / emailSize.Height * emailSize.Width)).ToString("F0")
                    : emailSize.Width.ToString("F0");
            }

            var imageTag = new HtmlTag("img").Attr("src", $"https:{emailSize.SiteUrl}")
                           .Attr("max-height", emailSize.Height).Attr("max-width", emailSize.Width).Attr("width", stringWidth);

            if (!string.IsNullOrWhiteSpace(emailSize.AltText))
            {
                imageTag.Attr("alt", emailSize.AltText);
            }

            if (!willHaveVisibleCaption && string.IsNullOrWhiteSpace(emailSize.AltText) &&
                !string.IsNullOrWhiteSpace(((ITitleSummarySlugFolder)pictureDirectoryInfo.DbEntry).Summary))
            {
                imageTag.Attr("alt", ((ITitleSummarySlugFolder)pictureDirectoryInfo.DbEntry).Summary);
            }

            return(imageTag);
        }
        public static PictureAsset ProcessImageDirectory(ImageContent dbEntry, DirectoryInfo directoryInfo,
                                                         string siteUrl)
        {
            var toReturn = new PictureAsset {
                DbEntry = dbEntry
            };

            var baseFileNameList = dbEntry.OriginalFileName.Split(".").ToList();
            var baseFileName     = string.Join("", baseFileNameList.Take(baseFileNameList.Count - 1));

            var fileVariants = directoryInfo.GetFiles().Where(x => x.Name.StartsWith($"{baseFileName}--")).ToList();

            var displayImageFile = fileVariants.SingleOrDefault(x => x.Name.Contains("--For-Display"));

            if (displayImageFile != null && displayImageFile.Exists)
            {
                toReturn.DisplayPicture = new PictureFile
                {
                    FileName = displayImageFile.Name,
                    SiteUrl  = $@"//{siteUrl}/Images/{dbEntry.Folder}/{dbEntry.Slug}/{displayImageFile.Name}",
                    File     = displayImageFile,
                    AltText  = dbEntry.AltText ?? string.Empty,
                    Height   =
                        int.Parse(Regex
                                  .Match(displayImageFile.Name, @".*--(?<height>\d*)h.*", RegexOptions.Singleline)
                                  .Groups["height"].Value),
                    Width = int.Parse(Regex
                                      .Match(displayImageFile.Name, @".*--(?<width>\d*)w.*", RegexOptions.Singleline)
                                      .Groups["width"].Value)
                }
            }
            ;

            var srcsetImageFiles = fileVariants.Where(x => x.Name.Contains("--Sized")).ToList();

            toReturn.SrcsetImages = srcsetImageFiles.Select(x => new PictureFile
            {
                FileName = x.Name,
                Height   =
                    int.Parse(Regex.Match(x.Name, @".*--(?<height>\d*)h.*", RegexOptions.Singleline)
                              .Groups["height"].Value),
                Width = int.Parse(Regex.Match(x.Name, @".*--(?<width>\d*)w.*", RegexOptions.Singleline)
                                  .Groups["width"].Value),
                SiteUrl = $@"//{siteUrl}/Images/{dbEntry.Folder}/{dbEntry.Slug}/{x.Name}",
                AltText = dbEntry.AltText ?? string.Empty,
                File    = x
            }).ToList();

            if (srcsetImageFiles.Any())
            {
                toReturn.LargePicture =
                    toReturn.SrcsetImages.OrderByDescending(x => Math.Max(x.Height, x.Width)).First();
                toReturn.SmallPicture = toReturn.SrcsetImages.OrderBy(x => Math.Max(x.Height, x.Width)).First();
            }

            return(toReturn);
        }
        public static HtmlTag PictureImgTagDisplayImageOnly(PictureAsset pictureDirectoryInfo)
        {
            var imageTag = new HtmlTag("img").AddClass("single-photo")
                           .Attr("src", $"https:{pictureDirectoryInfo.DisplayPicture.SiteUrl}")
                           .Attr("height", pictureDirectoryInfo.DisplayPicture.Height)
                           .Attr("width", pictureDirectoryInfo.DisplayPicture.Width);

            if (!string.IsNullOrWhiteSpace(pictureDirectoryInfo.DisplayPicture.AltText))
            {
                imageTag.Attr("alt", pictureDirectoryInfo.DisplayPicture.AltText);
            }

            return(imageTag);
        }
        public static HtmlTag PictureImgTagWithSmallestDefaultSrc(PictureAsset pictureAsset)
        {
            if (pictureAsset == null)
            {
                return(HtmlTag.Empty());
            }

            var imageTag = new HtmlTag("img").AddClass("thumb-photo").Attr("srcset", pictureAsset.SrcSetString())
                           .Attr("src", pictureAsset.SmallPicture.SiteUrl).Attr("height", pictureAsset.SmallPicture.Height)
                           .Attr("width", pictureAsset.SmallPicture.Width).Attr("loading", "lazy");

            var smallestGreaterThan100 = pictureAsset.SrcsetImages.Where(x => x.Width > 100).OrderBy(x => x.Width)
                                         .FirstOrDefault();

            imageTag.Attr("sizes", smallestGreaterThan100 == null ? "100px" : $"{smallestGreaterThan100.Width}px");

            if (!string.IsNullOrWhiteSpace(pictureAsset.DisplayPicture.AltText))
            {
                imageTag.Attr("alt", pictureAsset.DisplayPicture.AltText);
            }

            return(imageTag);
        }