public static string GetThumbLink(string imagePath, ThumbSizeEnum thumbSize, bool includeMediaDomain = true) { string subPath = ""; if (imagePath != null) { if (imagePath.StartsWith("~/")) { // subPath = imagePath.Substring(1, imagePath.LastIndexOf("/")); subPath = imagePath.Substring(1, imagePath.Length - 1); } else { // subPath = imagePath.Substring(0, imagePath.LastIndexOf("/")); subPath = imagePath; } } string currentPath = "/Thumbnail/" + thumbSize.ToString() + subPath; if (includeMediaDomain) { currentPath = EngineContext.Current.FalconConfig.MediaDomainName + currentPath; } return(currentPath); }
/// <summary> /// Tạo Link Image với ảnh thumb /// </summary> /// <param name="htmlHelper"></param> /// <param name="thumbSize"></param> /// <param name="imgSrc"></param> /// <param name="alt"></param> /// <param name="actionName"></param> /// <param name="controllerName"></param> /// <param name="routeValues"></param> /// <param name="htmlAttributes"></param> /// <param name="imgHtmlAttributes"></param> /// <returns></returns> public static MvcHtmlString ImageActionLink(this HtmlHelper htmlHelper, ThumbSizeEnum thumbSize, string imgSrc, string alt, string actionName, string controllerName, object routeValues = null, object htmlAttributes = null, object imgHtmlAttributes = null, bool lazyload = false) { object imgAtt; if (imgHtmlAttributes == null) { imgAtt = new { alt = alt }; } else { imgAtt = imgHtmlAttributes; } imgSrc = ThumbnailExtensions.Thumbnail(htmlHelper, thumbSize, imgSrc, alt, alt, imgAtt, true, lazyload).ToString(); UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; //TagBuilder imgTag = new TagBuilder("img"); //imgTag.MergeAttribute("src", imgSrc); //imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true); string url = urlHelper.Action(actionName, controllerName, routeValues); TagBuilder imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); imglink.InnerHtml = imgSrc; //imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true); imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return(MvcHtmlString.Create(imglink.ToString())); }
public static MvcHtmlString ImageActionLink(this HtmlHelper htmlHelper, ThumbSizeEnum thumbSize, string imgSrc, string alt, object htmlAttributes = null, object imgHtmlAttributes = null) { string path = ""; if (imgSrc != null) { if (imgSrc.StartsWith("~/")) { path = imgSrc.Substring(1); } else { path = imgSrc; } } imgSrc = ThumbnailExtensions.Thumbnail(htmlHelper, thumbSize, imgSrc, alt, alt, imgHtmlAttributes, true, false).ToString(); string url = path; TagBuilder imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); imglink.InnerHtml = imgSrc; // imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true); imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return(MvcHtmlString.Create(imglink.ToString())); }
public static MvcHtmlString Thumbnail(this HtmlHelper helper, ThumbSizeEnum thumbSize, string imagePath, string imageAlt = "", string imageTitle = "", object htmlAttributes = null, bool includeMediaDomain = true, bool lazyload = false, bool cache = false) { if (string.IsNullOrEmpty(imagePath)) { return(MvcHtmlString.Create(string.Empty)); } string currentPath = Util.GetThumbLink(imagePath, thumbSize, includeMediaDomain); var img = new TagBuilder("img"); var noscript = new TagBuilder("noscript"); if (lazyload == true) { img.MergeAttribute("class", "lazyload"); img.MergeAttribute("src", "/Themes/Portal/Default/Images/bg_lazyload_140.gif"); img.MergeAttribute("data-original", currentPath); noscript.InnerHtml = "<img src='" + currentPath + "'/>"; } else { img.MergeAttribute("src", currentPath); } if (!string.IsNullOrEmpty(imageAlt)) { img.MergeAttribute("alt", imageAlt); } if (!string.IsNullOrEmpty(imageTitle)) { img.MergeAttribute("title", imageTitle); } img.MergeAttributes(new RouteValueDictionary(htmlAttributes)); var c = cache ? 1 : 0; var onerror = string.Format("/Thumbnail.ashx?i={0}&s={1}&c={2}", imagePath, GetImageSize(thumbSize), c); if (lazyload) { img.MergeAttribute("data-onerror", onerror); } else { img.MergeAttribute("onerror", "this.src='" + onerror + "'"); } if (lazyload) { return(MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing) + noscript.ToString())); } else { return(MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing))); } }
public static string GetImageSize(ThumbSizeEnum size) { switch (size) { case ThumbSizeEnum.Small: return("s"); case ThumbSizeEnum.Medium: return("m"); case ThumbSizeEnum.Large: return("l"); case ThumbSizeEnum.ExtraLarge: return("xl"); default: return("s"); } }
public bool CreateThumnail(ThumbSizeEnum thumbSize, string imagePath) { try { if (string.IsNullOrEmpty(imagePath)) { return(false); } if (File.Exists(HttpContext.Current.Server.MapPath(imagePath))) { string subPath; if (!imagePath.StartsWith("~/")) { imagePath = "~" + imagePath; } subPath = imagePath.Substring(1, imagePath.LastIndexOf("/")); string savePath = "~/Thumbnail/" + thumbSize.ToString() + subPath; if (!Directory.Exists(HttpContext.Current.Server.MapPath(savePath))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(savePath)); } savePath += imagePath.Substring(imagePath.LastIndexOf("/")); ThumbnailSetting setting = _thumbService.GetByThumbSize(thumbSize); if (setting == null) { return(false); } return(CreateImage(imagePath, setting.Width, setting.Height, savePath)); } return(false); } catch (Exception ex) { return(false); } }
private void ReturnDefaultImage(HttpContext context) { string size = context.Request.QueryString["s"]; if (!String.IsNullOrEmpty(size)) { ThumbSizeEnum thumbSize = GetThumbSize(size); if (thumbSize == ThumbSizeEnum.Small) { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_64_64.png")); } else if (thumbSize == ThumbSizeEnum.Medium) { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_64_64.png")); } else if (thumbSize == ThumbSizeEnum.Large) { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_64_64.png")); } else if (thumbSize == ThumbSizeEnum.ExtraLarge) { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_340_300.png")); } else if (thumbSize == ThumbSizeEnum.Pinterest) { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_214.png")); } } else { context.Response.ContentType = "image/png"; context.Response.WriteFile(context.Server.MapPath("~/Content/logo_64_64.png")); } }
public void ProcessRequest(HttpContext context) { string image = context.Request.QueryString["i"]; string size = context.Request.QueryString["s"]; var cache = String.Equals(context.Request.QueryString["c"], "1"); if (!string.IsNullOrEmpty(image) && image.StartsWith("http") == false) { ThumbSizeEnum thumbSize = GetThumbSize(size); string subPath; if (image.StartsWith("~/")) { subPath = image.Substring(1); } else { subPath = image; } string currentPath = "~/Thumbnail/" + thumbSize.ToString() + subPath; try { currentPath = context.Server.MapPath(currentPath); if (File.Exists(context.Server.MapPath(image))) { if (cache && File.Exists(currentPath)) { OutputCacheResponse(context, File.GetLastWriteTime(currentPath)); context.Response.ContentType = "image/jpg"; context.Response.WriteFile(currentPath); } else { var thumbnail = new Falcon.Services.Thumbnails.Thumbnail(EngineContext.Current.Resolve <IThumbnailSettingService>()); bool saveSuccess = thumbnail.CreateThumnail(thumbSize, image); // Tạo luôn tat cả kích cỡ thumbnail.CreateAllThumbnails(image); if (saveSuccess && File.Exists(currentPath)) { context.Response.ContentType = "image/jpg"; context.Response.WriteFile(currentPath); } else { ReturnDefaultImage(context); } } } else { ReturnDefaultImage(context); } } catch { ReturnDefaultImage(context); } } else { ReturnDefaultImage(context); } }
public static MvcHtmlString ImageActionLinkPromotion(this HtmlHelper htmlHelper, ThumbSizeEnum thumbSize, string imgSrc, string alt, string actionName, string controllerName, int PromotionRate, object routeValues = null, object htmlAttributes = null, object imgHtmlAttributes = null) { imgSrc = ThumbnailExtensions.Thumbnail(htmlHelper, thumbSize, imgSrc, alt, alt, imgHtmlAttributes).ToString(); UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; string url = urlHelper.Action(actionName, controllerName, routeValues); TagBuilder tagDivRoot = new TagBuilder("div"); tagDivRoot.MergeAttribute("class", "image_sale"); TagBuilder imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); //imglink.InnerHtml = imgSrc.ToString(); TagBuilder tagDiv = new TagBuilder("div"); if (PromotionRate >= 40) { tagDiv.MergeAttribute("class", "sale_off_hot"); } else { tagDiv.MergeAttribute("class", "sale_off"); } tagDiv.InnerHtml = "-" + PromotionRate.ToString() + "%"; imglink.InnerHtml = imgSrc.ToString(); imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes)); tagDivRoot.InnerHtml += imglink.ToString(); tagDivRoot.InnerHtml += tagDiv.ToString(); return(MvcHtmlString.Create(tagDivRoot.ToString())); }
public ThumbnailSetting GetByThumbSize(ThumbSizeEnum thumbSize) { return(_thumbnailsettingRepository.Table.SingleOrDefault(c => c.ThumbSize == thumbSize.ToString())); }
public static MvcHtmlString ThumbnailUrl(this HtmlHelper helper, string imagePath, ThumbSizeEnum thumbSize, bool includeMediaDomain = true) { return(MvcHtmlString.Create(Util.GetThumbLink(imagePath, thumbSize, includeMediaDomain))); }
public static MvcHtmlString Thumbnail(this HtmlHelper helper, ThumbSizeEnum thumbSize, string imagePath, object htmlAttributes = null, bool includeMediaDomain = true, bool lazyload = false, bool cache = false) { return(Thumbnail(helper, thumbSize, imagePath, "", "", htmlAttributes, includeMediaDomain, lazyload, cache)); }