Opening images on NodeJS and finding out width/height
如何在NodeJS上获得等效的" new Image()"(然后是myImage.src ...等)?
为此使用
https://github.com/image-size/image-size
1 2 3 4 5 6 7 | var sizeOf = require('image-size') sizeOf('images/funny-cats.png', function (err, dimensions) { if (err) throw err console.log(dimensions.width, dimensions.height) }) |
有node-imagemagick,(显然,您需要拥有ImageMagick)。
1 2 3 4 5 6 | var im = require('imagemagick'); im.identify('kittens.jpg', function(err, features){ if (err) throw err console.log(features) // { format: 'JPEG', width: 3904, height: 2622, depth: 8 } }) |
应该有所帮助的https://github.com/nodeca/probe-image-size。 小型+同步/异步模式+网址支持。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var probe = require('probe-image-size'); // Get by URL // probe('http://example.com/image.jpg', function (err, result) { console.log(result); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg', wUnits: 'px', hUnits: 'px' } }); // From the stream // var input = require('fs').createReadStream('image.jpg'); probe(input, function (err, result) { console.log(result); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg', wUnits: 'px', hUnits: 'px' } // terminate input, depends on stream type, // this example is for fs streams only. input.destroy(); }); // From a Buffer // var data = require('fs').readFileSync('image.jpg'); console.log(probe.sync(data)); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg', wUnits: 'px', hUnits: 'px' } |
免责声明:我是这段代码的作者。