1

我正在研究一个 lambda 函数,它将通过 amazon rest api 触发。

我创建了以下

  • REST API 控制台上的POST端点
  • 链接到 REST API 的 lambda 函数

现在作为第一次尝试,我创建了函数,以便它将接受参数作为

{
 "userProfileImagePath":"facebook users profile image path via GET /{user-id}/picture"
}

lambda 函数将使用request模块获取图像,然后上传到存储桶。

由于我使用了多个模块,因此我在本地创建了所有内容并将它们压缩,包括并通过指定名称node_modules将它们上传到 lambda 函数控制台。Handler

到目前为止它很好,而且效果很好。

现在为了扩展它,我试图发送 2 张个人资料图片 - 一张来自用户 - 另一张是他/她的朋友之一 - 将两张图片合并为一张,然后将合并后的图片上传到目标文件。

我尝试了不同的合并方法,但没有任何效果。在这里找到了一些解决方案,似乎他们真的不喜欢流

这是我到目前为止所做的,正如我提到的那样,这两个图像都被上传到一个存储桶中,但是合并似乎失败了,我没有办法完成这个,如果你能给我一些关于如何做。

目前它使用异步模块来完成单个任务

  • 首先将两个图像并行上传到s3
  • 然后使用 s3.getObject 并行下载它们
  • 最后在完成第二个任务时尝试合并并推回 s3

基本上会有一个模板图像,并且在模板图像上将放置(合并)多个图像,在下面的代码中,我将userProfileImagePath其视为主模板并尝试将另一个图像合并到其中。

如果有其他方法可以做到这一点,请告诉我。

这是我到目前为止所做的

/**
* This is a test script which runs as a lambda function on amazon 
* The lamda function is linked with an amazon end point
* The script will accept a image url (facebook/gravator) etc and will upload to a destination s3 bucket and returns the url 
* The param looks like 
{
    "userProfileImagePath":"https://fbcdn-profile-a.......",
    "friendProfileImagePath":"https://fbcdn-profile-a......."
}
*/

var exec = require('child_process').exec,
    async = require('async'),
    request = require('request'),
    gm = require('gm'),
    imageMagick = gm.subClass({ imageMagick: true }),
    aws = require('aws-sdk');

exports.handler = function(req, context) {
    var errMsg = '',
        userProfileImageName = 'user_profile',
        friendProfileImageName = 'friend_profile',
        mergedImageName = 'final_image',
        destinationBucket = 'destination_bucket',
        response = {} ,
        s3 = new aws.S3();

    if (req.userProfileImagePath === '') {
        errMsg = 'Missing the userProfileImage';
    }

    if (req.friendProfileImagePath === '') {
        errMsg = 'Missing the friendProfileImagePath ';
    }

    if (errMsg === '') {
        async.auto({
            copyUserImageToS3 : function(autoCallback) {
                console.log('MyImage :'+req.userProfileImagePath);
                //var myImageData = {} ;
                request({
                    url: req.userProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }

                    s3.putObject({
                        Bucket: destinationBucket,
                        Key: userProfileImageName+'.jpg',
                        ContentType: res.headers['content-type'],
                        ContentLength: res.headers['content-length'],
                        Body: body, // buffer
                        ACL:'public-read'
                    }, autoCallback); 
                });
            },

            copyFriendImageToS3 : function(autoCallback) {
                console.log('FriendImage :'+req.friendProfileImagePath);
                //var friendImageData = {} ;
                request({
                    url: req.friendProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }  

                    s3.putObject({
                        Bucket: destinationBucket,
                        Key: friendProfileImageName+'.jpg',
                        ContentType: res.headers['content-type'],
                        ContentLength: res.headers['content-length'],
                        Body: body, // buffer
                        ACL:'public-read'
                    }, autoCallback);
               });
            },

            getUserImageFromS3 : ['copyUserImageToS3', function(autoCallback,results) {
                    s3.getObject({
                    Bucket: destinationBucket,
                    Key: userProfileImageName+'.jpg',
                }, autoCallback);
            }],

            getFriendImageFromS3 : ['copyFriendImageToS3', function(autoCallback,results) {
                    s3.getObject({
                    Bucket: destinationBucket,
                    Key: friendProfileImageName+'.jpg',
                }, autoCallback);
            }],

            mergeImages : ['getUserImageFromS3','getFriendImageFromS3', function(autoCallback,results) {
                console.log('Here');
                gm()
                .in('-page', '+0+0')  // Custom place for each of the images
                .in(results.getUserImageFromS3.body)
                .in('-page', '+100+100')
                .in(results.getFriendImageFromS3.body)
                .minify()  
                .mosaic()  
                .toBuffer('JPG', function (err, buffer) {
                        if (err) { return autoCallback(err); }
                        s3.putObject({
                        Bucket: destinationBucket,
                        Key: mergedImageName+'.jpg',
                        ContentType:  results.getUserImageFromS3.headers['content-type'],
                        ContentLength: results.getUserImageFromS3.headers['content-length'],
                        Body: buffer, // buffer
                        ACL:'public-read'
                    }, autoCallback);
                });
            }],
        },
        function(err, results) {
            if (err) {
                response = {
                    "stat":"error",
                    "msg":"Error manipulating the image :: "+err 
                } ;
                context.done(null,response);
            } else {
                response = {
                    "stat":"ok",
                    "imageUrl":"https://s3-................../"+mergedImageName+".jpg"
                } ;
                context.done(null,response);
            }
        });
    } else {
        response = {
            "stat":"error",
            "msg": errMsg
        } ;
        context.done(null,response);
    }
};

