Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 352
Morten Hansen Avatar asked Nov 17 '16 09:11

Morten Hansen


3 Answers

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) => {
      //...
  });
like image 165
Aaron Chen Avatar answered Nov 19 '22 21:11

Aaron Chen


Azure functions can run custom docker images also

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image

Not sure which language you are interested in, but you can have a python image with below style Dockerfile

FROM mcr.microsoft.com/azure-functions/python:2.0

RUN apt-get update && \
    apt-get install -y --no-install-recommends apt-utils  && \
    apt-get install -y imagemagick

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY . /home/site/wwwroot

RUN cd /home/site/wwwroot && \
    pip install -r requirements.txt

And then use the PythonMagick to work with the same

like image 37
Tarun Lalwani Avatar answered Nov 19 '22 21:11

Tarun Lalwani


You can use the site extension to make imagemagick work for azure web apps.

You can check the repository for more info: https://github.com/fatihturgut/azure-imagemagick-nodejs

like image 2
Fatih Turgut Avatar answered Nov 19 '22 23:11

Fatih Turgut