2

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"
5
  • convert --version checks Imagemagick is installed NOT Imagick. Imagick is a php API for Imagemagick and is not part of Imagemagick - from what I understand it is a class. Commented Apr 29, 2017 at 9:44
  • @Bonzo Yes it is a class of imagemagick. Is there any way to use imagemagic with php in lambda?? Commented Apr 29, 2017 at 9:50
  • I have no idea what Lambda is but I use Imagemagick on a normal server with the php exec( ). There is currently another post about Imagemagick and Lambda here: stackoverflow.com/questions/43622770/… Commented Apr 29, 2017 at 11:58
  • @Bonzo Kindly suggest me where I can find docs for using imagick with exec() in php. Is commands are there for all functions of imagick.? Commented Apr 29, 2017 at 12:05
  • I would suggest you forget about php for this specific use case. You don't need it, Lambda can exec (spawn). @Bonzo Lambda is a service from AWS where your code is auto-deployed inside (docker?) containers that are totally hands-off to you, automatically infinitely scaling, billed in fractional seconds of CPU time for each function call you invoke. There's a system image identical to theirs for compiling/testing binaries -- but everything your code needs to have on the server must be contained/deployed in a single zip file, including the binaries for e.g. imagemagick and even php itself. Commented Apr 29, 2017 at 13:37

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.