Search code examples
node.jsexpressgraphicsmagick

Express js multiple operations gm (GraphicsMagick) module


Using Expressjs with gm (GraphicsMagick) module.

Currently (see code)operation #1 and #2 work correctly when executed separately, but they do not work together (as seen below).

I would like to combine both operations in one statement, any suggestions?

var express = require('express');
var router = express.Router();
var gm = require('gm'); // GraphicsMagick

router.get('/', function(req, res) {

    gm('image.png') 
        // Operation #1
        .composite('topimage.png')
        .geometry('+200+200')

        // Operation #2
        .drawText(5, 20, 'my text')
        .fontSize(20)
        .font(__dirname + 'fonts/MyFont.TTF')

        .stream(function streamOut (err, stdout, stderr) {
            stdout.pipe(res); //pipe to response
        });

});

module.exports = router;

Solution

  • After a while of struggle I found that gm() can take a Stream as the input param. The following code solves my question.

    var express = require('express'); 
    var router = express.Router(); 
    var gm = require('gm'); // GraphicsMagick
    
    router.get('/', function(req, res) {
    
       gm(gm('image.png') 
                     // Operation #1
                .composite('topimage.png')
                .geometry('+200+200')
                .stream())
    
                     // Operation #2
                .drawText(5, 20, 'my text')
                .fontSize(20)
                .font(__dirname + 'fonts/MyFont.TTF')
    
                .stream(function streamOut (err, stdout, stderr) {
                    stdout.pipe(res); //pipe to response
                });
    
        });