我在我的SixLabors核心项目中使用ImageSharp和ImageSharp.Web。对于存储在磁盘上的图像的查询字符串,图像大小调整很好。示例:
/myimage.jpg?width=10&height=10&rmode=max但是,如果图像是从流提供的,ImageSharp似乎不会调整图像的大小。下面是一个示例中间件,如果它符合某些条件,我将使用它从安全文件夹交付图像:
public class ExposeSecureImageMiddleware
{
public ExposeSecureImageMiddleware(RequestDelegate next, IFolders folders)
{
Next = next;
Folders = folders;
}
public async Task Invoke(HttpContext httpContext)
{
if (meets_my_criteria)
await SendFile(httpContext);
else
await Next(httpContext);
}
async Task SendFile(HttpContext httpContext)
{
var fs = File.OpenRead("c:/path/to/secure/file.jpg");
var bytes = new byte[fs.Length];
await fs.ReadAsync(bytes, 0, bytes.Length);
httpContext.Response.Headers.ContentLength = bytes.Length;
httpContext.Response.ContentType = "image/jpeg";
await httpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
}我在我的中间件之前在ImageSharp中注册了Startup.cs,这样它就有机会拦截响应:
Startup.cs
app.UseImageSharp();
app.UseMiddleware<ExposeSecureImageMiddleware>();当路径不在磁盘上时,如何让ImageSharp根据查询字符串参数调整图像的大小?
发布于 2019-07-18 00:43:43
ImageSharp中间件只截获具有可识别命令的图像请求。
由于您已经在启动ImageSharp中间件之后注册了中间件,ImageSharp中间件在您拦截请求之前已经处理了该请求。
有两种方法可以满足你的要求:
IImageProvider,它处理图像的分辨率以限制您的标准。https://stackoverflow.com/questions/57064737
复制相似问题