Coder Social home page Coder Social logo

streamich / probe-image-size Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nodeca/probe-image-size

0.0 2.0 0.0 198 KB

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

License: MIT License

JavaScript 100.00%

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, ICO.

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

Example

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

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


// By URL with options
let result = await probe('http://example.com/image.jpg', { rejectUnauthorized: false });
console.log(result);


// From the stream
let result = await probe(require('fs').createReadStream('image.jpg'));
console.log(result);


// From a Buffer (sync)
let data = require('fs').readFileSync('image.jpg');
console.log(probe.sync(data));

API

Note:

  • You can access/browserify stream.js / http.js / sync.js directly.
  • If you don't like http.js dependencies, you can create your own wrapper for stream.js.

probe(src [, options|keepOpen]) -> Promise

  • src can be of this types:
    • String - URL to fetch
    • Stream - readable stream
  • options - HTTP only. See needle documentation, and customized defaults.
  • keepOpen (Boolean) - stream only. Keep stream open after parser finishes (input stream will be closed by default)

result (Promise) 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)
  url: ...,     // HTTP only, last url for the image in chain of redirects
                // (if no redirects, same as src)
  variants: [ { width, height }, ... ] | undefined // full list of sizes for ICO
}

Returned errors can be extended with 2 fields:

  • 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.

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

Support probe-image-size

You can support this project via Tidelift subscription.

probe-image-size's People

Contributors

boromisp avatar crux153 avatar e2e8 avatar leolabs avatar 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.