├── .eslintignore ├── .gitignore ├── .eslintrc.json ├── package.json ├── index.js ├── CHANGELOG.md └── README.md /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true, 4 | "es6": true 5 | }, 6 | "extends": ["savelist"], 7 | "rules": { 8 | "no-underscore-dangle": ["off"] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "request-image-size", 3 | "version": "2.2.0", 4 | "description": "Detect image dimensions via request.", 5 | "author": "Rodrigo Fernández Romero", 6 | "license": "MIT", 7 | "keywords": [ 8 | "image", 9 | "size", 10 | "dimension", 11 | "resolution", 12 | "width", 13 | "height", 14 | "request", 15 | "http", 16 | "https", 17 | "promise", 18 | "detect", 19 | "probe", 20 | "png", 21 | "jpeg", 22 | "bmp", 23 | "gif", 24 | "psd", 25 | "tiff", 26 | "webp", 27 | "svg", 28 | "ico", 29 | "cur" 30 | ], 31 | "homepage": "https://github.com/FdezRomero/request-image-size", 32 | "repository": { 33 | "type": "git", 34 | "url": "https://github.com/FdezRomero/request-image-size.git" 35 | }, 36 | "bugs": { 37 | "url": "https://github.com/FdezRomero/request-image-size/issues" 38 | }, 39 | "main": "index.js", 40 | "scripts": { 41 | "lint": "eslint ./" 42 | }, 43 | "dependencies": { 44 | "image-size": "^0.9.3", 45 | "request": "^2.88.2", 46 | "standard-http-error": "^2.0.1" 47 | }, 48 | "devDependencies": { 49 | "eslint-config-savelist": "^1.2.3" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * request-image-size: Detect image dimensions via request. 3 | * Licensed under the MIT license. 4 | * 5 | * https://github.com/FdezRomero/request-image-size 6 | * © 2017 Rodrigo Fernández Romero 7 | * 8 | * Based on the work of Johannes J. Schmidt 9 | * https://github.com/jo/http-image-size 10 | */ 11 | 12 | const request = require('request'); 13 | const imageSize = require('image-size'); 14 | const HttpError = require('standard-http-error'); 15 | 16 | module.exports = function requestImageSize(options) { 17 | let opts = { 18 | encoding: null 19 | }; 20 | 21 | if (options && typeof options === 'object') { 22 | opts = Object.assign(options, opts); 23 | } else if (options && typeof options === 'string') { 24 | opts = Object.assign({ uri: options }, opts); 25 | } else { 26 | return Promise.reject( 27 | new Error( 28 | 'You should provide an URI string or a "request" options object.' 29 | ) 30 | ); 31 | } 32 | 33 | opts.encoding = null; 34 | 35 | return new Promise((resolve, reject) => { 36 | const req = request(opts); 37 | 38 | req.on('response', res => { 39 | if (res.statusCode >= 400) { 40 | return reject(new HttpError(res.statusCode, res.statusMessage)); 41 | } 42 | 43 | let buffer = Buffer.from([]); 44 | let size; 45 | let imageSizeError; 46 | 47 | res.on('data', chunk => { 48 | buffer = Buffer.concat([buffer, chunk]); 49 | 50 | try { 51 | size = imageSize(buffer); 52 | } catch (err) { 53 | imageSizeError = err; 54 | return req.abort(); 55 | } 56 | 57 | if (size) { 58 | return req.abort(); 59 | } 60 | }); 61 | 62 | res.on('error', err => reject(err)); 63 | 64 | res.on('end', () => { 65 | if (!size) { 66 | return reject(imageSizeError); 67 | } 68 | 69 | size.downloaded = buffer.length; 70 | return resolve(size); 71 | }); 72 | }); 73 | 74 | req.on('error', err => reject(err)); 75 | }); 76 | }; 77 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | # 2.1.0 (2017-10-05) 3 | 4 | - Minor change: Better error handling by returning an error of type `HttpError` if the response status code is 4xx/5xx, instead of the generic `TypeError: unsupported file type: undefined (file: undefined)` passed by `image-size`. Fixes #8, thanks to @Arturszott and @dustingraves for their contribution. 5 | 6 | 7 | # 2.0.0 (2017-08-10) 8 | 9 | - Breaking change: `request-image-size` now returns an ES6 native promise instead of using a callback. Please read about the new API in the [README](README.md). Requires Node.js v4+. 10 | - Updated `image-size` to 0.6.1, adds support for ICO/CUR. 11 | 12 | 13 | # 1.3.0 (2017-04-20) 14 | 15 | This is the last version using a callback instead of returning a promise. 16 | 17 | ## Basic usage 18 | 19 | ```js 20 | var requestImageSize = require('request-image-size'); 21 | 22 | requestImageSize('http://nodejs.org/images/logo.png', function(err, size, downloaded) { 23 | 24 | if (err) { 25 | return console.error('An error has ocurred:', error); 26 | } 27 | 28 | if (!size) { 29 | return console.error('Could not get image size'); 30 | } 31 | 32 | console.log('Image is %dpx x %dpx, downloaded %d bytes', size.width, size.height, downloaded); 33 | 34 | }); 35 | ``` 36 | 37 | ## Advanced usage 38 | 39 | Specifying a request `options` object ([docs](https://github.com/mikeal/request#requestoptions-callback)): 40 | 41 | ```js 42 | var requestImageSize = require('request-image-size'); 43 | 44 | var options = { 45 | url: 'http://nodejs.org/images/logo.png', 46 | headers: { 47 | 'User-Agent': 'request-image-size' 48 | } 49 | }; 50 | 51 | requestImageSize(options, function(err, size, length) { 52 | console.log(err, size, length); 53 | }); 54 | ``` 55 | 56 | The callback receives three arguments: `err`, `size`, `downloaded`: 57 | 58 | - `err` returns an `Error` object if anything goes wrong. 59 | - `size` is in the form `{ width: 245, height: 66, type: 'png' }`. 60 | - `downloaded` is the number of bytes downloaded before being able to extract the image size. 61 | 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # request-image-size 2 | 3 | [![NPM](https://nodei.co/npm/request-image-size.png)](https://nodei.co/npm/request-image-size/) 4 | 5 | Detects image dimensions via [request](https://github.com/request/request) instead of Node.js native `http`/`https`, allowing for options and following redirects by default. It reduces network traffic by aborting requests as soon as [image-size](https://github.com/image-size/image-size) is able to obtain the image size. 6 | 7 | Since version 2.0.0 it returns an ES6 native `Promise` that resolves with the `size` object or rejects with an `Error`. Requires Node.js v4+. 8 | 9 | If you prefer using a callback, please use version 1.3.0 instead ([docs](CHANGELOG.md)) 10 | 11 | Supports all the image formats supported by [image-size](https://github.com/image-size/image-size): 12 | - BMP 13 | - CUR 14 | - GIF 15 | - ICO 16 | - JPEG 17 | - PNG 18 | - PSD 19 | - TIFF 20 | - WebP 21 | - SVG 22 | - DDS 23 | 24 | ### Dependencies 25 | - [request](https://github.com/request/request) 26 | - [image-size](https://github.com/image-size/image-size) 27 | - [standard-http-error](https://github.com/moll/js-standard-http-error) 28 | 29 | ## Basic usage 30 | 31 | ```js 32 | const requestImageSize = require('request-image-size'); 33 | 34 | requestImageSize('http://nodejs.org/images/logo.png') 35 | .then(size => console.log(size)) 36 | .catch(err => console.error(err)); 37 | ``` 38 | 39 | Result: 40 | ```js 41 | { width: 245, height: 66, type: 'png', downloaded: 856 } 42 | ``` 43 | 44 | 45 | ## Advanced usage 46 | 47 | Specifying a request `options` object ([docs](https://github.com/request/request/#requestoptions-callback)): 48 | 49 | ```js 50 | const requestImageSize = require('request-image-size'); 51 | 52 | const options = { 53 | url: 'http://nodejs.org/images/logo.png', 54 | headers: { 55 | 'User-Agent': 'request-image-size' 56 | } 57 | }; 58 | 59 | requestImageSize(options) 60 | .then(size => console.log(size)) 61 | .catch(err => console.error(err)); 62 | ``` 63 | 64 | ## License 65 | 66 | Copyright (c) 2017 Rodrigo Fernández Romero 67 | 68 | Licensed under the MIT license. 69 | 70 | Based on [http-image-size](https://github.com/jo/http-image-size) from Johannes J. Schmidt. 71 | --------------------------------------------------------------------------------