ImageSharp.Web-在Asp.Net Core 3.1中通过querystring调整图像大小 [英] ImageSharp.Web - resize image by querystring in Asp.Net Core 3.1

查看:491
本文介绍了ImageSharp.Web-在Asp.Net Core 3.1中通过querystring调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ImageSharp.Web通过查询字符串参数来调整图像大小,例如:

I am trying to use ImageSharp.Web to resize images through querystring parameters eg: http://localhost:5001/content/photo/img.jpg?width=800&height=600

我创建了一个新的MVC Asp.Net Core 3.1项目,安装了软件包,并遵循了

I created a new MVC Asp.Net Core 3.1 project, installed the packages and followed the Documentation

我尝试了所描述的最低配置和多种变体,但是中间件似乎没有拦截图像请求.我总是得到原始图像,并且不会触发任何错误.

I tried the minimum configuration described and several variations, but it seems that the middleware is not intercepting the image request. I always get the original image and no error is triggered.

我错过了什么吗? 此功能的最低可能配置是什么?

Am I missing something? What would be the minimum possible configuration for this feature?

谢谢!

Startup.cs:

        public void ConfigureServices(IServiceCollection services)
    {

        services.AddDependencyInjectionSetup();
        services.AddControllersWithViews();

        //https://docs.sixlabors.com/articles/imagesharp.web/gettingstarted.html
        services.AddImageSharp();

        services.AddImageSharpCore(
            options =>
            {
                options.MaxBrowserCacheDays = 7;
                options.MaxCacheDays = 365;
                options.CachedNameLength = 8;
                options.OnParseCommands = _ => { };
                options.OnBeforeSave = _ => { };
                options.OnProcessed = _ => { };
                options.OnPrepareResponse = _ => { };
            })
            .SetRequestParser<QueryCollectionRequestParser>()
            .SetMemoryAllocator(provider => ArrayPoolMemoryAllocator.CreateWithMinimalPooling())
            .Configure<PhysicalFileSystemCacheOptions>(options =>
            {
                options.CacheFolder = "imagesharp-cache";
            })
            .SetCache<PhysicalFileSystemCache>()
            .SetCacheHash<CacheHash>()
            .AddProvider<PhysicalFileSystemProvider>()
            .AddProcessor<ResizeWebProcessor>()
            .AddProcessor<FormatWebProcessor>();


    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();         

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

        app.UseImageSharp();


    }

推荐答案

    app.UseImageSharp();

必须在

    app.UseStaticFiles();

正在发生的是,内置的Static Files中间件甚至在击中ImageSharp之前就已启动..在startup.cs的Configure()部分中注册项目的顺序很重要,因为它们会顺序运行内.

What is happening is the built in Static Files middle-ware is kicking in before its even hitting the ImageSharp one.. the order you register items in the Configure() section of startup.cs is important as that's they order they run in.

这篇关于ImageSharp.Web-在Asp.Net Core 3.1中通过querystring调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