Socket
Socket
Sign inDemoInstall

image-size

Package Overview
Dependencies
0
Maintainers
3
Versions
65
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    image-size

get dimensions of any image file


Version published
Weekly downloads
8.1M
decreased by-19.2%
Maintainers
3
Install size
18.8 kB
Created
Weekly downloads
 

Package description

What is image-size?

The image-size npm package is a module that provides a simple API to determine the dimensions of an image file. It supports a variety of image formats, including but not limited to JPEG, PNG, GIF, BMP, and WebP. The package can be used in both synchronous and asynchronous contexts and can handle local files as well as remote images via URLs.

What are image-size's main functionalities?

Synchronous image size retrieval

This feature allows you to synchronously get the dimensions of an image by providing the path to the image file.

const sizeOf = require('image-size');
const dimensions = sizeOf('path/to/image.jpg');
console.log(dimensions.width, dimensions.height);

Asynchronous image size retrieval

This feature allows you to asynchronously get the dimensions of an image by providing the path to the image file and a callback function.

const sizeOf = require('image-size');
sizeOf('path/to/image.jpg', (err, dimensions) => {
  if (err) throw err;
  console.log(dimensions.width, dimensions.height);
});

Image size retrieval from a buffer

This feature allows you to get the dimensions of an image from a buffer, which is useful when you have the image data in memory rather than stored in a file.

const sizeOf = require('image-size');
const fs = require('fs');

fs.readFile('path/to/image.jpg', (err, data) => {
  if (err) throw err;
  const dimensions = sizeOf(data);
  console.log(dimensions.width, dimensions.height);
});

Other packages similar to image-size

Readme

Source

image-size

NPM Version Build Status NPM Downloads Coverage Status devDependency Status

A Node module to get dimensions of any image file

Supported formats

  • BMP
  • GIF
  • JPEG
  • PNG
  • PSD
  • TIFF
  • WebP
  • SVG

Upcoming

  • SWF

Programmatic Usage

npm install image-size --save

Synchronous

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

Asynchronous

var sizeOf = require('image-size');
sizeOf('images/funny-cats.png', function (err, dimensions) {
  console.log(dimensions.width, dimensions.height);
});

NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.

Using a URL

var url = require('url');
var http = require('http');

var sizeOf = require('image-size');

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

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

You can optionally check the buffer lengths & stop downloading the image after a few kilobytes. You don't need to download the entire image

Command-Line Usage (CLI)

npm install image-size --global
image-size image1 [image2] [image3] ...

Credits

not a direct port, but an attempt to have something like dabble's imagesize as a node module.

Contributors

Keywords

FAQs

Last updated on 25 Dec 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc