View source
<?php
function image_imagemagick_info() {
return array(
'name' => 'imagemagick',
'title' => 'ImageMagick Toolkit.',
);
}
function image_imagemagick_settings() {
$form['#after_build'] = array(
'_image_imagemagick_build_version',
);
$form['imagemagick_binary'] = array(
'#type' => 'fieldset',
'#title' => t('ImageMagick Binary'),
'#collapsible' => FALSE,
'#description' => t('ImageMagick is a standalone program used to manipulate images. To use it, it must be installed on your server and you need to know where it is located. If you are unsure of the exact path consult your ISP or server administrator.'),
);
$form['imagemagick_binary']['image_imagemagick_convert'] = array(
'#type' => 'textfield',
'#title' => t('Path to the "convert" binary'),
'#default_value' => variable_get('image_imagemagick_convert', '/usr/bin/convert'),
'#required' => TRUE,
'#description' => t('Specify the complete path to the ImageMagic <kbd>convert</kbd> binary. For example: <kbd>/usr/bin/convert</kbd> or <kbd>C:\\Program Files\\ImageMagick-6.3.4-Q16\\convert.exe</kbd>'),
);
$form['imagemagick_binary']['image_imagemagick_debugging'] = array(
'#type' => 'checkbox',
'#title' => t('Display debugging information'),
'#default_value' => variable_get('image_imagemagick_debugging', 0),
'#description' => t('Checking this option will display the ImageMagick commands and ouput to users with the <em>administer site configuration</em> permission.'),
);
return $form;
}
function _image_imagemagick_build_version($form, $form_element) {
if (!isset($form['#post']['image_toolkit']) || $form['#post']['image_toolkit'] == 'imagemagick') {
$valid = _image_imagemagick_check_path($form_element['image_imagemagick_convert'], 'image_imagemagick_convert');
if ($valid) {
_image_imagemagick_convert_exec('-version', $output, $errors);
$form['imagemagick_binary']['version'] = array(
'#type' => 'item',
'#value' => '<pre>' . check_plain(trim($output)) . '</pre>',
);
}
}
return $form;
}
function _image_imagemagick_check_path($path, $attach_error_to = FALSE) {
if (is_file($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 resriction is set to %open-basedir, which may be interfering with the attempts to locate ImageMagick.", array(
'%file' => $path,
'%open-basedir' => $open_basedir,
'!info-link' => url('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;
}
function _image_imagemagick_alter_invoke($op, $filepath, $args) {
foreach (module_implements('imagemagick_alter') as $module) {
$function = $module . '_imagemagick_alter';
$function($op, $filepath, $args);
}
return $args;
}
function image_imagemagick_resize($source, $dest, $width, $height) {
$args = array(
'resize' => '-resize ' . (int) $width . 'x' . (int) $height . '!',
);
$args = _image_imagemagick_alter_invoke('resize', $source, $args);
return _image_imagemagick_convert($source, $dest, $args);
}
function image_imagemagick_rotate($source, $dest, $degrees, $bg_color = 0x0) {
$args = array(
'rotate' => '-rotate ' . (double) $degrees,
'background' => '-background #' . str_pad(dechex($bg_color), 6, 0),
);
$args = _image_imagemagick_alter_invoke('rotate', $source, $args);
return _image_imagemagick_convert($source, $dest, $args);
}
function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
$args = array(
'crop' => '-crop ' . (int) $width . 'x' . (int) $height . '+' . (int) $x . '+' . (int) $y . '!',
);
$args = _image_imagemagick_alter_invoke('crop', $source, $args);
return _image_imagemagick_convert($source, $dest, $args);
}
function _image_imagemagick_convert($source, $dest, $args) {
$command = implode(' ', array(
preg_replace("/[^A-Za-z0-9\\!\\.\\-\\+\\_\\/ ]/", '', implode(' ', $args)),
escapeshellarg($source),
escapeshellarg($dest),
));
if (0 != _image_imagemagick_convert_exec($command, $output, $errors)) {
return FALSE;
}
return file_exists($dest);
}
function _image_imagemagick_convert_exec($command_args, &$output, &$errors) {
$convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert');
if (!_image_imagemagick_check_path($convert_path)) {
drupal_set_message(t("ImageMagick could not be found. The admin will need to set the path on the <a href='@image-toolkit-settings'>image toolkit page</a>.", array(
'@image-toolkit-settings' => url('admin/settings/image-toolkit'),
)), 'error');
return FALSE;
}
if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
$convert_path = 'start "window title" /D' . escapeshellarg(getcwd()) . ' /B ' . escapeshellarg($convert_path);
}
$descriptors = array(
0 => array(
'pipe',
'r',
),
1 => array(
'pipe',
'w',
),
2 => array(
'pipe',
'w',
),
);
if ($h = proc_open($convert_path . ' ' . $command_args, $descriptors, $pipes)) {
$output = '';
while (!feof($pipes[1])) {
$output .= fgets($pipes[1]);
}
$errors = '';
while (!feof($pipes[2])) {
$errors .= fgets($pipes[2]);
}
if (variable_get('image_imagemagick_debugging', FALSE) && user_access('administer site configuration')) {
drupal_set_message(t("ImageMagick command: @command", array(
'@command' => $convert_path . ' ' . $command_args,
)));
drupal_set_message(t("ImageMagick output: @output", array(
'@output' => $output,
)));
}
if ($errors) {
drupal_set_message(t("ImageMagick reported an error: %error", array(
'%error' => $errors,
)), 'error');
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
return proc_close($h);
}
return FALSE;
}