diff --git a/ImageResizer/Global.asax.cs b/ImageResizer/Global.asax.cs index 5378028..633c76b 100644 --- a/ImageResizer/Global.asax.cs +++ b/ImageResizer/Global.asax.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; @@ -42,7 +43,8 @@ public class ImageService : Service const int ThumbnailSize = 100; readonly string UploadsDir = "~/uploads".MapHostAbsolutePath(); readonly string ThumbnailsDir = "~/uploads/thumbnails".MapHostAbsolutePath(); - + readonly List ImageSizes = new[] { "320x480", "640x960", "640x1136", "768x1024", "1536x2048" }.ToList(); + public object Get(Images request) { return Directory.GetFiles(UploadsDir).SafeConvertAll(x => x.SplitOnLast(Path.DirectorySeparatorChar).Last()); @@ -79,14 +81,17 @@ private void WriteImage(Stream ms) using (var img = Image.FromStream(ms)) { img.Save(UploadsDir.CombineWith(fileName)); - var stream = Resize(img, ThumbnailSize, ThumbnailSize); File.WriteAllBytes(ThumbnailsDir.CombineWith(fileName), stream.ReadFully()); + + ImageSizes.ForEach(x => File.WriteAllBytes( + AssertDir(UploadsDir.CombineWith(x)).CombineWith(hash + ".png"), + Get(new Resize { Id = hash, Size = x }).ReadFully())); } } [AddHeader(ContentType = "image/png")] - public object Get(Resize request) + public Stream Get(Resize request) { var imagePath = UploadsDir.CombineWith(request.Id + ".png"); if (request.Id == null || !File.Exists(imagePath)) @@ -95,7 +100,6 @@ public object Get(Resize request) using (var stream = File.OpenRead(imagePath)) using (var img = Image.FromStream(stream)) { - var parts = request.Size == null ? null : request.Size.Split('x'); int width = img.Width; int height = img.Height; @@ -128,7 +132,6 @@ public static Stream Resize(Image img, int newWidth, int newHeight) var ratioX = (double)newWidth / img.Width; var ratioY = (double)newHeight / img.Height; var ratio = Math.Max(ratioX, ratioY); - var width = (int)(img.Width * ratio); var height = (int)(img.Height * ratio); @@ -179,17 +182,22 @@ public static Image Crop(Image Image, int newWidth, int newHeight, int startX = public object Any(Reset request) { - if (!Directory.Exists(UploadsDir)) - Directory.CreateDirectory(UploadsDir); - if (!Directory.Exists(ThumbnailsDir)) - Directory.CreateDirectory(ThumbnailsDir); - - Directory.GetFiles(UploadsDir).ToList().ForEach(File.Delete); - Directory.GetFiles(ThumbnailsDir).ToList().ForEach(File.Delete); + Directory.GetFiles(AssertDir(UploadsDir)).ToList().ForEach(File.Delete); + Directory.GetFiles(AssertDir(ThumbnailsDir)).ToList().ForEach(File.Delete); + ImageSizes.ForEach(x => + Directory.GetFiles(AssertDir(UploadsDir.CombineWith(x))).ToList().ForEach(File.Delete)); File.ReadAllLines("~/preset-urls.txt".MapHostAbsolutePath()).ToList() .ForEach(url => WriteImage(new MemoryStream(url.Trim().GetBytesFromUrl()))); + return HttpResult.Redirect("/"); } + + private static string AssertDir(string dirPath) + { + if (!Directory.Exists(dirPath)) + Directory.CreateDirectory(dirPath); + return dirPath; + } } public class AppHost : AppHostBase diff --git a/ImageResizer/default.html b/ImageResizer/default.html index 2a9c45b..4625530 100644 --- a/ImageResizer/default.html +++ b/ImageResizer/default.html @@ -107,7 +107,7 @@

Image Resizer

for (var size in resolutions) { var wh = size.split('x'); html.push("

" + size + " - " + resolutions[size] + "

"); - html.push(""); + html.push(""); } html.push("

Original Size

"); html.push("");