Javascript 使用GraphicsMagick输出多个文件

Javascript 使用GraphicsMagick输出多个文件,javascript,node.js,express,graphicsmagick,Javascript,Node.js,Express,Graphicsmagick,我设置了以下功能,可以捕获上传的图像,转换它们并将它们写入服务器。如何重命名转换后的文件 目前,该功能获取上传的图像并保存图像的一个版本 目标: // upload new user image app.post('/api/users/user/upload_image', function (req, res) { req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file

我设置了以下功能,可以捕获上传的图像,转换它们并将它们写入服务器。如何重命名转换后的文件

目前,该功能获取上传的图像并保存图像的一个版本

目标:

// upload new user image
    app.post('/api/users/user/upload_image', function (req, res) {
        req.pipe(req.busboy);
        req.busboy.on('file', function (fieldname, file, filename) {

            console.log('\n\nUploading file: '.underline +filename .underline);
            console.log('Uploaded\nConverting...');
            var size = { width: 200, height: 200 };

            gm(file,'www/uploads/' + filename)
            .resize(size.width * 2, (size.height * 2) + '')
            .thumbnail(size.width, size.height + '^')
            .gravity('Center')
            .extent(size.width, size.height)
            .file(filename)
            .profile('*')
            .write('www/uploads/' + filename, function (err) {
                console.log("Success!".bold);
            });

        });

        req.busboy.on('finish', function () {
            res.writeHead(303, { Connection: 'close', Location: '/' });
            res.end();
        });
    });
  • 根据图像大小保存上载图像的多个版本
  • 使用图像大小附加文件名,例如文件名-400x400.jpg,文件名-200x200.jpg
我不太确定如何处理第一个和第二个,我认为busboy正在干扰它,因为下面函数中的
filename
包括扩展名。我需要能够附加文件名的大小之前的扩展名。这可以用正则表达式完成吗

功能:

// upload new user image
    app.post('/api/users/user/upload_image', function (req, res) {
        req.pipe(req.busboy);
        req.busboy.on('file', function (fieldname, file, filename) {

            console.log('\n\nUploading file: '.underline +filename .underline);
            console.log('Uploaded\nConverting...');
            var size = { width: 200, height: 200 };

            gm(file,'www/uploads/' + filename)
            .resize(size.width * 2, (size.height * 2) + '')
            .thumbnail(size.width, size.height + '^')
            .gravity('Center')
            .extent(size.width, size.height)
            .file(filename)
            .profile('*')
            .write('www/uploads/' + filename, function (err) {
                console.log("Success!".bold);
            });

        });

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

就创建多个文件而言,您可以复制已有的gm链,只需使用不同的文件名即可

对于文件名问题,您可以创建自己的文件名,也可以使用regexp将维度插入原始文件名。对于后者,您可以使用以下内容:

// changes "foo.jpg" to "foo-400x400.jpg"
filename.replace(/\.[^\.]+$/, '-400x400$&')