Code Monkey home page Code Monkey logo

probe-image-size's Introduction

probe-image-size

Build Status NPM version Coverage Status

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD.

Key features:

  • small size, no heavy dependencies
  • works with remote and local data
  • effective with big images (speed/memory), download minimal data from remotes
  • easy to browserify (splitted to components)

Install

npm install probe-image-size --save

Example

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

// Get by URL
//
probe('http://example.com/image.jpg', function (err, result) {
  console.log(result); // =>
  /*
    {
      width: xx,
      height: yy,
      type: 'jpg',
      mime: 'image/jpeg',
      wUnits: 'px',
      hUnits: 'px'
    }
  */
});

// By URL with options
//
probe({ url: 'http://example.com/image.jpg', timeout: 5000 }, function (err, result) {
  console.log(result);
});

// With Promise
//
probe('http://example.com/image.jpg').then(function (result) {
  console.log(result);
});

// From the stream
//
var input = require('fs').createReadStream('image.jpg');

probe(input, function (err, result) {
  console.log(result);

  // terminate input, depends on stream type,
  // this example is for fs streams only.
  input.destroy();
});

// From a Buffer
//
var data = require('fs').readFileSync('image.jpg');

console.log(probe.sync(data));

API

probe(src [, callback(err, result)])

src can be of this types:

  • String - URL to fetch
  • Object - options for request, defaults are { timeout: 5000, maxRedirects: 2 }
  • Stream - readable stream

result contains:

{
  width: XX,
  height: YY,
  length: ZZ, // byte length of the file (if available, HTTP only)
  type: ..., // image 'type' (usual file name extention)
  mime: ...,  // mime type
  wUnits: 'px', // width units type ('px' by default, can be different for SVG)
  hUnits: 'px', // height units type ('px' by default, can be different for SVG)
}

err is an error, which is extended with:

  • code - equals to ECONTENT if the library failed to parse the file;
  • status - equals to a HTTP status code if it receives a non-200 response.

If callback not provided, Promise will be returned.

Note. If you use Stream as source, it's your responsibility to close that stream in callback. In other case you can get memory leak, because stream will be left in paused state. With http requests that's not a problem - everything is released automatically, as soon as possible.

sync.probe(src) -> result|null

Sync version can eat arrays, typed arrays and buffers. On success it returns the same result as async version. On fail it returns null.

Note. Formats like JPEG & TIFF can store size anywhere (far from the head). That usually does not happens, but if you need guarantees - always provide full file content to sync methods. We strongly recommend to use async version as memory-friendly.

Similar projects

License

MIT

probe-image-size's People

Contributors

rlidwka avatar vanderhoorn avatar winglian avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.