How do I get the equivalent of a "new Image()" (and then myImage.src... etc), but on NodeJS?
Answer: Use the JavaScript clientWidth property You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.
The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.
To get the size of a file in Node. js, you can use the stat() method provided by the built-in fs module. This method works asynchronously and lists the statistics of a file at the given path. The size of the file is returned in bytes.
NodeJS – Resize() is an inbuilt function that is used to resize the images to the desired size. We can use resize to set the height and width using a 2-pass bilinear algorithm. It can resize an image into any size as declared by the user. We can take input from the user or resize it into fixed Width*Height size.
Using imagemagick
for this is very overkill since you only want to read the header of the file and check the dimensions. image-size
is a pure javascript implementation of said feature which is very easy to use.
https://github.com/image-size/image-size
var sizeOf = require('image-size')
sizeOf('images/funny-cats.png', function (err, dimensions) {
if (err) throw err
console.log(dimensions.width, dimensions.height)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With