image-size
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -33,2 +33,8 @@ function isBMP (buffer) { | ||
function isWebP (buffer) { | ||
return ('RIFF' === buffer.toString('ascii', 0, 4) && | ||
'WEBP' === buffer.toString('ascii', 8, 12) && | ||
'VP8' === buffer.toString('ascii', 12, 15)); | ||
} | ||
var typeMap = { | ||
@@ -40,3 +46,4 @@ 'bmp': isBMP, | ||
'psd': isPSD, | ||
'tiff': isTIFF | ||
'tiff': isTIFF, | ||
'webp': isWebP | ||
}; | ||
@@ -43,0 +50,0 @@ |
@@ -8,27 +8,55 @@ var fs = require('fs'); | ||
var handlers = {}; | ||
['png', 'gif', 'bmp', 'psd', 'jpg'].forEach(function (type) { | ||
['png', 'gif', 'bmp', 'psd', 'jpg', 'webp'].forEach(function (type) { | ||
handlers[type] = require(libpath + 'types/' + type); | ||
}); | ||
module.exports = function (filepath) { | ||
function lookup (buffer) { | ||
// detect the file type.. don't rely on the extension | ||
var type = detector(buffer); | ||
if (type in handlers) { | ||
return handlers[type](buffer); | ||
} else { | ||
throw new TypeError('unsupported file type'); | ||
} | ||
} | ||
/** | ||
* @params filepath - relative/absolute path of the image file | ||
* @params callback - optional function for async detection | ||
*/ | ||
module.exports = function (filepath, callback) { | ||
// resolve the file path | ||
filepath = path.resolve(filepath); | ||
// get the file descriptor | ||
var descriptor = fs.openSync(filepath, 'r'); | ||
// create a buffer to read data in | ||
var buffer = new Buffer(1024); | ||
// read first 1KB from the file | ||
fs.readSync(descriptor, buffer, 0, 1024, 0); | ||
if (typeof callback === 'function') { | ||
// read first 1KB from the file, asynchronously | ||
fs.open(filepath, 'r', function (err, descriptor) { | ||
var type = detector(buffer); | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (type in handlers) { | ||
return handlers[type](buffer); | ||
fs.read(descriptor, buffer, 0, 1024, 0, function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// return the dimensions | ||
var dimensions = lookup(buffer); | ||
callback(null, dimensions); | ||
}); | ||
}); | ||
} else { | ||
throw new TypeError('unsupported file type'); | ||
// read first 1KB from the file, synchronously | ||
var descriptor = fs.openSync(filepath, 'r'); | ||
fs.readSync(descriptor, buffer, 0, 1024, 0); | ||
// return the dimensions | ||
return lookup(buffer); | ||
} | ||
}; |
{ | ||
"name": "image-size", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "get dimensions of any image file", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
[![NPM version](https://badge.fury.io/js/image-size.png)](https://npmjs.org/package/image-size) [![Build Status](https://travis-ci.org/netroy/image-size.png?branch=master)](https://travis-ci.org/netroy/image-size) | ||
[![Endorse](https://api.coderwall.com/netroy/endorsecount.png)](https://coderwall.com/netroy) | ||
@@ -11,6 +12,14 @@ #### Instalation | ||
var sizeOf = require('image-size'); | ||
var dimesions = sizeOf('images/funny-cats.png'); | ||
console.log(dimesions.width, dimesions.height); | ||
var dimensions = sizeOf('images/funny-cats.png'); | ||
console.log(dimensions.width, dimensions.height); | ||
``` | ||
##### Async version | ||
```javascript | ||
var sizeOf = require('image-size'); | ||
sizeOf('images/funny-cats.png', function (err, dimensions) { | ||
console.log(dimensions.width, dimensions.height); | ||
}); | ||
``` | ||
#### Supported formats | ||
@@ -17,0 +26,0 @@ * BMP |
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
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
7148
15
148
38