简体   繁体   中英

How to use imagick with php in aws lambda?

Currently Amazon lambda does supports only node.js and python. I found a official document to run php in lambda. link is https://aws.amazon.com/blogs/compute/scripting-languages-for-aws-lambda-running-php-ruby-and-go/

I have successfully ran php in lambda. But the problem is I don't know how to use imagick with php. I have installed imagick on my EC2 with following commands

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

running following command returns my imagemagick versions

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

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']);

imagick.php

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

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 ). Amazon have a blog that I believe you may have followed.

I am running my php with php binaries in node.js

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. 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.

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. The methods used in php with Imagick and with Magick.NET were easily convertable but it also depends on the rest of your enviroment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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