New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

image-size-stream

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

image-size-stream

Detect the width and height of images in a stream

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
1.1K
16.98%
Maintainers
1
Weekly downloads
 
Created
Source

image-size-stream

NPM version Build Status Build status Coverage Status Dependency Status devDependency Status

Detect the width and height of images in a stream

//       +-----------+
//       |           |
// 300px |  foo.jpg  |
//       |           |
//       +-----------+
//           400px 

var imageSizeStream = createImageSizeStream()
.on('size', function(dimensions) {
  dimensions; //=> {width: 400, height: 300, type: 'jpg'}
  stream.destroy();
});

var stream = fs.createReadStream('path/to/foo.jpg').pipe(imageSizeStream);

Installation

Use npm.

npm install --save image-size-stream

Supported image formats

API

var createImageSizeStream = require('image-size-stream');

createImageSizeStream([option])

option: Object
Return: Object (stream.Transform)

The stream tries to detect the image size and emits size or error event.

var createImageSizeStream = require('image-size-stream');
var imageSizeStream = createImageSizeStream();

option.limit

Type: Number
Default: 128 * 1024

The maximum bytes the stream can reads. It emits an error if it cannot detect the image size though it has reached the limit.

Usually the default value meets the requirements.

Events

size

This event fires when the stream detect the image size. It passes an object in the form {width: [Number], height: [Number], type: [String]} to the callback function.

type will be one of the following strings: bmp gif jpg png psd svg tiff webp

imageSizeStream.on('size', function(dimensions) {
  console.log('size: ' + dimensions.width + ' x ' + dimensions.height);
  console.log('image format: ' + dimensions.type);
});

error

This event fires when the stream failed to detect the image size. It passes an error to the callback function.

Examples

These examples show that you don't need to read the image entirely if you just want to detect its width and height.

Read data from local file system

var fs = require('fs');
var fileStream = fs.createReadStream('path/to/image.jpg');

var createImageSizeStream = require('image-size-stream');
var size = createImageSizeStream();
size
.on('size', function(dimensions) {
  console.log(dimensions);
  fileStream.destroy();
})
.on('error', function(err) {
  throw err;
});

fileStream.pipe(size);

If you want to stop reading the rest of the image file at size event, call fs.ReadStream#destroy() or fs.ReadStream#close().

Read data via HTTP

var http = require('http');

var createImageSizeStream = require('image-size-stream');
var size = createImageSizeStream();
size
.on('size', function(dimensions) {
  console.log(dimensions);
  request.abort();
})
.on('error', function(err) {
  throw err;
});

var request = http.get('url/to/image.png', function(response) {
  response.pipe(size);
});

If you want to stop loading the rest of the image file at size event, call request.abort().

License

Copyright (c) 2014 - 2015 Shinnosuke Watanabe

Licensed under the MIT License.

Keywords

image

FAQs

Package last updated on 24 Mar 2015

Did you know?

Socket

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