search

Home  >  Q&A  >  body text

node.js - node如何获取远程图片的尺寸?

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

怪我咯怪我咯2767 days ago751

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 12:06:30

    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.

    reply
    0
  • Cancelreply