【问题标题】:AWS lambda convert svg to pngAWS lambda 将 svg 转换为 png
【发布时间】:2018-12-08 18:26:53
【问题描述】:

我想使用 AWS Lambda 服务将 SVG 转换为使用 NodeJS 语言的 PNG。

第二次,SVG 将包含带有自定义字体的文本,我想知道如何使用外部字体。

目前我使用 PHP 将 SVG 转换为 PNG 以下是 PHP 转换的示例:

$im = new Imagick();
$draw = new ImagickDraw();
$im->setFormat('MSVG');
// Open SVG with ImageMagick
$im->readImageBlob($svgParse->asXML());
$im->setImagePage(0, 0, 0, 0);
$im->scaleImage($viewBox['2'],$viewBox['3'], false);
// Add Text with custom FONT
foreach($ListText as $text){
    // Set Custom FONT
    $draw->setFont(Mage::getBaseDir('media').DS.'font'.DS.$text['font-family'].".ttf");
    $draw->setFontSize( $text['font-size'] );
    $draw->setFillColor ( $text['fill']);
    // Add text in image
    $im->annotateImage($draw, $text['x'], $text['y'], 0, $text['text']);
}
/*png settings*/
// FB image
$imFB = clone $im;
$imFB->setFormat('PNG');
$imFB->cropImage(760, 400,0, 0);
$imFB->scaleImage(600,400,1);
// Save
$imFB->writeImage($filename_FB);
$imFB->clear();
$imFB->destroy();

如果我可以为 AWS Lambda 转换上述代码,那将是完美的。我选择 NodeJS 是因为与 Python 和 Java 相比,我在 Web 上找到了更多示例,但是如果您控制 Python 或 Java,没问题我会更改语言

目前在 AWS lambda 上的代码:

var im = require('imagemagick');
var fs = require('fs');
var util = require("util");


var postProcessResource = function(resource, fn) {
    var ret = null;
    if (resource) {
        if (fn) {
            ret = fn(resource);
        }
        try {
            fs.unlinkSync(resource);
        } catch (err) {
            // Ignore
        }
    }
    return ret;
};

exports.handler = function(event, context) {
    var operation = event.operation;
    delete event.operation;
    if (operation) {
        console.log('Operation', operation, 'requested');
    }

    switch (operation) {
        case 'ping':
            context.succeed('pong');
            break;
        case 'convert':

            var conv = im.convert(['svg:-', 'png:-'])
            conv.on('data', function(data) {
                //console.log('data');
                //console.log(data);
            }); 
            conv.on('end', function() {
                //console.log('end');
            });                                                                                
            conv.stdin.write('<svg height="100%" version="1.1" width="100%"   style="overflow: hidden; position: relative;" id="paper" viewBox="0 0 760 890" preserveAspectRatio="xMinYMin"><desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Created with Raphaël 2.1.2</desc><defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></defs><image x="0" y="0" width="760" height="890" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://camaieu.akria.fr/media/catalog/product/cache/1/image_position_coeur/9df78eab33525d08d6e5fb8d27136e95/3/_/3_1_2.png" id="svg_image" preserveaspectratio="" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" stroke-width="1" product_id="22"></image><text x="320" y="400" text-anchor="left" font-family="detex" font-size="32px" stroke="none" fill="#fefefe" id="svg_message" stroke-width="1" height="80" width="170" style="font-size: 32px;"><tspan dy="10.6015625" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">zdfazerf</tspan></text></svg>');
            conv.stdin.end();
            console.log(conv.toString('base64'));
            break;
        default:
            context.fail(new Error('Unrecognized operation "' + operation + '"'));
    }
};

这个过程需要9s但我不知道检索结果,我什至不知道实际上是什么

示例不成功:

我开始使用 NodeJS 和 AWS Lambda。

谢谢。

【问题讨论】:

  • 也许你可以看看 elasticTranscoder AWS 服务aws.amazon.com/elastictranscoder
  • AWS 弹性转码器只支持格式化视频和音频,它不允许转换图像
  • 是的,对不起,我的错。这是一项请求功能,但尚不可用

标签: node.js amazon-web-services svg aws-lambda


【解决方案1】:

这是我刚刚使用 pandoc/xelatex 在 AWS Lambda 上为自定义字体工作的内容。我敢打赌,您可以根据自己的目的调整这种技术。

我在我的项目中创建了一个fonts 目录并将我的所有字体都放在那里。同样在该目录中,我创建了一个 fonts.conf 文件,如下所示:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>

然后在我的(基于 node.js 的)处理函数中,在调用 pandoc 之前,我设置了一个 ENV var 来告诉 fontconfig 在哪里可以找到字体。

process.env.FONTCONFIG_PATH='/var/task/fonts'

之后,我可以在我的模板中按名称(只是 Bitter)引用字体,例如 Bitter,然后 pandoc/xelatex/fontconfig/whatever 知道要使用哪个版本的字体(例如 @987654328 @ vs Bitter-Italic.otf) 基于任何文本应该具有的样式。

我是根据这个项目中关于让 RSVG 在 Lambda 上使用自定义字体的技巧来解决这个问题的:https://github.com/claudiajs/rsvg-convert-aws-lambda-binary/blob/master/README.md#using-custom-fonts

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 2011-09-09
    • 2019-08-05
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2012-04-09
    相关资源
    最近更新 更多