简体   繁体   English

获取上传图片的纵横比(宽度和高度)

[英]Get aspect ratio (width and height) of an uploaded image

I want to validate my image upload in my API.我想在我的 API 中验证我上传的图片。 I want only allow photos which are in landscape mode.我只想允许处于横向模式的照片。 I would also like to check the aspect ratio.我还想检查纵横比。 This is my code for checking if the iFormFile is an image:这是我检查 iFormFile 是否为图像的代码:

    [HttpPost]
    public JsonResult Post(IFormFile file)
    {
        if (file.ContentType.ToLower() != "image/jpeg" &&
            file.ContentType.ToLower() != "image/jpg" &&
            file.ContentType.ToLower() != "image/png")
        {
            // not a .jpg or .png file
            return new JsonResult(new
            {
                success = false
            });
        }

        // todo: check aspect ratio for landscape mode

        return new JsonResult(new
        {
            success = true
        });
    }

Since System.Drawing.Image is not available anymore, I couldn't find a way to convert the iFormFile into an Image typed object, to check the width and height to calculate the aspect ratio of it.由于System.Drawing.Image不再可用,我找不到将 iFormFile 转换为 Image 类型对象的方法,以检查宽度和高度以计算其纵横比。 How can I get the width and height of an image with the type iFormFile in ASP.NET Core API 2.0?如何在 ASP.NET Core API 2.0 中获取类型为 iFormFile 的图像的宽度和高度?

Since System.Drawing.Image is not available anymore, I couldn't find a way to convert the iFormFile into an Image typed object, to check the width and height to calculate the aspect ratio of it.由于System.Drawing.Image不再可用,我找不到将 iFormFile 转换为 Image 类型对象的方法,以检查宽度和高度以计算其纵横比。

That's actually not correct.这实际上是不正确的。 Microsoft has released System.Drawing.Common as a NuGet , which provides cross-platform GDI+ graphics functionality. Microsoft 已将System.Drawing.Common作为NuGet发布,它提供跨平台 GDI+ 图形功能。 The API should be an in-place replacement for any old System.Drawing code: API 应该是任何旧System.Drawing代码的就地替代品:

using (var image = Image.FromStream(file.OpenReadStream()))
{
    // use image.Width and image.Height
}

I had similar issue in .net core and I solved it by a client side solution .我在 .net core 中遇到了类似的问题,我通过客户端解决方案解决了它。 Based on your project conditions, You may trust in client side processing and let them to do some jobs.根据您的项目情况,您可以信任客户端处理并让他们做一些工作。 You can get the aspect ratio with some lines of js code which is supported on all browsers and then passing those ( width & height ) to API as well:您可以使用所有浏览器支持的一些 js 代码行获取纵横比,然后将这些( widthheight )也传递给 API:

var file = e.target.files[0];
if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
    var reader = new FileReader();
    reader.addEventListener("load", function () {
        var image = new Image();
        image.src = this.result as string;
        image.addEventListener('load', function () {
            console.log(`height: ${this.height}, width: ${this.width}`);
        });
                
    }, false);
            
    reader.readAsDataURL(file);
}

And API will look like: API 将如下所示:

//POST : api/samples?width=100&height=500
[HttpPost]
public JsonResult Post(IFormFile file, int width, int height) {}

I've tested this solution for multiple file upload and works fine.我已经针对多文件上传测试了这个解决方案并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM