-1
내 node.js 프로젝트에서 웹에서 이미지를 가져 와서 원하는 크기로 크기를 조정하고 base64 문자열로 출력 할 수 있기를 원합니다.서버 크기의 이미지 크기 조정
어떤 라이브러리가 가장 좋습니까? 나는 다른 사람의 무리를 시도했지만 그들 중 누구도 작동하지 않습니다 :
var url = "www.abc.com/image.png"
var gm = require('gm')
gm(request(url))
.resize(100, 100)
.setFormat('jpg')
.toBuffer(function (err, buffer) {
if (err) {
console.log("error" + err);
} else {
console.log('done!');
}
})
var Canvas = require('canvas')
, Image = Canvas.Image
, canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d');
// set its dimension to target size
canvas.width = width;
canvas.height = height;
// draw source image into the off-screen canvas:
ctx.drawImage(request(url), 0, 0, width, height);
console.log(canvas.toDataURL());
sharp('input.gif')
.resize(478, 269)
.toFormat('jpeg')
.toBuffer(function(err, outputBuffer) {
if (err) {
throw err;
}
// outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high
// containing a scaled version, embedded on a transparent canvas, of input.gif
});
require('lwip').open(request(url), function(err, image){
image.resize(300, 200, function(err, image){
console.log("done");
});
});
var request = require('request');
var lwip = require('lwip');
request({url: url, encoding:null}, function (err, response, imageBuffer) {
var imageFormat = response.headers["content-type"].match(/(png|jpg|jpeg)/)[0];
lwip.open(imageBuffer, imageFormat, function(err, image){
if (err || !image) throw err;
image.resize(196, 196, function(err, image){
if (err || !image) throw err;
image.toBuffer(imageFormat, function(err, buffer){
//here you buffer you can save image in file with FS
});
});
});
}
});
gm은 좋은 라이브러리 중 하나입니다. –
내 코드에 어떤 문제가 있습니까? –