使用nodejs中的gm调整图像大小而不将其上传到任何地方 [英] resize an image without uploading it to anywhere using gm in nodejs

查看:84
本文介绍了使用nodejs中的gm调整图像大小而不将其上传到任何地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 multipart/form-data 上传图像,我想先调整图像大小,然后再将其存储在磁盘上的任何位置.我正在使用 gm 完成此操作,但无法做到这一点.

I'm uploading an image using multipart/form-data, i want to resize it before storing it anywhere on the disk. I'm using gm to accomplish this but was not able to do it.

<form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
        <input type="file" name="userFile" />
        <input type="submit" value="Upload File" name="submit">
</form>

这是 js 文件,现在我想调整图像大小,而无需使用 node 中的 Imagemagick(gm)模块将其存储在磁盘上的任何位置.我是Node的新手,我们如何使用零件和调整图像大小.

here is the js file, now i want to resize the image without storing it anywhere on the disk using Imagemagick(gm) module in node. I'm new to node, how can we use the part and resize the image.

var express = require('express');
var multiparty = require("multiparty");
var app = express();
const sharp = require('sharp');
const gm = require('gm').subClass({imageMagick: true});

app.get('/', function(req, res){
  res.sendFile('index.html' , { root : __dirname});
});


 app.post('/upload', function(req, res){

  console.log("in upload")

  var count = 0;
  var form = new multiparty.Form();

  // Errors may be emitted
  // Note that if you are listening to 'part' events, the same error may be
  // emitted from the `form` and the `part`.
  form.on('error', function(err) {
    console.log('Error parsing form: ' + err.stack);
  });

  // Parts are emitted when parsing the form
  form.on('part', function(part) {
    // You *must* act on the part by reading it
    // NOTE: if you want to ignore it, just call "part.resume()"

    if (!part.filename) {
      // filename is not defined when this is a field and not a file
      console.log('got field named dd' + part.name);
      // ignore field's content
      part.resume();
    }

    if (part.filename) {
      // filename is defined when this is a file
      count++;
      console.log('got file named ' + part.name);
     // console.log(part);

     part.on('data', (chunk) => {
       console.log("chunck: "+chunk);

      var readStream = fs.createReadStream(chunk);
       gm(readStream, part.filename)
       .resize(240, 240)
       .noProfile()
       .write('res.png', function (err) {
         console.log('Ingm');
         if (!err) console.log('resize done');
           else
               console.log('gm error: '+err);
       });

      });

      // ignore file's content here
      part.resume();
    }

    part.on('error', function (err) {
      // decide what to do
    });
  });

  // Close emitted after form parsed
  form.on('close', function() {
    console.log('Upload completed!');
    res.setHeader('text/plain');
    res.end('Received ' + count + ' files');
  });

  // Parse req
  form.parse(req);

  });

推荐答案

您正在尝试读取上传的文件流,而不是将其传递给imageMagick.另外,您在接收到的文件上使用 resume(),将其丢弃.尝试更改此内容:

You're trying to read the uploaded file stream instead of passing it to imageMagick. Also, you're using resume() on the received file, discarding it. Try changing this:

if (part.filename) {
  // filename is defined when this is a file
  count++
  console.log('got file named ' + part.name)
  // console.log(part);
  part.on('data', (chunk) => {
    console.log('chunck: ' + chunk)
    var readStream = fs.createReadStream(chunk)
    gm(readStream, part.filename)
      .resize(240, 240)
      .noProfile()
      .write('res.png', function (err) {
        console.log('Ingm')
        if (!err) console.log('resize done')
        else
          console.log('gm error: ' + err)
      })
  })
  // ignore file's content here
  part.resume()
}

为此:

if (part.filename) {
  // filename is defined when this is a file
  count++
  console.log('got file named ' + part.name)
  // console.log(part);
  gm(part)
    .resize(240, 240)
    .noProfile()
    .write('res.png', function (err) {
      console.log('Ingm')
      if (!err) console.log('resize done')
      else
        console.log('gm error: ' + err)
    })
  // ignore file's content here; but we don't want that!
  // part.resume()
}

这篇关于使用nodejs中的gm调整图像大小而不将其上传到任何地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