更新

在进行一些更改后,我尝试让代码在本地运行,并使用文件系统进行操作,它似乎正在工作,不知道如何使用 s3 使其在 Lamba 上同样工作 这是使用文件在本地运行的代码系统

/**
* This will download both the images locally and then merge them 
* We can merge multiple images as we need on a base template specifiying the position as shown in the code
* need to make sure that the graphicsmagick is inatalled 
* sudo apt-get install graphicsmagick
*/
var exec = require('child_process').exec,
    async = require('async'),
    request = require('request'),
    fs = require('fs'),
    gm = require('gm'),
    imageMagick = gm.subClass({ imageMagick: true }),
    userProfileImagePath ='https://fbcdn-profile-a.akamaihd.net...',
    friendProfileImagePath ='https://fbcdn-profile-a.akamaihd.net..';


exports.mergeFile = function(req, context) {
    var errMsg = '',
        userProfileImageName = 'user_profile',
        friendProfileImageName = 'friend_profile',
        mergedImageName = 'final_image',
        destinationBucket = 'testimagemanipulator',
        response = {} ;

    if (errMsg === '') {
        async.auto({
            copyUserImage : function(autoCallback) {
                request({
                    url: userProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }

                    fs.writeFile(__dirname +'/public/images/'+userProfileImageName+'.jpg', body, 'binary', function(err) {
                        if(err) { return autoCallback(err); }

                        return autoCallback();
                    }); 
                });
            },

            copyFriendImage : function(autoCallback) {
                request({
                    url: friendProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }  

                    fs.writeFile(__dirname +'/public/images/'+friendProfileImageName+'.jpg', body, 'binary', function(err) {
                        if(err) { return autoCallback(err); }

                        return autoCallback();
                    });
                });
            },

            mergeImages : ['copyUserImage','copyFriendImage', function(autoCallback,results) {
                console.log('Here');
                gm()
                .in('-page', '+0+0')  // Custom place for each of the images
                .in(__dirname +'/public/images/'+userProfileImageName+'.jpg')
                .in('-page', '+140+50')
                .in(__dirname +'/public/images/'+friendProfileImageName+'.jpg')
                .minify()  // Halves the size, 512x512 -> 256x256
                .mosaic()  // Merges the images as a matrix
                .write(__dirname +'/public/images/'+mergedImageName+'.jpg', function (err) {
                        if (err) { return autoCallback(err); }

                        fs.unlink(__dirname +'/public/images/'+userProfileImageName+'.jpg');
                        fs.unlink(__dirname +'/public/images/'+friendProfileImageName+'.jpg');
                        return autoCallback() ;
                });
            }],
        },
        function(err, results) {
            if (err) {
                response = {
                    "stat":"error",
                    "msg":"Error manipulating the image :: "+err 
                } ;
                console.log(response) ;
            } else {
                response = {
                    "stat":"ok",
                    "imageUrl":__dirname +'/public/images/'+mergedImageName+'.jpg'
                } ;
                console.log(response) ;
            }
        });
    } else {
        response = {
            "stat":"error",
            "msg": errMsg
        } ;
        console.log(response) ;
    }
};

module.exports.mergeFile() ;
4

1 回答 1

2

