【问题标题】:Using ImageMagick or GraphicsMagick on Azure Functions在 Azure Functions 上使用 ImageMagick 或 GraphicsMagick
【发布时间】:2017-03-31 17:44:44
【问题描述】:

我正在尝试查看我的公司是否可以使用 Azure Functions 将 TIFF 文件自动转换为多种 JPG 和 PNG 格式和大小。我在 Node.js 中使用 Functions,但也可以使用其他语言。

我的问题是,我无法让 GraphicsMagick 或 ImageMagick 处理函数。我使用 npm install 使用正常的安装程序。

似乎安装正常,并且模块似乎也加载了,但是当我尝试处理文件时没有任何反应。什么都没有,因为也没有错误。

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

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

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); };

我不确定这是否可能,但我没有找到任何说明它不能的信息。

那么,我可以在 Functions 中使用 ImageMagick、GraphicsMagick 或第三个图像转换器吗?如果是,安装时我需要注意什么特别的事情吗?

是否还有 C# 解决方案?

【问题讨论】:

  • c# 版本如果可能会更好
  • Cloudinary API 可用于从他们的页面currently supports the following formats: JPG, PNG, GIF, BMP, TIFF, ICO, PDF, EPS, PSD, SVG, WebP, JXR, and WDP 进行图像转换cloudinary.com/cookbook/converting_an_image_format

标签: node.js azure imagemagick graphicsmagick azure-functions


【解决方案1】:

Azure 函数也可以运行自定义 docker 映像

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

不确定您对哪种语言感兴趣,但您可以拥有一个具有以下样式 Dockerfile 的 python 图像

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

然后使用PythonMagick 来处理同样的事情

【讨论】:

    【解决方案2】:

    您可以使用站点扩展使 imagemagick 可用于 azure web 应用程序。

    您可以查看存储库以获取更多信息:https://github.com/fatihturgut/azure-imagemagick-nodejs

    【讨论】:

    • 当然你仍然需要安装 npm 包 :-)
    【解决方案3】:

    Azure 中的 Web 应用是一种 SaaS(软件即服务)。您将您的位部署到 Azure IIS 容器,其余的由 Azure 完成。我们没有太多的控制权。 因此,我们将无权在 Azure Functions App(例如 ImageMagick 或 GraphicsMagick)上安装任何第 3 方可执行文件。如果您需要这样做,请查看虚拟机。另一种选择是使用云服务的 Web 或 Worker 角色。

    另外,有一个很好的 Node 图像处理库,完全用 JavaScript 编写,外部或本地依赖项为零,Jimp。 https://github.com/oliver-moran/jimp

    示例用法:

    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);
    });
    

    还有一个名为sharp 的node.js 库可以满足您的要求。你可以这样试试:

    首先,在您的本地环境中安装Sharp,然后使用包含已编译模块的node_modules 文件夹将您的应用程序部署到Azure。最后,将 Azure 应用服务上的节点可执行文件升级到 64 位。

    类似的帖子你可以参考here

    示例用法:

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

    【讨论】:

    • 是的,我还在某处读到无法在 Azure Functions 上安装可执行文件……但我还是尝试了,它确实有效!我在一个文件夹中安装了 GraphicsMagick 并为 GraphicsMagick 执行了一个子进程,它确实转换了我想要的文件。这是一个错误,还是按预期使用?我还查看了 Jimp,但它无法转换 TIFF,所以很遗憾,这会破坏交易。
    • 刚刚花了 4 个小时试图在 Azure Functions 上快速运行 :-( 只是无法将所有部分放在一起。
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2017-11-25
    相关资源
    最近更新 更多