<%@ WebHandler Language="C#" Class="LiveImage" %> using System; using System.Web; using System.IO; public class LiveImage : IHttpHandler { /* SYNTAX FOR USING THIS TOOL */ /* * LiveImage.ashx?img=[PathToImage]&height=[height]&width=[width][&aspect=1] * * where * * - [PathToImage] is the path to the source image, relative to the site root * - [height] is the desired height of the image * - [width] is the desired width of the image * - [&aspect=1] -- add this parameter to preserve the photos original aspect ratio, leave it out to allow it to deform */ public void ProcessRequest (HttpContext context) { string ImageLocation = context.Request["img"]; if (ImageLocation != null && ImageLocation.Length > 0) { string sImageHeight = context.Request["height"]; string sImageWidth = context.Request["width"]; string sAspect = context.Request["aspect"]; if (context.Request["preset"] != null) { switch (context.Request["preset"]) { case "large": sImageWidth = "640"; sImageHeight = "480"; sAspect = "1"; break; case "medium": sImageWidth = "360"; sImageHeight = "360"; sAspect = "1"; break; case "small": sImageWidth = "212"; sImageHeight = "212"; sAspect = "1"; break; case "icon": sImageWidth = "30"; sImageHeight = "30"; sAspect = "1"; break; default: sImageWidth = "300"; sImageHeight = "300"; sAspect = "1"; break; } } int ImageHeight = 0; if (sImageHeight != null) Int32.TryParse(sImageHeight, out ImageHeight); int ImageWidth = 0; if (sImageWidth != null) Int32.TryParse(sImageWidth, out ImageWidth); bool Aspect = (sAspect != null); if (!Aspect && (ImageHeight == 0 || ImageWidth == 0)) throw new Exception("Must supply both width and height if not maintaining aspect ratio"); if (ImageHeight != 0 || ImageWidth != 0) { // come up with the file's cached location int FileExtensionIndex = ImageLocation.LastIndexOf('.'); if (FileExtensionIndex > -1) { string FileExtension = ImageLocation.Substring(FileExtensionIndex); string FileName = ImageLocation.Substring(0, FileExtensionIndex); string CachedFilename = FileName.Replace("/", "-").Replace("~", ""); string CacheDirectory = context.Server.MapPath("~/ImageCache/"); string CacheFile = CachedFilename + "H" + (sImageHeight == null ? "" : sImageHeight) + "W" + (sImageWidth == null ? "" : sImageWidth) + "A" + (Aspect ? "1" : "0") + FileExtension; if (File.Exists(CacheDirectory + CacheFile)) ImageLocation = "~/ImageCache/" + CacheFile; // found a cached image else { // time to create the new image // ensure the target directory exists if (!Directory.Exists(CacheDirectory)) Directory.CreateDirectory(CacheDirectory); // get the physical location of the raw file string PhysicalLocation = context.Server.MapPath(ImageLocation); // ensure that it exists if (File.Exists(PhysicalLocation)) { System.Drawing.Image img = Graphix.GetSizedImage(PhysicalLocation, ImageWidth, ImageHeight, Aspect); img.Save(CacheDirectory + CacheFile); ImageLocation = "~/ImageCache/" + CacheFile; // found a cached image } } } } context.Response.Redirect(ImageLocation); } } public bool IsReusable { get { return false; } } }