好的,看起来很难实现我在上面问题中解释的内容。然而,在做了几次尝试和不同的模块之后,没有一个尝试奏效。如前所述,lambda 函数的目的是

  • 获取公共图片网址(facebook)

  • 下载它们并合并在一起,也可以通过图像模板合并

  • 在合并的图像中写入一些文本。

到目前为止,最好的图像处理模块是gm,它可以在 aws lambda 上使用,我们需要确保它是Image Magic

接下来是合并操作可以通过使用 aws/tmp文件夹来完成,图像可以在其中临时放置,合并完成后可以从那里删除。

这是一个链接到 aws REST API 的工作 lambda 脚本

/**
* This is a test script which runs as a lambda function on amazon 
* The lamda function is linked with an amazon end point
* The script will accept a image url (facebook/gravator) etc and will upload to a destination s3 bucket and returns the url 
* The param looks like 
{
    "userProfileImagePath":"https://fbcdn-profile-a.akamaihd.net/....",
    "friendProfileImagePath":"https://fbcdn-profile-a.akamaihd.net/...."
}
*/

var exec = require('child_process').exec,
    async = require('async'),
    request = require('request'),
    gm = require('gm').subClass({ imageMagick: true }),
    fs = require('fs'),
    aws = require('aws-sdk');

exports.handler = function(req, context) {
    var errMsg = '',
        userProfileImageName = 'user_profile',
        friendProfileImageName = 'friend_profile',
        mergedImageName = 'final_image',
        destinationBucket = 'mybucket',
        response = {} ,
        s3 = new aws.S3();

    if (req.userProfileImagePath === '') {
        errMsg = 'Missing the userProfileImage';
    }

    if (req.friendProfileImagePath === '') {
        errMsg = 'Missing the friendProfileImagePath ';
    }

    if (errMsg === '') {
        async.auto({
            copyUserImage : function(autoCallback) {
                request({
                    url: req.userProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }

                    fs.writeFile('/tmp/'+userProfileImageName+'.jpg', body, 'binary', function(err) {
                        if(err) { return autoCallback(err); }

                        return autoCallback();
                    }); 
                });
            },

            copyFriendImage : function(autoCallback) {
                request({
                    url: req.friendProfileImagePath,
                    encoding: null
                }, function(err, res, body) {
                    if (err) { return autoCallback(err); }  

                    fs.writeFile('/tmp/'+friendProfileImageName+'.jpg', body, 'binary', function(err) {
                        if(err) { return autoCallback(err); }

                        return autoCallback();
                    });
                });
            },

            mergeImages : ['copyUserImage','copyFriendImage', function(autoCallback,results) {
                var bgImage = '/tmp/'+userProfileImageName+'.jpg',
                    frontImage = '/tmp/'+friendProfileImageName+'.jpg';

                gm()
                .in('-page', '+0+0')  // Custom place for each of the images
                .in(bgImage)
                .in('-page', '+140+50')
                .in(frontImage)
                .mosaic()  // Merges the images as a matrix
                .font("Arial")
                .fontSize(50)
                .fill('black')
                .drawText(1, 1, 'Hello World', 'Center')
                .fill('blue')
                .drawText(0, 0, 'Hello World', 'Center')
                .write('/tmp/'+mergedImageName+'.jpg', function (err) {
                        if (err) { return autoCallback(err); }

                        var stream = fs.createReadStream('/tmp/'+mergedImageName+'.jpg');
                        var stats = fs.statSync('/tmp/'+mergedImageName+'.jpg');
                        console.log('Merged File size :'+stats['size']);
                        s3.upload({
                            Bucket: destinationBucket,
                            Key: mergedImageName+'.jpg',
                            ContentType: 'image/jpeg',
                            ContentLength: stats['size'],
                            Body: stream, // buffer
                            ACL:'public-read'
                    }, autoCallback);   
                });
            }],
        },
        function(err, results) {
            if (err) {
                response = {
                    "stat":"error",
                    "msg":"Error manipulating the image :: "+err 
                } ;
                context.done(null,response);
            } else {
                response = {
                    "stat":"ok",
                    "imageUrl":"https://domain.amazonaws.com/mybucket/"+mergedImageName+".jpg"
                } ;
                context.done(null,response);
            }
        });
    } else {
        response = {
            "stat":"error",
            "msg": errMsg
        } ;
        context.done(null,response);
    }
};
于 2016-03-21T12:13:06.000 回答