在node.js中使用imagemagick调整图像大小

在我的应用程序,我试图调整图像使用imagemagick,但我有以下错误时resize错误:imagesexecvp():没有这样的文件或directory.this是我的代码

im.resize({ srcPath: '/tmp/Images/' + req.files.image.name, dstPath: 'resized_'+req.files.image.name , width:42, height:42 }, function(err, stdout, stderr){ if (err) { console.log('error while resizing images' + stderr); }; }); 

imagemagick模块使用convertidentify命令,并且您所描述的错误可能会发生,因为找不到命令。 确保命令位于path环境variables引用的文件夹中,或者可以引用节点应用程序中的命令:

 var im = require('imagemagick'); im.identify.path = '/opt/ImageMagick/bin/identify' im.convert.path = '/opt/ImageMagick/bin/convert'; 

它看起来像你正在尝试调整文件的大小,而不改变目的地。 尝试这个:

 im.resize({ srcPath: process.cwd() + '/tmp/Images/' + req.files.image.name, dstPath: process.cwd() + '/tmp/Images/resized_'+req.files.image.name , width:42, height:42 }, function(err, stdout, stderr){ if (err) { console.log('error while resizing images' + stderr); }; console.log( process.cwd() + '/tmp/Images/' + req.files.image.name + 'has been resized and saved as ' + process.cwd() + '/tmp/Images/resized_'+req.files.image.name) }); 

您也可以在/tmp/Images/检查您的权限(ls -l),以确保它们设置正确

你需要安装image magic命令行工具。 尝试brew install imagemagick

如果你在Ubuntu上,像这样安装包:

 apt-get install imagemagick 

之后,再试一次:D