[6a3a178] | 1 | # image-size
|
---|
| 2 |
|
---|
| 3 | [![NPM Version](https://img.shields.io/npm/v/image-size.svg)](https://www.npmjs.com/package/image-size)
|
---|
| 4 | [![Build Status](https://travis-ci.org/image-size/image-size.svg?branch=master)](https://travis-ci.org/image-size/image-size)
|
---|
| 5 | [![NPM Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=&from=&to=)
|
---|
| 6 | [![Coverage Status](https://img.shields.io/coveralls/image-size/image-size/master.svg)](https://coveralls.io/github/image-size/image-size?branch=master)
|
---|
| 7 | [![devDependency Status](https://david-dm.org/image-size/image-size/dev-status.svg)](https://david-dm.org/image-size/image-size#info=devDependencies)
|
---|
| 8 |
|
---|
| 9 | A [Node](https://nodejs.org/en/) module to get dimensions of any image file
|
---|
| 10 |
|
---|
| 11 | ## Supported formats
|
---|
| 12 |
|
---|
| 13 | * BMP
|
---|
| 14 | * GIF
|
---|
| 15 | * JPEG
|
---|
| 16 | * PNG
|
---|
| 17 | * PSD
|
---|
| 18 | * TIFF
|
---|
| 19 | * WebP
|
---|
| 20 | * SVG
|
---|
| 21 | * DDS
|
---|
| 22 |
|
---|
| 23 | ### Upcoming
|
---|
| 24 |
|
---|
| 25 | * SWF
|
---|
| 26 |
|
---|
| 27 | ## Programmatic Usage
|
---|
| 28 |
|
---|
| 29 | ```
|
---|
| 30 | npm install image-size --save
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | ### Synchronous
|
---|
| 34 |
|
---|
| 35 | ```javascript
|
---|
| 36 | var sizeOf = require('image-size');
|
---|
| 37 | var dimensions = sizeOf('images/funny-cats.png');
|
---|
| 38 | console.log(dimensions.width, dimensions.height);
|
---|
| 39 | ```
|
---|
| 40 |
|
---|
| 41 | ### Asynchronous
|
---|
| 42 |
|
---|
| 43 | ```javascript
|
---|
| 44 | var sizeOf = require('image-size');
|
---|
| 45 | sizeOf('images/funny-cats.png', function (err, dimensions) {
|
---|
| 46 | console.log(dimensions.width, dimensions.height);
|
---|
| 47 | });
|
---|
| 48 | ```
|
---|
| 49 | NOTE: The asynchronous version doesn't work if the input is a Buffer. Use synchronous version instead.
|
---|
| 50 |
|
---|
| 51 | ### Using a URL
|
---|
| 52 |
|
---|
| 53 | ```javascript
|
---|
| 54 | var url = require('url');
|
---|
| 55 | var http = require('http');
|
---|
| 56 |
|
---|
| 57 | var sizeOf = require('image-size');
|
---|
| 58 |
|
---|
| 59 | var imgUrl = 'http://my-amazing-website.com/image.jpeg';
|
---|
| 60 | var options = url.parse(imgUrl);
|
---|
| 61 |
|
---|
| 62 | http.get(options, function (response) {
|
---|
| 63 | var chunks = [];
|
---|
| 64 | response.on('data', function (chunk) {
|
---|
| 65 | chunks.push(chunk);
|
---|
| 66 | }).on('end', function() {
|
---|
| 67 | var buffer = Buffer.concat(chunks);
|
---|
| 68 | console.log(sizeOf(buffer));
|
---|
| 69 | });
|
---|
| 70 | });
|
---|
| 71 | ```
|
---|
| 72 |
|
---|
| 73 | You can optionally check the buffer lengths & stop downloading the image after a few kilobytes.
|
---|
| 74 | **You don't need to download the entire image**
|
---|
| 75 |
|
---|
| 76 | ## Command-Line Usage (CLI)
|
---|
| 77 |
|
---|
| 78 | ```
|
---|
| 79 | npm install image-size --global
|
---|
| 80 | image-size image1 [image2] [image3] ...
|
---|
| 81 | ```
|
---|
| 82 |
|
---|
| 83 | ## Credits
|
---|
| 84 |
|
---|
| 85 | not a direct port, but an attempt to have something like
|
---|
| 86 | [dabble's imagesize](https://github.com/dabble/imagesize/blob/master/lib/image_size.rb) as a node module.
|
---|
| 87 |
|
---|
| 88 | ## [Contributors](Contributors.md)
|
---|