image-size
Advanced tools
Comparing version 0.1.10 to 0.1.15
@@ -1,81 +0,11 @@ | ||
function isBMP (buffer) { | ||
return ('BM' === buffer.toString('ascii', 0, 2)); | ||
} | ||
var typeMap = {}; | ||
var types = require('./types'); | ||
function isPSD (buffer) { | ||
return ('8BPS' === buffer.toString('ascii', 0, 4)); | ||
} | ||
// load all available handlers | ||
types.forEach(function (type) { | ||
typeMap[type] = require('./types/' + type).detect; | ||
}); | ||
var gifRegexp = /^GIF8[7,9]a/; | ||
function isGIF (buffer) { | ||
var signature = buffer.toString('ascii', 0, 6); | ||
return (gifRegexp.test(signature)); | ||
} | ||
module.exports = function (buffer, filepath) { | ||
var pngSignature = 'PNG\r\n\u001a\n'; | ||
function isPNG (buffer) { | ||
if (pngSignature === buffer.toString('ascii', 1, 8)) { | ||
if ('IHDR' !== buffer.toString('ascii', 12, 16)) { | ||
throw new TypeError('invalid png'); | ||
} | ||
return true; | ||
} | ||
} | ||
function isTIFF (buffer) { | ||
var hex4 = buffer.toString('hex', 0, 4); | ||
return ('49492a00' === hex4 || '4d4d002a' === hex4); | ||
} | ||
// TODO: handle the following as well | ||
// ffe2 — Canon EOS-1D JPEG | ||
// ffdb - Samsung D807 JPEG | ||
// ffe3 — Samsung D500 JPEG | ||
var validJFIFMarkers = { | ||
'ffe0': '4a46494600', // standard jpeg | ||
'ffe1': '4578696600', // camera jpeg, with EXIF data | ||
'ffe8': '5350494646' // SPIFF jpeg | ||
}; | ||
function isJPG (buffer) { | ||
var SOIMarker = buffer.toString('hex', 0, 2); | ||
var JFIFMarker = buffer.toString('hex', 2, 4); | ||
// not a valid jpeg | ||
if ('ffd8' !== SOIMarker) { | ||
return false; | ||
} | ||
var actual, expected; | ||
for (var marker in validJFIFMarkers) { | ||
expected = validJFIFMarkers[marker]; | ||
actual = buffer.toString('hex', 6, 6 + (expected.length / 2)); | ||
if (marker === JFIFMarker) { | ||
return validJFIFMarkers[marker] === actual; | ||
} | ||
} | ||
// not a valid jpeg | ||
return false; | ||
} | ||
function isWebP (buffer) { | ||
return ('RIFF' === buffer.toString('ascii', 0, 4) && | ||
'WEBP' === buffer.toString('ascii', 8, 12) && | ||
'VP8' === buffer.toString('ascii', 12, 15)); | ||
} | ||
var typeMap = { | ||
'bmp': isBMP, | ||
'gif': isGIF, | ||
'jpg': isJPG, | ||
'png': isPNG, | ||
'psd': isPSD, | ||
'tiff': isTIFF, | ||
'webp': isWebP | ||
}; | ||
module.exports = function (buffer) { | ||
var type, result; | ||
@@ -85,3 +15,3 @@ buffer = buffer.slice(0, 16); | ||
for (type in typeMap) { | ||
result = typeMap[type](buffer); | ||
result = typeMap[type](buffer, filepath); | ||
if (result) { | ||
@@ -88,0 +18,0 @@ return type; |
@@ -8,4 +8,7 @@ var fs = require('fs'); | ||
var handlers = {}; | ||
var types = ['png', 'gif', 'bmp', 'psd', 'jpg']; | ||
var types = require(libpath + 'types'); | ||
// patches | ||
require(libpath + 'patches'); | ||
// load all available handlers | ||
@@ -16,16 +19,20 @@ types.forEach(function (type) { | ||
/// Maximum buffer size, with a default of 32 kilobytes. | ||
var bufferSize = 32*1024; | ||
// Maximum buffer size, with a default of 128 kilobytes. | ||
// TODO: make this adaptive based on the initial signature of the image | ||
var bufferSize = 128*1024; | ||
function lookup (buffer) { | ||
function lookup (buffer, filepath) { | ||
// detect the file type.. don't rely on the extension | ||
var type = detector(buffer); | ||
var type = detector(buffer, filepath); | ||
// find an appropriate handler for this file type | ||
if (type in handlers) { | ||
return handlers[type](buffer); | ||
var size = handlers[type].calculate(buffer, filepath); | ||
if (size !== false) { | ||
return size; | ||
} | ||
} | ||
// throw up, if we can't understand the file | ||
else { | ||
throw new TypeError('unsupported file type'); | ||
} | ||
throw new TypeError('unsupported file type'); | ||
} | ||
@@ -77,3 +84,3 @@ | ||
// return the dimensions | ||
callback(null, lookup(buffer)); | ||
callback(null, lookup(buffer, filepath)); | ||
}); | ||
@@ -83,7 +90,4 @@ } else { | ||
// return the dimensions | ||
return lookup(buffer); | ||
return lookup(buffer, filepath); | ||
} | ||
}; | ||
// Export the lookup method for benchmarking | ||
module.exports.lookup = lookup; |
@@ -1,2 +0,6 @@ | ||
module.exports = function (buffer) { | ||
function isBMP (buffer) { | ||
return ('BM' === buffer.toString('ascii', 0, 2)); | ||
} | ||
function calculate (buffer) { | ||
return { | ||
@@ -6,2 +10,7 @@ 'width': buffer.readUInt32LE(18), | ||
}; | ||
} | ||
module.exports = { | ||
'detect': isBMP, | ||
'calculate': calculate | ||
}; |
@@ -1,2 +0,8 @@ | ||
module.exports = function (buffer) { | ||
var gifRegexp = /^GIF8[7,9]a/; | ||
function isGIF (buffer) { | ||
var signature = buffer.toString('ascii', 0, 6); | ||
return (gifRegexp.test(signature)); | ||
} | ||
function calculate(buffer) { | ||
return { | ||
@@ -6,2 +12,7 @@ 'width': buffer.readUInt16LE(6), | ||
}; | ||
}; | ||
} | ||
module.exports = { | ||
'detect': isGIF, | ||
'calculate': calculate | ||
}; |
@@ -6,2 +6,29 @@ // NOTE: we only support baseline and progressive JPGs here | ||
// TODO: handle the following as well | ||
// ffe2 - Canon EOS-1D JPEG | ||
// ffe3 - Samsung D500 JPEG | ||
var validJFIFMarkers = { | ||
'ffe0': '4a46494600', // standard jpeg | ||
'ffe1': '4578696600', // camera jpeg, with EXIF data | ||
'ffe8': '5350494646', // SPIFF jpeg | ||
'ffdb': '0001010101', // Samsung D807 JPEG | ||
'ffec': '4475636b79' // Photoshop JPEG | ||
}; | ||
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; | ||
} | ||
// TODO: validate the end-bytes of a jpeg file | ||
// use filepath, get the last bytes, check for ffd9 | ||
var expected = JFIFMarker && validJFIFMarkers[JFIFMarker]; | ||
return buffer.toString('hex', 6, 11) === expected; | ||
} | ||
function extractSize (buffer, i) { | ||
@@ -25,3 +52,3 @@ return { | ||
module.exports = function (buffer) { | ||
function calculate (buffer) { | ||
@@ -51,2 +78,7 @@ // Skip 5 chars, they are for signature | ||
throw new TypeError('Invalid JPG, no size found'); | ||
} | ||
module.exports = { | ||
'detect': isJPG, | ||
'calculate': calculate | ||
}; |
@@ -1,2 +0,12 @@ | ||
module.exports = function (buffer) { | ||
var pngSignature = 'PNG\r\n\x1a\n'; | ||
function isPNG (buffer) { | ||
if (pngSignature === buffer.toString('ascii', 1, 8)) { | ||
if ('IHDR' !== buffer.toString('ascii', 12, 16)) { | ||
throw new TypeError('invalid png'); | ||
} | ||
return true; | ||
} | ||
} | ||
function calculate (buffer) { | ||
return { | ||
@@ -6,2 +16,7 @@ 'width': buffer.readUInt32BE(16), | ||
}; | ||
} | ||
module.exports = { | ||
'detect': isPNG, | ||
'calculate': calculate | ||
}; |
@@ -1,2 +0,6 @@ | ||
module.exports = function (buffer) { | ||
function isPSD (buffer) { | ||
return ('8BPS' === buffer.toString('ascii', 0, 4)); | ||
} | ||
function calculate (buffer) { | ||
return { | ||
@@ -6,2 +10,7 @@ 'width': buffer.readUInt32BE(18), | ||
}; | ||
} | ||
module.exports = { | ||
'detect': isPSD, | ||
'calculate': calculate | ||
}; |
@@ -1,10 +0,38 @@ | ||
module.exports = function (buffer) { | ||
var lossless = ('L' === buffer.toString('ascii', 15, 16)); | ||
// based on https://developers.google.com/speed/webp/docs/riff_container | ||
function isWebP (buffer) { | ||
var riffHeader = 'RIFF' === buffer.toString('ascii', 0, 4); | ||
var webpHeader = 'WEBP' === buffer.toString('ascii', 8, 12); | ||
var vp8Header = 'VP8' === buffer.toString('ascii', 12, 15); | ||
// console.log(buffer.readUInt32LE(4)); | ||
return (riffHeader && webpHeader && vp8Header); | ||
} | ||
function calculate (buffer) { | ||
var chunkHeader = buffer.toString('ascii', 12, 16); | ||
var lossless = ('L' === chunkHeader[3]); | ||
if (lossless) { | ||
// var chunkSize = buffer.readUInt32LE(16); | ||
var signature = buffer.toString('hex', 20, 21); | ||
if (signature === '2f') { | ||
// read 14 bits of width & 14 bits of height | ||
var bits = buffer.slice(21, 25).toJSON(); | ||
bits = bits.map(function (dec) { | ||
// console.log(dec.toString(2)); | ||
return dec.toString(2); | ||
}).join(''); | ||
// console.log(bits); | ||
// var width = (bits[0] << 6) + (bits[1] >> 2); | ||
// var height = bits[] | ||
// console.log(bits, width, (bits[0] << 6), (bits[1] >> 2)); | ||
} | ||
} else { | ||
// TODO: implementation for lossy webp | ||
} | ||
return false; | ||
} | ||
return {}; | ||
}; | ||
module.exports = { | ||
'detect': isWebP, | ||
'calculate': calculate | ||
}; |
{ | ||
"name": "image-size", | ||
"version": "0.1.10", | ||
"version": "0.1.15", | ||
"description": "get dimensions of any image file", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,9 +0,5 @@ | ||
[![Build Status](https://travis-ci.org/netroy/image-size.png?branch=master)](https://travis-ci.org/netroy/image-size) | ||
[![Dependency Status](https://gemnasium.com/netroy/image-size.png)](https://gemnasium.com/netroy/image-size) | ||
[![Build Status](https://travis-ci.org/netroy/image-size.png?branch=master)](https://travis-ci.org/netroy/image-size) | ||
[![Technical debt analysis](https://www.sidekickjs.com/r/netroy/image-size/status_badge.svg)](https://www.sidekickjs.com/r/netroy/image-size) | ||
[![NPM version](https://badge.fury.io/js/image-size.png)](https://npmjs.org/package/image-size) | ||
[![Endorse](https://api.coderwall.com/netroy/endorsecount.png)](https://coderwall.com/netroy) | ||
#### Instalation | ||
@@ -35,6 +31,8 @@ | ||
* PSD | ||
* TIFF | ||
##### Upcoming | ||
* TIFF | ||
* WebP | ||
* SVG | ||
* SWF | ||
@@ -41,0 +39,0 @@ ##### Credits |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
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
15841
20
356
43
8
1