image-size
Advanced tools
Comparing version 0.3.5 to 0.4.0
@@ -6,11 +6,10 @@ 'use strict'; | ||
var libpath = process.env.TEST_COV ? '../lib-cov/' : '../lib/'; | ||
var detector = require(libpath + 'detector'); | ||
var detector = require('./detector'); | ||
var handlers = {}; | ||
var types = require(libpath + 'types'); | ||
var types = require('./types'); | ||
// load all available handlers | ||
types.forEach(function (type) { | ||
handlers[type] = require(libpath + 'types/' + type); | ||
handlers[type] = require('./types/' + type); | ||
}); | ||
@@ -75,3 +74,3 @@ | ||
// Handle buffer input | ||
if (input instanceof Buffer) { | ||
if (Buffer.isBuffer(input)) { | ||
return lookup(input); | ||
@@ -106,1 +105,3 @@ } | ||
}; | ||
module.exports.types = types; |
@@ -11,2 +11,2 @@ 'use strict'; | ||
module.exports = readUInt; | ||
module.exports = readUInt; |
@@ -19,2 +19,2 @@ 'use strict'; | ||
'calculate': calculate | ||
}; | ||
}; |
@@ -8,40 +8,6 @@ 'use strict'; | ||
// TO-DO: handle all JFIFs | ||
var validJFIFMarkers = { | ||
'ffdb': '0001010101', // Samsung D807 JPEG | ||
'ffe0': '4a46494600', // Standard JPEG | ||
'ffe1': '4578696600', // Camera JPEG, with EXIF data | ||
'ffe2': '4943435f50', // Canon EOS-1D JPEG | ||
'ffe3': '', // Samsung D500 JPEG | ||
'ffe8': '5350494646', // SPIFF JPEG | ||
'ffec': '4475636b79', // Photoshop JPEG | ||
'ffed': '50686f746f', // Adobe JPEG, Photoshop CMYK buffer | ||
'ffee': '41646f6265' // Adobe JPEG, Unrecognised (Lightroom??) | ||
}; | ||
var red = ['\x1B[31m', '\x1B[39m']; | ||
function isJPG (buffer) { //, filepath | ||
var SOIMarker = buffer.toString('hex', 0, 2); | ||
var JFIFMarker = buffer.toString('hex', 2, 4); | ||
// not a valid jpeg | ||
if ('ffd8' !== SOIMarker) { | ||
return false; | ||
} | ||
// TO-DO: validate the end-bytes of a jpeg file | ||
// use filepath, get the last bytes, check for ffd9 | ||
var got = buffer.toString('hex', 6, 11); | ||
var expected = JFIFMarker && validJFIFMarkers[JFIFMarker]; | ||
if (expected === '') { | ||
console.warn( | ||
red[0] + | ||
'this looks like a unrecognised jpeg\n' + | ||
'please report the issue here\n' + | ||
red[1], | ||
'\thttps://github.com/netroy/image-size/issues/new\n' | ||
); | ||
return false; | ||
} | ||
return (got === expected) || (JFIFMarker === 'ffdb'); | ||
return ('ffd8' === SOIMarker); | ||
} | ||
@@ -48,0 +14,0 @@ |
@@ -9,57 +9,66 @@ 'use strict'; | ||
var extractorRegExps = { | ||
'root': /<svg [^>]+>/, | ||
'width': /(^|\s)width\s*=\s*"(.+?)"/i, | ||
'height': /(^|\s)height\s*=\s*"(.+?)"/i, | ||
'viewbox': /(^|\s)viewbox\s*=\s*"(.+?)"/i | ||
'root': /<svg\s[^>]+>/, | ||
'width': /\bwidth=(['"])([^%]+?)\1/, | ||
'height': /\bheight=(['"])([^%]+?)\1/, | ||
'viewbox': /\bviewBox=(['"])(.+?)\1/ | ||
}; | ||
function getRatio (viewbox) { | ||
var ratio = 1; | ||
if (viewbox && viewbox[2]) { | ||
var dim = viewbox[2].split(/\s/g); | ||
if (dim.length === 4) { | ||
dim = dim.map(function (i) { | ||
return parseInt(i, 10); | ||
}); | ||
ratio = (dim[2] - dim[0]) / (dim[3] - dim[1]); | ||
} | ||
} | ||
return ratio; | ||
function parseViewbox (viewbox) { | ||
var bounds = viewbox.split(' '); | ||
return { | ||
'width': parseInt(bounds[2], 10), | ||
'height': parseInt(bounds[3], 10) | ||
}; | ||
} | ||
function parse (buffer) { | ||
var body = buffer.toString().replace(/[\r\n\s]+/g, ' '); | ||
var section = body.match(extractorRegExps.root); | ||
var root = section && section[0]; | ||
if (root) { | ||
var width = root.match(extractorRegExps.width); | ||
var height = root.match(extractorRegExps.height); | ||
var viewbox = root.match(extractorRegExps.viewbox); | ||
var ratio = getRatio(viewbox); | ||
function parseAttributes (root) { | ||
var width = root.match(extractorRegExps.width); | ||
var height = root.match(extractorRegExps.height); | ||
var viewbox = root.match(extractorRegExps.viewbox); | ||
return { | ||
'width': width && parseInt(width[2], 10), | ||
'height': height && parseInt(height[2], 10), | ||
'viewbox': viewbox && parseViewbox(viewbox[2]) | ||
}; | ||
} | ||
function calculateByDimensions (attrs) { | ||
return { | ||
'width': attrs.width, | ||
'height': attrs.height | ||
}; | ||
} | ||
function calculateByViewbox (attrs) { | ||
var ratio = attrs.viewbox.width / attrs.viewbox.height; | ||
if (attrs.width) { | ||
return { | ||
'width': parseInt(width && width[2], 10) || 0, | ||
'height': parseInt(height && height[2], 10) || 0, | ||
'ratio': ratio | ||
'width': attrs.width, | ||
'height': Math.floor(attrs.width / ratio) | ||
}; | ||
} | ||
if (attrs.height) { | ||
return { | ||
'width': Math.floor(attrs.height * ratio), | ||
'height': attrs.height | ||
}; | ||
} | ||
return { | ||
'width': attrs.viewbox.width, | ||
'height': attrs.viewbox.height | ||
}; | ||
} | ||
function calculate (buffer) { | ||
var parsed = parse(buffer); | ||
var width = parsed.width; | ||
var height = parsed.height; | ||
var ratio = parsed.ratio; | ||
if (width && height) { | ||
return { 'width': width, 'height': height }; | ||
} else { | ||
if (width) { | ||
return { 'width': width, 'height': Math.floor(width / ratio) }; | ||
} else if (height) { | ||
return { 'width': Math.floor(height * ratio), 'height': height }; | ||
} else { | ||
throw new TypeError('invalid svg'); | ||
var root = buffer.toString('utf8').match(extractorRegExps.root); | ||
if (root) { | ||
var attrs = parseAttributes(root[0]); | ||
if (attrs.width && attrs.height) { | ||
return calculateByDimensions(attrs); | ||
} | ||
if (attrs.viewbox) { | ||
return calculateByViewbox(attrs); | ||
} | ||
} | ||
throw new TypeError('invalid svg'); | ||
} | ||
@@ -66,0 +75,0 @@ |
{ | ||
"name": "image-size", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "get dimensions of any image file", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"bin", | ||
"lib" | ||
], | ||
"engines": { | ||
@@ -10,11 +14,10 @@ "node": ">=0.10.0" | ||
"bin": { | ||
"image-size": "./bin/image-size" | ||
"image-size": "bin/image-size.js" | ||
}, | ||
"scripts": { | ||
"test": "make" | ||
"pretest": "jshint", | ||
"test": "mocha specs", | ||
"coverage": "istanbul cover _mocha specs" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:netroy/image-size.git" | ||
}, | ||
"repository": "image-size/image-size", | ||
"keywords": [ | ||
@@ -38,13 +41,11 @@ "image", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/netroy/image-size/issues" | ||
}, | ||
"devDependencies": { | ||
"escomplex-js": "~0.1.4", | ||
"expect.js": "~0.3.1", | ||
"glob": "~4.0.6", | ||
"jshint": "~2.5.6", | ||
"mocha": "~1.21.5", | ||
"sinon": "~1.10.3" | ||
"escomplex-js": "^1.2.0", | ||
"expect.js": "^0.3.1", | ||
"glob": "^6.0.1", | ||
"istanbul": "^0.4.0", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.3.4", | ||
"sinon": "^1.17.2" | ||
} | ||
} |
@@ -1,16 +0,34 @@ | ||
[![Build Status](https://travis-ci.org/netroy/image-size.png?branch=master)](https://travis-ci.org/netroy/image-size) | ||
[![NPM Downloads](http://img.shields.io/npm/dm/image-size.svg)](https://npmjs.org/package/image-size) | ||
[![Gittip](http://img.shields.io/gittip/netroy.svg)](https://www.gittip.com/netroy/) | ||
# image-size | ||
[![Coverage Status](https://coveralls.io/repos/netroy/image-size/badge.png?branch=master)](https://coveralls.io/r/netroy/image-size?branch=master) | ||
[![Technical debt analysis](https://www.sidekickjs.com/r/netroy/image-size/status_badge.svg)](https://www.sidekickjs.com/r/netroy/image-size) | ||
[![Code Climate](https://codeclimate.com/github/netroy/image-size.png)](https://codeclimate.com/github/netroy/image-size) | ||
[![Dependency Status](https://gemnasium.com/netroy/image-size.png)](https://gemnasium.com/netroy/image-size) | ||
[![NPM Version](https://img.shields.io/npm/v/image-size.svg)](https://www.npmjs.com/package/image-size) | ||
[![Build Status](https://travis-ci.org/image-size/image-size.svg?branch=master)](https://travis-ci.org/image-size/image-size) | ||
[![NPM Downloads](https://img.shields.io/npm/dm/image-size.svg)](http://npm-stat.com/charts.html?package=image-size&author=&from=&to=) | ||
[![Coverage Status](https://img.shields.io/coveralls/image-size/image-size/master.svg)](https://coveralls.io/github/image-size/image-size?branch=master) | ||
[![devDependency Status](https://david-dm.org/image-size/image-size/dev-status.svg)](https://david-dm.org/image-size/image-size#info=devDependencies) | ||
#### Instalation | ||
A [Node](https://nodejs.org/en/) module to get dimensions of any image file | ||
`npm install image-size` | ||
## Supported formats | ||
#### Usage | ||
* BMP | ||
* GIF | ||
* JPEG | ||
* PNG | ||
* PSD | ||
* TIFF | ||
* WebP | ||
* SVG | ||
### Upcoming | ||
* SWF | ||
## Programmatic Usage | ||
``` | ||
npm install image-size --save | ||
``` | ||
### Synchronous | ||
```javascript | ||
@@ -22,3 +40,4 @@ var sizeOf = require('image-size'); | ||
##### Async version | ||
### Asynchronous | ||
```javascript | ||
@@ -31,3 +50,4 @@ var sizeOf = require('image-size'); | ||
##### Using a url | ||
### Using a URL | ||
```javascript | ||
@@ -52,22 +72,18 @@ var url = require('url'); | ||
``` | ||
You can optionally check the buffer lengths & stop downloading the image after a few kilobytes. | ||
**You don't need to download the entire image** | ||
#### Supported formats | ||
* BMP | ||
* GIF | ||
* JPEG | ||
* PNG | ||
* PSD | ||
* TIFF | ||
* WebP | ||
* SVG | ||
## Command-Line Usage (CLI) | ||
##### Upcoming | ||
* SWF | ||
``` | ||
npm install image-size --global | ||
image-size image1 [image2] [image3] ... | ||
``` | ||
##### Credits | ||
## Credits | ||
not a direct port, but an attempt to have something like | ||
[dabble's imagesize](https://github.com/dabble/imagesize/blob/master/lib/image_size.rb) as a node module. | ||
##### [Contributors](Contributors.md) | ||
## [Contributors](Contributors.md) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
471
87
6
17602
7
16
1
1