Skip to content

Commit 07d0cc4

Browse files
committed
misc WebRSize improvements
1 parent 6fcba80 commit 07d0cc4

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/MagicScaler/Core/ProcessImageSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public static ProcessImageSettings FromDictionary(IDictionary<string, string> di
252252
FrameIndex = Math.Max(int.TryParse(dic.GetValueOrDefault("frame") ?? dic.GetValueOrDefault("page"), out int f) ? f : 0, 0),
253253
Width = Math.Max(int.TryParse(dic.GetValueOrDefault("width") ?? dic.GetValueOrDefault("w"), out int w) ? w : 0, 0),
254254
Height = Math.Max(int.TryParse(dic.GetValueOrDefault("height") ?? dic.GetValueOrDefault("h"), out int h) ? h : 0, 0),
255-
JpegQuality = Math.Max(int.TryParse(dic.GetValueOrDefault("quality"), out int q) ? q : 0, 0)
255+
JpegQuality = Math.Max(int.TryParse(dic.GetValueOrDefault("quality") ?? dic.GetValueOrDefault("q"), out int q) ? q : 0, 0)
256256
};
257257

258258
s.Sharpen = bool.TryParse(dic.GetValueOrDefault("sharpen"), out bool bs) ? bs : s.Sharpen;

src/WebRSize/WebRSizeHandler.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Web;
44
using System.Web.Hosting;
55
using System.Reflection;
6+
using System.Diagnostics;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using System.Collections.Concurrent;
@@ -107,22 +108,29 @@ public override async Task ProcessRequestAsync(HttpContext ctx)
107108

108109
if (tsource?.Task == task)
109110
{
110-
ctx.Trace.Write("ProcessImage Begin");
111+
ctx.Trace.Write(nameof(WebRSize), $"{nameof(MagicImageProcessor.ProcessImage)} Begin");
111112
await process(tsource, ctx.Request.Path, cachePath, s);
112-
ctx.Trace.Write("ProcessImage End");
113+
ctx.Trace.Write(nameof(WebRSize), $"{nameof(MagicImageProcessor.ProcessImage)} End");
113114
}
114115

115-
var res = await task;
116+
var img = await task;
117+
var res = ctx.Response;
116118

117-
if (!ctx.Response.IsClientConnected)
119+
if (!res.IsClientConnected)
118120
return;
119121

120-
ctx.Response.BufferOutput = false;
121-
ctx.Response.ContentType = MimeMapping.GetMimeMapping(Path.GetFileName(cachePath));
122-
ctx.Response.AddHeader("Content-Length", res.Count.ToString());
123-
ctx.Response.Cache.SetLastModifiedFromFileDependencies();
122+
try
123+
{
124+
res.BufferOutput = false;
125+
res.ContentType = MimeMapping.GetMimeMapping(Path.GetFileName(cachePath));
126+
res.AddHeader("Content-Length", img.Count.ToString());
124127

125-
await ctx.Response.OutputStream.WriteAsync(res.Array, res.Offset, res.Count);
128+
await res.OutputStream.WriteAsync(img.Array, img.Offset, img.Count);
129+
}
130+
catch (HttpException ex) when (new StackTrace(ex).GetFrame(0)?.GetMethod().Name == "RaiseCommunicationError")
131+
{
132+
// no problem here. client just disconnected before transmission completed.
133+
}
126134
}
127135

128136
public override bool IsReusable => true;

src/WebRSize/WebRSizeModule.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ private async Task mapRequest(object sender, EventArgs e)
3737
dic = folderConfig.DefaultSettings.ToDictionary().Coalesce(dic);
3838

3939
var s = ProcessImageSettings.FromDictionary(dic);
40+
41+
int rw = s.Width, rh = s.Height;
42+
if (double.TryParse(dic.GetValueOrDefault("devicepixelratio") ?? dic.GetValueOrDefault("dpr"), out double dpr))
43+
{
44+
dpr = dpr.Clamp(1d, 5d);
45+
s.Width = (int)Math.Floor(s.Width * dpr);
46+
s.Height = (int)Math.Floor(s.Height * dpr);
47+
}
48+
4049
if (exists && s.IsEmpty && !folderConfig.ForceProcessing)
4150
return;
4251

@@ -51,6 +60,7 @@ private async Task mapRequest(object sender, EventArgs e)
5160
s.Width = s.Height = 100;
5261

5362
ifi = new ImageFileInfo(s.Width > 0 ? s.Width : s.Height, s.Height > 0 ? s.Height : s.Width);
63+
s.SaveFormat = FileFormat.Png;
5464
}
5565

5666
ifi = ifi ?? await CacheHelper.GetImageInfoAsync(path);
@@ -80,8 +90,14 @@ private async Task mapRequest(object sender, EventArgs e)
8090
return;
8191
}
8292

83-
var cacheVPath = namingStrategy.Value.GetCacheFilePath(path, s);
84-
var cachePath = HostingEnvironment.MapPath(cacheVPath);
93+
if (dpr > 0d && !dic.ContainsKey("quality") && !dic.ContainsKey("q"))
94+
{
95+
dpr = Math.Max(Math.Max((double)s.Width / (rw > 0 ? rw : s.Width), (double)s.Height / (rh > 0 ? rh : s.Height)), 1d);
96+
s.JpegQuality -= (int)Math.Round((dpr - 1d) * 10);
97+
}
98+
99+
string cacheVPath = namingStrategy.Value.GetCacheFilePath(path, s);
100+
string cachePath = HostingEnvironment.MapPath(cacheVPath);
85101

86102
if (File.Exists(cachePath))
87103
{

0 commit comments

Comments
 (0)