public static HtmlString AdvancedImageField(this SitecoreHelper helper, string fieldName, Item item, int width = 0, int height = 0, string alt = null, string cssClass = null, bool disableWebEditing = false) { /* * Example usage in view @Html.Sitecore().AdvancedImageField("Cropped", Model.Item, 100, 400) * * */ if (item == null) { return(new HtmlString("No datasource set")); } ImageField imageField = new ImageField(item.Fields[fieldName]); if (String.IsNullOrEmpty(imageField.Value)) { return(new HtmlString(String.Empty)); } XmlDocument xml = new XmlDocument(); xml.LoadXml(imageField.Value); if (xml.DocumentElement == null) { return(new HtmlString("No cropping image parameters found.")); } string cropx = xml.DocumentElement.HasAttribute("cropx") ? xml.DocumentElement.GetAttribute("cropx") : string.Empty; string cropy = xml.DocumentElement.HasAttribute("cropy") ? xml.DocumentElement.GetAttribute("cropy") : string.Empty; string focusx = xml.DocumentElement.HasAttribute("focusx") ? xml.DocumentElement.GetAttribute("focusx") : string.Empty; string focusy = xml.DocumentElement.HasAttribute("focusy") ? xml.DocumentElement.GetAttribute("focusy") : string.Empty; float.TryParse(cropx, out float cx); float.TryParse(cropy, out float cy); float.TryParse(focusx, out float fx); float.TryParse(focusy, out float fy); string imageSrc = MediaManager.GetMediaUrl(imageField.MediaItem); string src = $"{imageSrc}?cx={cx}&cy={cy}&cw={width}&ch={height}"; string hash = HashingUtils.GetAssetUrlHash(src); TagBuilder builder = new TagBuilder("img"); builder.Attributes.Add("src", $"{src}&hash={hash}"); builder.Attributes.Add("alt", !String.IsNullOrEmpty(imageField.Alt) ? imageField.Alt : alt); if (!string.IsNullOrEmpty(cssClass)) { builder.AddCssClass(cssClass); } return(MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal))); }
public static string GetUrl(this AdvanceImageField imageField, int width = 0, int height = 0) { if (imageField == null) { return(string.Empty); } if (width <= 0 || height <= 0) { return(string.Empty); } var src = string.Format("{0}?cx={1}&cy={2}&cw={3}&ch={4}", imageField.Src, imageField.CropX, imageField.CropY, width, height); var hash = HashingUtils.GetAssetUrlHash(src); return(string.Format("{0}&hash={1}", src, hash)); }
protected override MediaOptions GetOptions() { var queryString = this.InnerRequest.QueryString; if (queryString != null && !string.IsNullOrEmpty(queryString.Get("cx"))) { options = new MediaOptions(); this.ProcessCustomParameters(options); if (!options.CustomOptions.ContainsKey("cx") && !string.IsNullOrEmpty(queryString.Get("cx"))) { options.CustomOptions.Add("cx", queryString.Get("cx")); } if (!options.CustomOptions.ContainsKey("cy") && !string.IsNullOrEmpty(queryString.Get("cy"))) { options.CustomOptions.Add("cy", queryString.Get("cy")); } if (!options.CustomOptions.ContainsKey("cw") && !string.IsNullOrEmpty(queryString.Get("cw"))) { options.CustomOptions.Add("cw", queryString.Get("cw")); } if (!options.CustomOptions.ContainsKey("ch") && !string.IsNullOrEmpty(queryString.Get("ch"))) { options.CustomOptions.Add("ch", queryString.Get("ch")); } } else { MediaUrlOptions mediaQueryString = this.GetMediaQueryString(); options = new MediaOptions { AllowStretch = mediaQueryString.AllowStretch, BackgroundColor = mediaQueryString.BackgroundColor, IgnoreAspectRatio = mediaQueryString.IgnoreAspectRatio, Scale = mediaQueryString.Scale, Width = mediaQueryString.Width, Height = mediaQueryString.Height, MaxWidth = mediaQueryString.MaxWidth, MaxHeight = mediaQueryString.MaxHeight, Thumbnail = mediaQueryString.Thumbnail }; if (mediaQueryString.DisableMediaCache) { options.UseMediaCache = false; } string[] strArray = queryString.AllKeys; for (int i = 0; i < strArray.Length; i = (int)(i + 1)) { string str = strArray[i]; if ((str != null) && (queryString.Get(str) != null)) { options.CustomOptions[str] = queryString.Get(str); } } } if (!this.IsRawUrlSafe) { if (Settings.Media.RequestProtection.LoggingEnabled) { string urlReferrer = this.GetUrlReferrer(); Log.SingleError(string.Format("MediaRequestProtection: An invalid/missing hash value was encountered. The expected hash value: {0}. Media URL: {1}, Referring URL: {2}", HashingUtils.GetAssetUrlHash(this.InnerRequest.Path), this.InnerRequest.Path, string.IsNullOrEmpty(urlReferrer) ? ((object)"(empty)") : ((object)urlReferrer)), this); } options = new MediaOptions(); } return(options); }