我不认为 imagemagick 可以处理 GET 请求。但是您可以做的是将来自 GET 请求的图像保存到本地文件中,然后调用 imagemagick 将此图像裁剪为单独的图块。
一个非常好的 node.js 的 http 请求库是request。
它有一个pipe()
方法可以将任何响应通过管道传输到文件流:
http.createServer(function (req, resp) {
if (req.url === '/img.jpg') {
if (req.method === 'GET' || req.method === 'HEAD') {
var r= request.get('http://localhost:8008/img.jpg')
.on('error', function(err) {
console.log(err)
}).pipe(fs.createWriteStream('doodle.png')) // pipe to file
}
}
})
通过将响应分配给变量,您可以检查管道操作是否已完成,如果已完成,您可以调用 imagemagick 方法进行裁剪操作。
流是事件发射器,因此您可以侦听某些事件,例如end
事件。
r.on('end', function() {
im.convert(['img.jpg','-crop','512x512','output.jpg'], function(err) {
if(err) { throw err; }
res.end("Image crop complete");
});
});
这是完整的代码(但未经测试)。这仅用于指导。
http.createServer(function (req, resp) {
if (req.url === '/img.jpg') {
if (req.method === 'GET' || req.method === 'HEAD') {
var r = request.get('http://localhost:8008/img.jpg')
.on('error', function(err) {
console.log(err)
}).pipe(fs.createWriteStream('doodle.png')) // pipe to file
r.on('end', function() {
im.convert(['img.jpg','-crop','512x512','output.jpg'], function(err) {
if(err) { throw err; }
res.end("Image crop complete");
});
});
}
}
})