Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS图像上载成功,但调整大小不适用于GraphicsMagick_Node.js_Express_Fs_Graphicsmagick_Formidable - Fatal编程技术网

Node.js NodeJS图像上载成功,但调整大小不适用于GraphicsMagick

Node.js NodeJS图像上载成功,但调整大小不适用于GraphicsMagick,node.js,express,fs,graphicsmagick,formidable,Node.js,Express,Fs,Graphicsmagick,Formidable,我正在尝试调整已上载的图像的大小。我已经测试了几乎可以在互联网上找到的方法,但它们似乎都不起作用。我能够将图像上传到服务器的文件夹,但它仍然保持完整的图像大小。我错过了什么 var formidable = require('formidable'), path = require('path'), fs = require('fs'), gm = require('gm'); //handles image upload exports.image = functio

我正在尝试调整已上载的图像的大小。我已经测试了几乎可以在互联网上找到的方法,但它们似乎都不起作用。我能够将图像上传到服务器的文件夹,但它仍然保持完整的图像大小。我错过了什么

var formidable = require('formidable'),
    path = require('path'),
    fs = require('fs'),
    gm = require('gm');

//handles image upload
exports.image = function (req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        var file = files.file;
        var tempPath = file.path;

        var rename = makeid();
        var targetPath = path.resolve('./public/assets/images/upload/'+ file.name );

        fs.rename(tempPath, targetPath, function (err) {
            if (err) {
                throw err
            }
            gm( targetPath).resize(null, 50).on('finish', (function done(file) {
                console.log('done: ' + file);
            }).bind(null, file));

            return res.json({path: 'assets/images/upload/'+ file.name })
        })
    });
};

function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 15; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
var-leverable=require('leverable'),
path=require('path'),
fs=需要('fs'),
gm=要求(“gm”);
//处理图像上传
exports.image=函数(请求、分辨率){
var form=new.IncomingForm();
解析(请求、函数(错误、字段、文件){
var file=files.file;
var tempPath=file.path;
var rename=makeid();
var targetPath=path.resolve('./public/assets/images/upload/'+file.name);
fs.rename(临时路径、目标路径、函数(错误){
如果(错误){
失误
}
gm(targetPath).resize(null,50).on('finish'),(函数完成(文件){
log('done:'+文件);
}).bind(null,file));
返回res.json({path:'assets/images/upload/'+file.name})
})
});
};
函数makeid()
{
var text=“”;
var-mablue=“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789”;
对于(变量i=0;i<15;i++)
text+=可能的.charAt(Math.floor(Math.random()*可能的.length));
返回文本;
}

调整大小不是就地完成的,您必须使用
write()
将调整大小的文件写入文件系统。@ShanShan您的意思是这样的吗?gm(targetPath).resize(null,50).write(path.resolve('./public/assets/images/upload/new.jpg'),函数(err){if(!err)console.log('done');})@ShanShan,我想知道在调整大小的过程中targetpath映像是否也可用?是的,源映像只被读取,不被修改(除非您
write()
到相同的路径)。我不明白你的第二个问题,源文件没有被锁定,所以可以读取。我按照你说的做了,我有一个无效参数错误-resize@ShanShan