node.js - node如何获取远程图片的尺寸?
怪我咯
怪我咯 2017-04-17 12:04:30
0
1
746

图片在阿里云,想通过Node获取图片的尺寸,供iOS客户端展示,怎样做到不把图片下载下来的情况下,获取图片的尺寸或比例?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
巴扎黑

Found the solution.

var url = require('url');
var http = require('http');
 
var sizeOf = require('image-size');
 
var imgUrl = 'http://my-amazing-website.com/image.jpeg';
var options = url.parse(imgUrl);
 
http.get(options, function (response) {
  var chunks = [];
  response.on('data', function (chunk) {
    chunks.push(chunk);
  }).on('end', function() {
    var buffer = Buffer.concat(chunks);
    console.log(sizeOf(buffer));
  });
});

Image-size is a third-party library for obtaining image size. With the http module, the image size can be obtained remotely.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!