Saturday 29 April 2017

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 imagick 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"



via phpnerd

No comments:

Post a Comment