How to get image width/height in next.js?

Viewed 1725

In my app I allow users to upload their images. Upon uploading it goes into public folder, and in database there is only a direct link for future requests.

When displaying these images on the page I encounter a problem. Image component wants to know height and width of each image. And so I want to know it too.

What are the available options for the workflow in this situation? Both processing while uploading and while requesting will work for me.

1 Answers

You can use image-size npm module:

npm install image-size --save

then:

var sizeOf = require('image-size');
var dimensions = sizeOf('./sample.jpg');
console.log(dimensions.width, dimensions.height);

from: How to check an image dimensions using js or node

Related