1. Products
  2.   Image
  3.   JavaScript
  4.   Image-Size
 
  

Open Source JavaScript Library for Image Processing

JavaScript API for detecting image dimensions

What is Image-Size?

Image-Size is a very simple light-weight image processing library that enables software developers to get dimensions of images at run time. The API supports a wide range of image formats and you can get dimensions of most of the popular file formats using the API. The API provides a synchronous and asynchronous method for working with the images. The asynchronous functions have a default concurrency limit of 100 and in order to change this limit, you can manually change concurrency. Furthermore, the asynchronous version doesn't work if the input is a Buffer and you'll have to use the asynchronous method instead.

Previous Next

Getting Started with Image-Size

The recommended way to install Image-Size via NPM. Please use the following command to install it.

Install Image-Size via NPM

 npm install image-size --global 

Get Image Dimensions via Free JavaScript API

The open-source Image-Size library allows JavaScript developers to get the dimensions of the images programmatically. In order to get dimensions from an image, the API provides sizeOf() method. By using the following two lines of code, you can easily get the dimensions of the image.

Get Image Dimensions

  1. Load Image-Size Library
  2. Get dimensions using sizeOf() method and pass image path as string
  3. Get width of image using dimensions.width and height using dimensions.height

Get Image Dimensions via JavaScript

const sizeOf = require('image-size')
const dimensions = sizeOf('images/funny-cats.png')
console.log(dimensions.width, dimensions.height)
        

The Image-Size library has also provided functionality for getting the size of the image using a URL. It is also possible to not download the entire image and optionally stop downloading the image after a few kilobytes. It is also possible to disable certain image types. The following example shows how to access image dimension using a URL

Get Image Size via URL

const url = require('url')
const http = require('http')

const sizeOf = require('image-size')

const imgUrl = 'http://my-amazing-website.com/image.jpeg'
const options = url.parse(imgUrl)

http.get(options, function (response) {
  const chunks = []
  response.on('data', function (chunk) {
    chunks.push(chunk)
  }).on('end', function() {
    const buffer = Buffer.concat(chunks)
    console.log(sizeOf(buffer))
  })
})
 English