Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening images on NodeJS and finding out width/height

How do I get the equivalent of a "new Image()" (and then myImage.src... etc), but on NodeJS?

like image 539
Mamsaac Avatar asked Apr 03 '11 16:04

Mamsaac


People also ask

How do you get the image width and height using JS?

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.

How do you fix the width and height of an image?

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.

How do you find the size of a node?

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.

How do I resize an image in node JS?

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.


1 Answers

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)
})
like image 145
Linus Unnebäck Avatar answered Oct 10 '22 00:10

Linus Unnebäck