Skip to content

Commit a58821b

Browse files
DemisDemis
Demis
authored and
Demis
committed
2 parents 3746ac8 + e298a7f commit a58821b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

ImageResizer/Global.asax.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Drawing.Imaging;
34
using System.IO;
45
using System.Linq;
@@ -42,7 +43,8 @@ public class ImageService : Service
4243
const int ThumbnailSize = 100;
4344
readonly string UploadsDir = "~/uploads".MapHostAbsolutePath();
4445
readonly string ThumbnailsDir = "~/uploads/thumbnails".MapHostAbsolutePath();
45-
46+
readonly List<string> ImageSizes = new[] { "320x480", "640x960", "640x1136", "768x1024", "1536x2048" }.ToList();
47+
4648
public object Get(Images request)
4749
{
4850
return Directory.GetFiles(UploadsDir).SafeConvertAll(x => x.SplitOnLast(Path.DirectorySeparatorChar).Last());
@@ -79,14 +81,17 @@ private void WriteImage(Stream ms)
7981
using (var img = Image.FromStream(ms))
8082
{
8183
img.Save(UploadsDir.CombineWith(fileName));
82-
8384
var stream = Resize(img, ThumbnailSize, ThumbnailSize);
8485
File.WriteAllBytes(ThumbnailsDir.CombineWith(fileName), stream.ReadFully());
86+
87+
ImageSizes.ForEach(x => File.WriteAllBytes(
88+
AssertDir(UploadsDir.CombineWith(x)).CombineWith(hash + ".png"),
89+
Get(new Resize { Id = hash, Size = x }).ReadFully()));
8590
}
8691
}
8792

8893
[AddHeader(ContentType = "image/png")]
89-
public object Get(Resize request)
94+
public Stream Get(Resize request)
9095
{
9196
var imagePath = UploadsDir.CombineWith(request.Id + ".png");
9297
if (request.Id == null || !File.Exists(imagePath))
@@ -95,7 +100,6 @@ public object Get(Resize request)
95100
using (var stream = File.OpenRead(imagePath))
96101
using (var img = Image.FromStream(stream))
97102
{
98-
99103
var parts = request.Size == null ? null : request.Size.Split('x');
100104
int width = img.Width;
101105
int height = img.Height;
@@ -128,7 +132,6 @@ public static Stream Resize(Image img, int newWidth, int newHeight)
128132
var ratioX = (double)newWidth / img.Width;
129133
var ratioY = (double)newHeight / img.Height;
130134
var ratio = Math.Max(ratioX, ratioY);
131-
132135
var width = (int)(img.Width * ratio);
133136
var height = (int)(img.Height * ratio);
134137

@@ -179,17 +182,22 @@ public static Image Crop(Image Image, int newWidth, int newHeight, int startX =
179182

180183
public object Any(Reset request)
181184
{
182-
if (!Directory.Exists(UploadsDir))
183-
Directory.CreateDirectory(UploadsDir);
184-
if (!Directory.Exists(ThumbnailsDir))
185-
Directory.CreateDirectory(ThumbnailsDir);
186-
187-
Directory.GetFiles(UploadsDir).ToList().ForEach(File.Delete);
188-
Directory.GetFiles(ThumbnailsDir).ToList().ForEach(File.Delete);
185+
Directory.GetFiles(AssertDir(UploadsDir)).ToList().ForEach(File.Delete);
186+
Directory.GetFiles(AssertDir(ThumbnailsDir)).ToList().ForEach(File.Delete);
187+
ImageSizes.ForEach(x =>
188+
Directory.GetFiles(AssertDir(UploadsDir.CombineWith(x))).ToList().ForEach(File.Delete));
189189
File.ReadAllLines("~/preset-urls.txt".MapHostAbsolutePath()).ToList()
190190
.ForEach(url => WriteImage(new MemoryStream(url.Trim().GetBytesFromUrl())));
191+
191192
return HttpResult.Redirect("/");
192193
}
194+
195+
private static string AssertDir(string dirPath)
196+
{
197+
if (!Directory.Exists(dirPath))
198+
Directory.CreateDirectory(dirPath);
199+
return dirPath;
200+
}
193201
}
194202

195203
public class AppHost : AppHostBase

ImageResizer/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ <h1>Image Resizer</h1>
107107
for (var size in resolutions) {
108108
var wh = size.split('x');
109109
html.push("<h3>" + size + " - " + resolutions[size] + "</h3>");
110-
html.push("<img src='resize/" + id + "?size=" + size + "' width='" + wh[0] + "' height='" + wh[1] + "' />");
110+
html.push("<img src='uploads/" + size + "/" + id + ".png' width='" + wh[0] + "' height='" + wh[1] + "' />");
111111
}
112112
html.push("<h2>Original Size</h2>");
113113
html.push("<img src='uploads/" + id + ".png' />");

0 commit comments

Comments
 (0)