Search code examples
node.jsazureimagemagickgraphicsmagickazure-functions

Using ImageMagick or GraphicsMagick on Azure Functions


I am trying to see if my company can use Azure Functions to automate conversions of TIFF files to a number of JPG and PNG formats and sizes. I am using Functions with Node.js, but other languages could be used.

My problem is, that I can't get GraphicsMagick or ImageMagick to work on Functions. I used the normal procedures for installment using npm install.

It seems to install ok, and the module also seems to load, but nothing happens when I try to process a file. Nothing, as in no errors either.

var fs = require('fs'); var gm = require('gm');

module.exports = function (context, req) { context.log('Start...');

try {
    context.log('Looking for GM...');
    context.log(require.resolve("gm"));
} catch(e) {
    console.log("GM is not found");
    process.exit(e.code);
}

gm('D:/home/site/wwwroot/HttpTriggerJS1/input/870003-02070-main-nfh.jpg')
    .resize(240, 240)
    .noProfile()
    .write('D:/home/site/wwwroot/HttpTriggerJS1/output/resize.jpg', 
    function (err) {
        context.log('TEST');
        if (!err) {
            context.log('done');
        }
    }
);

context.done(null, res); };

I'm not sure that it's even possible, but I haven't found any information that states that it can't.

So, can I use ImageMagick, GraphicsMagick or a third image converter in Functions? If yes, is there something special that I need to be aware of when installing?

Is there also a C# solution to this?


Solution

  • Web Apps in Azure is a SaaS (Software as a Service). You deploy your bits to the Azure IIS containers, and Azure do the rest. We don’t get much control. So we will not have the privilege to install any 3rd party executable file on Azure Functions App (e.g. ImageMagick or GraphicsMagick). If you need to do that, look at Virtual Machines. Another option is using Cloud Services' Web or Worker Role.

    Alternatively, there is a good image processing library for Node written entirely in JavaScript, with zero external or native dependencies, Jimp. https://github.com/oliver-moran/jimp

    Example usage:

    var Jimp = require("jimp");
    
    Jimp.read("lenna.png").then(function (lenna) {
        lenna.resize(256, 256)            // resize
             .quality(60)                 // set JPEG quality
             .greyscale()                 // set greyscale
             .write("lena-small-bw.jpg"); // save
    }).catch(function (err) {
        console.error(err);
    });
    

    There is another node.js library called sharp to achieve your requirement. You may try this way:

    First, install the sharp on your local environment, and then deploy your application to Azure with the node_modules folder which contains the compiled module. Finally, upgrade the node executable on Azure App Service to 64-bits.

    The similar thread you can refer here.

    Example usage:

    var sharp = require("sharp");
    
    sharp(inputBuffer)
      .resize(320, 240)
      .toFile('output.webp', (err, info) => {
          //...
      });