本文介绍了Express GraphicsMagick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建MEAN应用,而我目前正在研究的部分涉及图片上传.我正在尝试使用 GraphicsMagick for Node ,但实际上并没有取得任何成功.以下是我对图片上传的POST请求(原样):

I'm currently building a MEAN app and the section I'm currently working on involves image uploads. I'm trying to use GraphicsMagick for Node but I'm not really having any success. Below is my POST request for image uploads (as is):

app.post('/api/users/upload_image', function (req, res) {
        var fstream;
        req.pipe(req.busboy);
        req.busboy.on('file', function (fieldname, file, filename) {
            console.log('\n\nUploading file: '.underline.bold +filename .underline.bold);


            // var readStream = fs.createReadStream(filename);
            // gm(readStream, readStream.path)
            //  .resize('200','200')
            //  .stream(function (err, stdout, stderr) {
            //      var writeStream = fs.createWriteStream('www/uploads/' + readStream.path);
            //      stdout.pipe(writeStream);
            //  });

            fstream = fs.createWriteStream('www/uploads/' + filename);
            file.pipe(fstream);

        });

        req.busboy.on('finish', function () {
            res.writeHead(303, { Connection: 'close', Location: '/' });
            res.end();
        });
    });

注释掉的部分是我尝试使用GM的尝试,但是抛出了错误:错误:ENOENT,打开'[filename] .jpg'

The commented out section is my attempt at using GM but that throws back the error: Error: ENOENT, open '[filename].jpg'

我要去哪里错了?这是我第一次尝试使用GM,所以我是该库的新手!

Where am I going wrong? This is my first try at using GM so I'm a newb to this library!

推荐答案

var readStream = fs.createReadStream(filename);

在此名为filename的行中实际上并没有文件,但是您将该文件写在了注释行的下面.您所拥有的是从busyboy获得的名为 file 的流.所以摆脱这一行,直接将 file 传递给gm

at this line file named filename is not actually there yet you write that file below the commented lines. What you have is read steam named file you get from busyboy. so get rid of this line and pass file to gm directly

gm(file,....

下面的代码正是您想要的,请注意gm的write函数的参数.

Code below does exactly what you want, note the gm's write function's parameters.

app.post('/api/users/upload_image', function (req, res) {
    req.pipe(req.busboy);
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log('\n\nUploading file: '.underline.bold +filename .underline.bold);

        console.log('Resizing file...');

        gm(file,'www/uploads/' + filename)
        .resize(200,200)
        .write('www/uploads/' + filename, function (err) {
            console.log("finished");
        });
    });

    req.busboy.on('finish', function () {
        res.writeHead(303, { Connection: 'close', Location: '/' });
        res.end();
    });
});

请注意,以这种方式使用gm调整大小时,只是将其图像放置在200x200的框中,并不能完全将其缩放/拉伸为200x200.

Note that gm's resize when used this way, simply fit's image in 200x200 box, does not exactly rescale/stretch it to 200x200.

如果这仍然给您类似EPIPE的错误,则您的节点进程可能没有足够的权限来写入该文件.

If this still give you an error like EPIPE, your node process might not have sufficent permissions to write that file.

这篇关于Express GraphicsMagick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 07:34