-
Notifications
You must be signed in to change notification settings - Fork 7
/
NBrightThumb.ashx.cs
90 lines (76 loc) · 3.21 KB
/
NBrightThumb.ashx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using NBrightCore.common;
using NBrightCore.images;
namespace NBrightDNN
{
/// <summary>
/// Summary description for NBrightThumb1
/// </summary>
public class NBrightThumb : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var w = Utils.RequestQueryStringParam(context, "w");
var h = Utils.RequestQueryStringParam(context, "h");
var src = Utils.RequestQueryStringParam(context, "src");
var imgtype = Utils.RequestQueryStringParam(context, "imgtype");
if (h == "") h = "0";
if (w == "") w = "0";
if (Utils.IsNumeric(w) && Utils.IsNumeric(h))
{
src = HttpContext.Current.Server.MapPath(src);
var strCacheKey = context.Request.Url.Host.ToLower() + "*" + src + "*" + Utils.GetCurrentCulture() + "*img:" + w + "*" + h + "*";
var newImage = (Bitmap) Utils.GetCache(strCacheKey);
if (newImage == null)
{
newImage = ImgUtils.CreateThumbnail(src, Convert.ToInt32(w), Convert.ToInt32(h));
Utils.SetCache(strCacheKey, newImage);
}
if ((newImage != null))
{
context.Response.Clear();
context.Response.ClearHeaders();
ImageCodecInfo useEncoder;
// due to issues on some servers not outputing the png format correctly from the thumbnailer.
// this thumbnailer will always output jpg, unless specifically told to do a png format.
if (imgtype.ToLower() == "png")
{
useEncoder = ImgUtils.GetEncoder(ImageFormat.Png);
context.Response.ContentType = "image/png";
}
else
{
useEncoder = ImgUtils.GetEncoder(ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
}
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 85L);
try
{
context.Response.AddFileDependency(src);
context.Response.Cache.SetETagFromFileDependencies();
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
newImage.Save(context.Response.OutputStream, useEncoder, encoderParameters);
}
catch (Exception exc)
{
var outArray = Utils.StrToByteArray(exc.ToString());
context.Response.OutputStream.Write(outArray, 0, outArray.Count());
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}