2016-03-21 63 views
1

我一直在研究nodejs中的imagemagick模块,并且我在某个时刻被卡住了。我正在尝试调整图片大小。我需要调整图像的base64部分。我如何检索它?我尝试下面的代码,但这个创建空图像。使用Imagemagick在节点中调整图像大小

im.resize({ 
     srcPath: image, 
     width: 750, 
     height: 280 
     }, function(err, stdout, stderr) { 
     if (err) { 
      return next(err); 
     } 
     console.log('resized store-medium.jpg to fit within 750x280px'); 
     var mediumImage = new Buffer(stdout).toString('base64'); 
+0

这是一个相当老话题,但因为我有同样的问题,我会试试看。 @ ahmet-tanakol:你能解决问题吗? –

+0

@RicardoPesciotta不幸的是我找不到合适的解决方案,我用了一个名为imgix的服务https://www.imgix.com/ –

回答

0

使用dstPath参数并读取生成的图像,然后再转换为Base64。

例子:

var dstImage = image.replace(/\.\w+$/, '-resized.'); 
im.resize({ 
    srcPath: image, 
    dstPath: dstImage, // <--- destiny image 
    width: 750, 
    height: 280 
}, function (err, stdout, stderr) { 
    if (err) { 
     return next(err); 
    } 
    console.log('resized store-medium.jpg to fit within 750x280px'); 
    fs.readFile(dstImage, function (err, data) { 
     next(null, data.toString('base64')); 
    }); 
}); 
+0

我要调整大小并上传很多图像,所以这种方法会创建太多副本。 –