Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Demis authored and Demis committed Apr 15, 2013
2 parents 3746ac8 + e298a7f commit a58821b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions ImageResizer/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -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<string> ImageSizes = new[] { "320x480", "640x960", "640x1136", "768x1024", "1536x2048" }.ToList();

public object Get(Images request)
{
return Directory.GetFiles(UploadsDir).SafeConvertAll(x => x.SplitOnLast(Path.DirectorySeparatorChar).Last());
Expand Down Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ImageResizer/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1>Image Resizer</h1>
for (var size in resolutions) {
var wh = size.split('x');
html.push("<h3>" + size + " - " + resolutions[size] + "</h3>");
html.push("<img src='resize/" + id + "?size=" + size + "' width='" + wh[0] + "' height='" + wh[1] + "' />");
html.push("<img src='uploads/" + size + "/" + id + ".png' width='" + wh[0] + "' height='" + wh[1] + "' />");
}
html.push("<h2>Original Size</h2>");
html.push("<img src='uploads/" + id + ".png' />");
Expand Down

0 comments on commit a58821b

Please sign in to comment.