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‎, ‎PDFThumb‎, ‎PDF to ImageField‎ which are all based on imagemagick.
As a helper the ‎ImageMagick‎ was used.

Anyway we had a problem executing the system calls, which are implemented as follows in ‎PDF to ImageField‎ 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‎ 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.

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt. Wenn Sie ein zulässiges Avatar mit Ihrer E-Mail-Adresse verknüpft haben, wird dieses als Avatar verwendet.

Weitere Informationen über Formatierungsoptionen

Aktualisieren Geben Sie die Zeichen ein, die Sie im Bild sehen. Geben Sie die im Bild dargestellten Zeichen ein; wenn Sie diese nicht lesen können, senden Sie das Formular ohne Eingabe ab, um ein neues Bild zu generieren. Groß-/Kleinschreibung wird nicht beachtet.  Switch to audio verification.