【问题标题】:jquery-file-upload-middleware with express.js: how to move files & add watermarks?带有 express.js 的 jquery-file-upload-middleware:如何移动文件和添加水印?
【发布时间】:2014-07-14 06:59:45
【问题描述】:

我设法使用 express.js 4.0 设置了 https://github.com/aguidrevitch/jquery-file-upload-middleware,但在配置它时遇到了困难。

这是我的上传脚本:

var upload = require('jquery-file-upload-middleware');
upload.configure({
    imageVersions: {
        thumbs: {
            width: 80,
            height: 80
        },
        prev: {
            width: 1280,
            height: 1024
        }
    }
});

app.use('/admin/upload', function (req, res, next) {
    // imageVersions are taken from upload.configure()
    upload.fileHandler({
        uploadDir: function () {
            return __dirname + '/public/uploads/' + req.session.eventID;
        }
    })(req, res, next);

});

上传 Chicken.jpg 文件我得到以下结构:

/public/uploads/  -> public uploads folder
    534a8d502e889f8d6bf9cc07/  -> upload session folder
        prev/  -> resized version folder
            Chicken.jpg
        thumbs/    -> another resized version folder
            Chicken.jpg
        Chicken.jpg   -> original file

这就是我想要实现的目标:

  1. 将原始文件 /public/uploads/534a8d502e889f8d6bf9cc07/Chicken.jpg 移出 /public/uploads 文件夹,同时保留调整大小的版本。
  2. /public/uploads/534a8d502e889f8d6bf9cc07/prev/Chicken.jpg 文件中添加水印。

谁能给点建议?

谢谢!

【问题讨论】:

    标签: javascript node.js express imagemagick jquery-file-upload


    【解决方案1】:

    如何移动原始文件:

    jquery-file-upload-middleware 网站上,它解释了如何移动文件,如果你阅读了他们的文档,如何移动带有自定义后缀(用户 ID、会话 ID 等)的文件:

    app.use('/api', function (req, res, next) {
                req.filemanager = upload.fileManager();
                next();
            });
    
            app.use('/api/endpoint', function (req, res, next) {
                // your real /api handler that will actually move the file
                ...
                // req.filemanager.move(filename, path, function (err, result))
                req.filemanager.move('SomeFile.jpg', 'project1', function (err, result) {
                    // SomeFile.jpg gets moved from uploadDir/SomeFile.jpg to
                    // uploadDir/project1/SomeFile.jpg
                    // if path is relative (no leading slash), uploadUrl will
                    // be used to generate relevant urls,
                    // for absolute paths urls are not generated
                    if (!err) {
                        // result structure
                        // {
                        //     filename: 'SomeFile.jpg',
                        //     url: '/uploads/project1/SomeFile.jpg',
    

    如果您不想这样做,(This post) 将解释如何使用 node.js 将文件从一个位置移动到另一个位置。我将 unlinkSync() 更改为 unlink()

    var fs = require('fs');
    //var util = require('util');
    
    var is = fs.createReadStream('source_file');
    var os = fs.createWriteStream('destination_file');
    
    is.pipe(os);
    is.on('end',function() {
        fs.unlink('source_file', function(err){
             // Continue execution     
        });
    });
    
    /* node.js 0.6 and earlier you can use util.pump:
    util.pump(is, os, function() {
        fs.unlink('source_file', function(err){
             // Continue execution
        });
    });
    */
    

    为文件添加水印

    This post 解释了如何使用节点生成子进程并使用 ImageMagick 为图像添加水印:

        // Require our module dependencies
    var exec = require('child_process').exec;
    
    // Create command array to invoke ImageMagick composite where
    // -dissolve is the amount of transparency for the watermark
    // -gravity tells how to align images of varying size
    // -quality is the image quality of the JPEG (not required if producing PNG)
    var command = [
        'composite',
        '-dissolve', '50%',
        '-gravity', 'center', 
        '-quality', 100,
        pathToWatermarkJpg,
        pathToImageJpg,
        pathToResultJpg;
    ];
    
    // Join command array by a space character and then execute command
    exec(command.join(' '), function(err, stdout, stderr) {
        // Do stuff with result here
    });
    

    【讨论】:

      猜你喜欢
      • 2014-12-11
      • 2019-01-06
      • 1970-01-01
      • 2014-06-06
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2019-09-04
      相关资源
      最近更新 更多