node-imagemagick在react js中不起作用

时间:2019-07-05 07:11:07

标签: reactjs image-uploading

我想在React端使用imagemagick调整尺寸(降低质量)以上传图像。但它的功能无法正常工作并出现错误。

var im = require('imagemagick');
im.resize({
                srcPath: event.target.files[0],
                dstPath: 'kittens-small.jpg',
                width:   256
              }, function(err, stdout, stderr){
                if (err) throw err;
console.log('resized kittens.jpg to fit within 256x256px');
              });

这些错误总是来的。

TypeError: childproc.spawn is not a function
   }
  24 | }
  25 | 
> 26 | var child = childproc.spawn(file, args);
     | ^  27 | var killed = false;
  28 | var timedOut = false;

1 个答案:

答案 0 :(得分:0)

您不能在浏览器环境中使用imagemagic。如自述模块中所述,它适用于Node JS。

我猜代码想从您的节点进程中产生一个子进程来执行图像魔术。但是,因为您使用的是浏览器,所以没有从中生成子代的过程。

要在浏览器中调整图像大小,可以使用canvas