我在集成模式下在 iis7 上运行 ImageResizer。我只是想确保我不会在 Application_Start 中使用此代码引入不必要的开销。当请求不是来自我的域(例如热链接文件或 Googlebot 或 Pinterest 等)时,此处的目的是为某些图像加水印(基于文件夹,然后基于大小):
Config.Current.Pipeline.Rewrite += delegate(IHttpModule mysender, HttpContext context, IUrlEventArgs ev)
{
if (context.Request.UrlReferrer.Host != "www.mydomain.com")
{
//Check folder
string folder1 = VirtualPathUtility.ToAbsolute("~/images/products");
string folder2 = VirtualPathUtility.ToAbsolute("~/images/product-showcase");
string folder3 = VirtualPathUtility.ToAbsolute("~/images/frills");
if (ev.VirtualPath.StartsWith(folder1, StringComparison.OrdinalIgnoreCase) || ev.VirtualPath.StartsWith(folder2, StringComparison.OrdinalIgnoreCase) || ev.VirtualPath.StartsWith(folder3, StringComparison.OrdinalIgnoreCase))
{
//Estimate final image size, based on the original image being 300x300.
System.Drawing.Size estimatedSize = ImageBuilder.Current.GetFinalSize(new System.Drawing.Size(300, 300),
new ResizeSettings(ev.QueryString));
if (estimatedSize.Width > 100 || estimatedSize.Height > 100)
{
//It's over 100px, apply watermark
ev.QueryString["watermark"] = "watermarkname";
}
}
}
};
编辑/解决方案:对于工作代码,第三行应该是:
if (context.Request.UrlReferrer == null || (context.Request.UrlReferrer != null && context.Request.UrlReferrer.Host != "www.mydomain.com"))
这将为 1) 直接访问或 2) 在外部站点的页面中引用的图像添加水印。阿门。
谢谢,约翰