Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Javascript jquery文件上传中间件与express.js:如何移动文件&;添加水印?_Javascript_Node.js_Express_Imagemagick_Jquery File Upload - Fatal编程技术网

Javascript jquery文件上传中间件与express.js:如何移动文件&;添加水印?

Javascript jquery文件上传中间件与express.js:如何移动文件&;添加水印?,javascript,node.js,express,imagemagick,jquery-file-upload,Javascript,Node.js,Express,Imagemagick,Jquery File Upload,我设法用express.js 4.0进行了设置,但在配置时遇到了困难 以下是我的上传脚本: var upload = require('jquery-file-upload-middleware'); upload.configure({ imageVersions: { thumbs: { width: 80, height: 80 }, prev: { width:

我设法用express.js 4.0进行了设置,但在配置时遇到了困难

以下是我的上传脚本:

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
这就是我想要实现的目标:

  • 将原始文件/public/uploads/534a8d502e889f8d6bf9cc07/Chicken.jpg移出/public/uploads文件夹,同时保留大小调整后的版本
  • /public/uploads/534a8d502e889f8d6bf9cc07/prev/Chicken.jpg文件中添加水印
  • 有人能给我建议吗


    谢谢大家!

    如何移动原始文件:

    在网站上,它解释了如何移动文件,以及如果您阅读了他们的文档,如何移动带有自定义后缀(用户ID、会话ID等)的文件:

    如果不想这样做,()说明了如何使用节点将文件从一个位置移动到另一个位置。我将unlinkSync()更改为unlink()

    向文件添加水印

    说明如何使用node生成子进程并使用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
    });
    
    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
        });
    });
    */
    
        // 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
    });