Fragmented Thought

Image Magick detection bug in Drupal 6

By

Published:

Lance Gliser

Heads up! This content is more than six months old. Take some time to verify everything still works as expected.

The main details of this error can be read in more detail in the original post I found this fix on. I'll summarize here as well. While Drupal has access to execute Image Magick's convert function in an area outside php's open_basedir security restriction, it cannot open it. The imageapi_imagemagick.module's functionality check doesn't test first for execution, it tries to open the file. Leading to a working Image Magick install to appear faulty. The fix was listed here. As of yet, there's been not application of this to the module itself, so remember to refix every time you update. The following code should be placed at the top of the _imageapi_imagemagick_check_path function.

A copy of the code, in case someone needs it.

$errors = array(); // This line is already at the top of the function. Including for reference // Try it out and return without errors on success $handle = popen($path . " -version", 'r'); $im_version = fread($handle, 2096); pclose($handle); if (strpos($im_version, 'ImageMagick') !== FALSE) { return $errors; }

Updated 2010.11.12: And in other news today:

It seems requested image file in the image module image.imagemagick.inc also suffers this bug. Here's the updated function for that, and a link to the image issue with [patch](http://drupal.org/node/970326.

function _image_imagemagick_check_path($path, $attach_error_to = FALSE) { // Try it out and return without errors on success $handle = popen($path . " -version", 'r'); $im_version = fread($handle, 2096); pclose($handle); if (strpos($im_version, 'ImageMagick') !== FALSE) { return TRUE; } if (file_exists($path)) { return TRUE; } if ($attach_error_to) { if ($open_basedir = ini_get('open_basedir')) { form_set_error($attach_error_to, t("No file %file could be found. PHP's <a href='@open_basedir'>open_basedir</a> security restriction is set to %open_basedir, which may be interfering with the attempts to locate ImageMagick.", array('%file' => $path, '%open_basedir' => $open_basedir, '@open_basedir' => 'http://php.net/features.safe-mode#ini.open-basedir'))); } else { form_set_error($attach_error_to, t('The specified ImageMagic path %file does not exist.', array('%file' => $path))); } } return FALSE; } And the contributed module in the image module for image_im_advanced requires a patch as well. It tries to open the file too. This updated function forces it to rely on the image module's detection function instead. function image_im_advanced_requirements($phase) { $requirements = array(); if ($phase == 'runtime') { // If the image.imagemagick.inc file is not in the includes directory, // indicate an error. $toolkits = image_get_available_toolkits(); $requirements['imagemagick'] = array('title' => t('ImageMagick advanced options')); if (!isset($toolkits['imagemagick'])) { $requirements['imagemagick']['value'] = t('ImageMagick image toolkit is not properly installed'); $requirements['imagemagick']['description'] = t('The %toolkit_inc file must be copied to %inc_dir in order for the ImageMagick image toolkit to function.', array('%toolkit_inc' => drupal_get_path('module', 'image') . '/image.imagemagick.inc', '%inc_dir' => 'includes')); $requirements['imagemagick']['severity'] = REQUIREMENT_ERROR; return $requirements; } // If the image_im_advanced module is installed but the ImageMagick image // toolkit is not selected, indicate an error. if (image_get_toolkit() != 'imagemagick') { $requirements['imagemagick']['value'] = t('ImageMagic image toolkit is not selected.'); $requirements['imagemagick']['description'] = t('The ImageMagic image toolkit is not selected. The advanced options only apply to the ImageMagick toolkit. Select the image toolkit <a href="@toolkit">here</a>.', array('@toolkit' => url('admin/settings/image-toolkit'))); $requirements['imagemagick']['severity'] = REQUIREMENT_WARNING; return $requirements; } // If the imagemagick convert utility does not exist, indicate an error. $convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert'); if (!_image_imagemagick_check_path($convert_path)) { $requirements['imagemagick']['value'] = t('ImageMagick convert utility not found.'); $requirements['imagemagick']['description'] = t('The ImageMagick image convert utility (@convert) does not exist. If you have ImageMagick installed, click <a href="@toolkit">here</a> to provide the path to the convert utility.', array('@convert' => $convert_path, '@toolkit' => url('admin/settings/image-toolkit'))); $requirements['imagemagick']['severity'] = REQUIREMENT_ERROR; return $requirements; } $requirements['imagemagick']['value'] = t('ImageMagick image toolkit is selected.'); $requirements['imagemagick']['severity'] = REQUIREMENT_OK; } return $requirements; }