Having trouble using IMAGEMAGICK in PHP via shell_exec? Try putenv()!

Having trouble using IMAGEMAGICK in PHP via shell_exec? Try putenv()!

Some days ago we wanted to use imagemagick via PHP to create preview images from PDF pages.
We were trying some standard modules like pdfpreview.module, pdfthumb.module, pdf_to_imagefield.module which are all based on imagemagick.
As a helper the imagemagick.module was used.

Anyway we had a problem executing the system calls, which are implemented as follows in pdf_to_imagefield.module for example:

  1. watchdog('pdf_to_image', 'Running commandline %command', array('%command' => $command), WATCHDOG_DEBUG);
  2. $response = shell_exec($command);

The calls did not execute correctly, so we first thought the reason may be the imagemagick installation itself or the integration with ghostscript (gd).
But it wasn't. The reason was the base path that was used in the system calls. And the solution is simple: Setting "putenv()" before the "shell_exec()"-Call!

The following trick worked for us in our environment, which can easily be tested. It adds the directories "/usr/local/bin","/usr/bin" and "/bin" as base directories for the calls, so that the imagemagick library can can be called from here.

For pdf_to_imagefield.module the code looks like this:

  1. putenv("PATH=/usr/local/bin:/usr/bin:/bin");
  2. watchdog('pdf_to_image', 'Running commandline %command', array('%command' => $command), WATCHDOG_DEBUG);
  3. $response = shell_exec($command);

But of course we don't want to hack the Drupal Module, which requires us to put the "putenv()" call somewhere else.

This is easy to solve, if the trick works for you. Simply add the required line:

  1. putenv("PATH=/usr/local/bin:/usr/bin:/bin");

to the end of your settings.php
or change your php.ini configuration to include these directories by default.

Hope this helped you and of course I'd be happy about your comments.


Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen