简体   繁体   English

如何在AWS Lambda中将imagick与php一起使用?

[英]How to use imagick with php in aws lambda?

Currently Amazon lambda does supports only node.js and python. 当前,Amazon lambda确实仅支持node.js和python。 I found a official document to run php in lambda. 我找到了在lambda中运行php的官方文档。 link is https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-lambda-running-php-ruby-and-go/ 链接是https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-lambda-running-php-ruby-and-go/

I have successfully ran php in lambda. 我已经在lambda中成功运行了php。 But the problem is I don't know how to use imagick with php. 但是问题是我不知道如何在php中使用imagick。 I have installed imagick on my EC2 with following commands 我已使用以下命令在EC2上安装了imagick

sudo yum install pecl make ImageMagick ImageMagick-devel php-devel gcc re2c
sudo pecl install imagick

running following command returns my imagemagick versions 运行以下命令返回我的imagemagick版本

convert --version

Outputs 输出

Version: ImageMagick 6.7.8-9 2016-06-22 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP 

I am running my php with php binaries in node.js 我正在node.js中使用php二进制文件运行我的php

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

const spawn = require('child_process').spawn;

exports.handler = function(event, context,callback) {

    //var php = spawn('php',['helloLambda.php']); //local debug only
    var php = spawn('php-7-bin/bin/php',['imagick.php']);
    var output = "";

    //send the input event json as string via STDIN to php process
    php.stdin.write(JSON.stringify(event));

    //close the php stream to unblock php process
    php.stdin.end();

    //dynamically collect php output
    php.stdout.on('data', function(data) {
          output+=data;
    });

    //react to potential errors
    php.stderr.on('data', function(data) {
            console.log("STDERR: "+data);
    });

    //finalize when php process is done.
    php.on('close', function(code) {
            //context.succeed(JSON.parse(output));
            callback(null,output);
    });
}

Above PHP is successfully running, Now I'm trying to use imagick with php by replacing var php = spawn('php-7-bin/bin/php',['imagick.php']); 上面的PHP已成功运行,现在我想通过替换var php = spawn('php-7-bin / bin / php',['imagick.php']);将imagick与php一起使用。

imagick.php imagick.php

$image = new Imagick('image.jpg');
$image->thumbnailImage(100, 0);
// Just trying to use imagick function

php.js php.js

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

const spawn = require('child_process').spawn;

exports.handler = function(event, context,callback) {

    //var php = spawn('php',['helloLambda.php']); //local debug only
    var php = spawn('php-7-bin/bin/php',['imagick.php']);
    var output = "";

    //send the input event json as string via STDIN to php process
    php.stdin.write(JSON.stringify(event));

    //close the php stream to unblock php process
    php.stdin.end();

    //dynamically collect php output
    php.stdout.on('data', function(data) {
          output+=data;
    });

    //react to potential errors
    php.stderr.on('data', function(data) {
            console.log("STDERR: "+data);
    });

    //finalize when php process is done.
    php.on('close', function(code) {
            //context.succeed(JSON.parse(output));
            callback(null,output);
    });
}

But it throws following error 但它引发以下错误

"\nFatal error: Uncaught Error: Class 'Imagick' not found in /var/task/imagick.php:5\nStack trace:\n#0 {main}\n  thrown in /var/task/imagick.php on line 5\n"

As stated in the comments above, Imageick is a different than ImageMagick (well is more of a wrapper to access it, I think ). 如上面的评论所述,Imageick与ImageMagick是不同的(我认为它更像是用于访问它的包装器)。 Amazon have a blog that I believe you may have followed. 亚马逊有一个博客 ,我相信您可能会关注。

I am running my php with php binaries in node.js 我正在node.js中使用php二进制文件运行我的php

Which means you're most of the way there. 这意味着您已到达目的地。 From the above blog post, before you go compile php with ./configure --prefix=/home/ec2-user/php-7-bin/ you need to go compile Imagick for your php, so it is bundled with it. 在以上博客文章中,在使用./configure --prefix=/home/ec2-user/php-7-bin/编译php之前,需要为您的php编译Imagick,因此将其捆绑在一起。 But you need to compile it statically so its included in final code. 但是您需要静态地对其进行编译,使其包含在最终代码中。 I believe you may also want to compile ImageMagick statically as well so you don't have to rely on the one installed that it will always be installed. 我相信您可能还希望静态地编译ImageMagick,因此您不必依赖将始终安装的ImageMagick。

I don't have the instructions for both of the above anymore, we went down a different route with Magick.NET and wrote our image manipulation tool in C# instead. 我再也没有上述两个说明,我们使用Magick.NET走了一条不同的路线,改为使用C#编写了图像处理工具。 The methods used in php with Imagick and with Magick.NET were easily convertable but it also depends on the rest of your enviroment. 与Imagick和Magick.NET一起在php中使用的方法很容易转换,但这还取决于您的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM