public static UrlBuilder Format(this UrlBuilder target, ResizedImageFormat format) { if (format == ResizedImageFormat.Preserve) target.QueryCollection.Remove("format"); else target.Add("format", format.ToString().ToLowerInvariant()); return target; }
public static UrlBuilder ResizedImageUrl(this HtmlHelper helper, ContentReference image, int width, ResizedImageFormat format = ResizedImageFormat.Preserve) { var baseUrl = ResolveImageUrl(image); return(new UrlBuilder(baseUrl) .Width(width) .Format(format)); }
private static string BuildSize(string imageUrl, int width, ScaleMode sourceMode, AspectRatio sourceTargetAspectRatio, int?sourceQuality, IResponsiveImage focalPoint, int maxImageDimension, ResizedImageFormat format) { var url = BuildResizedImageUrl(imageUrl, width, sourceMode, sourceTargetAspectRatio, sourceQuality, focalPoint, maxImageDimension, format); return($"{url} {width}w"); }
public string TestResizedImageUrl(ResizedImageFormat format) { HtmlHelper html = null; return(html.ResizedImageUrl(new ContentReference(123), 500, format).ToString()); }
private static void RenderBackgroundCssClass(HtmlHelper helper, ContentReference image, StringBuilder stringBuilder, string className, int imageWidth, ResizedImageFormat format) { stringBuilder.Append($".{className} {{ "); stringBuilder.Append( $"background-image: url('{helper.ResizedImageUrl(image, imageWidth, format)}');"); stringBuilder.Append("}"); // css class close }
private static void RenderNonRetinaCss(HtmlHelper helper, ContentReference image, string mediaQuery, StringBuilder stringBuilder, string className, PictureSize allowedSize, ResizedImageFormat format) { if (!string.IsNullOrEmpty(mediaQuery)) { stringBuilder.Append($"@media ({mediaQuery}) {{"); } RenderBackgroundCssClass(helper, image, stringBuilder, className, Math.Min(allowedSize.ImageWidth, MaxResizedImageWidth), format); if (!string.IsNullOrEmpty(mediaQuery)) { stringBuilder.Append("}"); // media query close } }
private static void RenderRetinaCss(HtmlHelper helper, ContentReference image, string mediaQuery, StringBuilder stringBuilder, string className, PictureSize allowedSize, ResizedImageFormat format) { var mediaQuerySelector = string.IsNullOrEmpty(mediaQuery) ? "" : $"and ({mediaQuery})"; stringBuilder.Append($@"@media (-webkit-min-device-pixel-ratio: 2) {mediaQuerySelector}, (min--moz-device-pixel-ratio: 2) {mediaQuerySelector}, (-o-min-device-pixel-ratio: 2/1) {mediaQuerySelector}, ( min-device-pixel-ratio: 2) {mediaQuerySelector} {{"); RenderBackgroundCssClass(helper, image, stringBuilder, className, Math.Min(allowedSize.ImageWidth * 2, MaxResizedImageWidth), format); stringBuilder.Append("}"); }
private static TagBuilder CreateSourceElement(string imageUrl, PictureSource source, IResponsiveImage focalPoint, int maxImageDimension, ResizedImageFormat format) { var srcSets = source.AllowedWidths .Select(width => BuildSize(imageUrl, width, source.Mode, source.TargetAspectRatio, source.Quality, focalPoint, maxImageDimension, format)); var tagBuilder = new TagBuilder("source") { Attributes = { { "media", $"{source.MediaCondition}" }, { "srcset", string.Join(", ", srcSets) }, { "sizes", string.Join(", ", source.Sizes)} } }; return(tagBuilder); }
private static UrlBuilder BuildResizedImageUrl(string imageUrl, int width, ScaleMode scaleMode, AspectRatio targetAspectRatio, int?quality, IResponsiveImage image, int?maxImageDimension, ResizedImageFormat format) { width = Math.Min(width, maxImageDimension ?? int.MaxValue); var target = new UrlBuilder(imageUrl); if (scaleMode != ScaleMode.Default) { target.Mode(scaleMode); } if (quality.HasValue) { target.Quality(quality.Value); } target.Format(format); if (scaleMode != ScaleMode.Default && scaleMode != ScaleMode.Max && image != null) { if (targetAspectRatio == null || !targetAspectRatio.HasValue) { throw new ArgumentException("Aspect ratio is required when ScaleMode is other than Max"); } var height = (int)(width / targetAspectRatio.Ratio); if (height > maxImageDimension) { height = maxImageDimension.Value; width = (int)(height * targetAspectRatio.Ratio); } target.Width(width); target.Height(height); var cropSettings = ImageCropper.GetCropSettings(targetAspectRatio, image); if (cropSettings != null) { target.Crop(cropSettings); } } else { target.Width(width); } return(target); }