【发布时间】: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