[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/imageapi/ -> imageapi_imagemagick.module (source)

   1  <?php  // $Id: imageapi_imagemagick.module,v 1.17.2.5 2009/05/29 17:48:32 drewish Exp $
   2  
   3  /**
   4   * @file
   5   * ImageMagick toolkit functions
   6   */
   7  
   8  /**
   9   * Implementation of hook_imageapi_toolkit().
  10   *
  11   * this hook only gets called to see is a module implements the imageapi hooks...
  12   */
  13  function imageapi_imagemagick_imageapi_toolkit() {
  14  }
  15  
  16  
  17  /**
  18   * Settings form for the toolkit.
  19   */
  20  function imageapi_imagemagick_settings_form() {
  21    $form['imageapi_imagemagick_quality'] = array(
  22      '#type' => 'textfield',
  23      '#title' => t('Compression Quality'),
  24      '#description' => t('Ranges from 0 to 100. Higher values mean better image quality, but bigger files.'),
  25      '#size' => 10,
  26      '#maxlength' => 3,
  27      '#default_value' => variable_get('imageapi_imagemagick_quality', 75),
  28      '#field_suffix' => '%',
  29      '#element_validate' => array('imageapi_imagemagick_quality_element_validate'),
  30    );
  31  
  32    $form['imageapi_imagemagick_binary'] = array(
  33      '#type' => 'fieldset',
  34      '#title' => t('ImageMagick Binary'),
  35      '#collapsible' => FALSE,
  36      '#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.'),
  37    );
  38  
  39    $form['imageapi_imagemagick_binary']['version'] = array('#weight' => -1);
  40    $form['imageapi_imagemagick_binary']['#after_build'] = array('_imageapi_imagemagick_build_version');
  41  
  42    $form['imageapi_imagemagick_binary']['imageapi_imagemagick_convert'] = array(
  43      '#type' => 'textfield',
  44      '#title' => t('Path to the "convert" binary'),
  45      '#default_value' => variable_get('imageapi_imagemagick_convert', '/usr/bin/convert'),
  46      '#required' => TRUE,
  47      '#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>'),
  48      '#element_validate' => array('imageapi_imagemagick_validate_path'),
  49    );
  50  
  51    $form['imageapi_imagemagick_binary']['imageapi_imagemagick_debugging'] = array(
  52      '#type' => 'checkbox',
  53      '#title' => t('Display debugging information'),
  54      '#default_value' => variable_get('imageapi_imagemagick_debugging', 0),
  55      '#description' => t('Checking this option will display the ImageMagick commands and output to users with the <em>administer site configuration</em> permission.'),
  56      '#weight' => 2,
  57    );
  58  
  59    return system_settings_form($form);
  60  }
  61  
  62  function _imageapi_imagemagick_build_version($form_element, $form_state) {
  63    // make sure path is set and valid before running after build.
  64    if ($path_errors = _imageapi_imagemagick_check_path($form_state['values']['imageapi_imagemagick_convert'])) {
  65      // check here is primarily for pre-existing bad settings...
  66      // the #validate should prevent users from adding bad paths.
  67      $form_element['imageapi_imagemagick_binary'] = array(
  68        '#value' => '<p class="error">'. implode('<br />', $path_errors) .'</p>',
  69      );
  70    }
  71    else {
  72      _imageapi_imagemagick_convert_exec('-version', $output, $errors);
  73      $form_element['imageapi_imagemagick_binary']['version'] = array(
  74        '#title' => t('Version information'),
  75        '#value' => '<pre>'. check_plain(trim($output)) .'</pre>',
  76        '#description' => t('The ImageMagick <kbd>convert</kbd> binary was located and return this version information.'),
  77      );
  78    }
  79    $form_element['imageapi_imagemagick_binary']['version']['#type'] = 'item';
  80    $form_element['imageapi_imagemagick_binary']['version']['#weight'] = -1;
  81    return $form_element;
  82  }
  83  
  84  function imageapi_imagemagick_validate_path($element) {
  85    if ($errors = _imageapi_imagemagick_check_path($element['#value'])) {
  86      form_set_error($element['#parents'][0], implode('<br />', $errors));
  87      //form_set_value($element['#parents'][0], variable_get('imageapi_imagemagick_convert', '/usr/bin/convert'));
  88      return FALSE;
  89    }
  90    return TRUE;
  91  }
  92  
  93  function imageapi_imagemagick_quality_element_validate($element) {
  94    if ($element['#value'] < 0 || $element['#value'] > 100) {
  95      form_set_error($element['#name'], t('Compression Quality must be a value between 0 and 100.'));
  96    }
  97  }
  98  
  99  function imageapi_imagemagick_image_open($image) {
 100    $image->ops = array();
 101    return $image;
 102  }
 103  
 104  function imageapi_imagemagick_image_close($image, $dst) {
 105    return _imageapi_imagemagick_convert($image->source, $dst, $image->ops);
 106  }
 107  
 108  function imageapi_imagemagick_image_crop(&$image, $x, $y, $width, $height) {
 109    $image->ops[]  = '-crop '. (int) $width .'x'. (int) $height .'+'. (int) $x .'+'. (int) $y .'!';
 110    $image->info['width'] = $width;
 111    $image->info['height'] = $height;
 112    return TRUE;
 113  }
 114  
 115  function imageapi_imagemagick_image_resize(&$image, $width, $height) {
 116    $image->ops[] = '-resize '. (int) $width .'x'. (int) $height .'!';
 117    $image->info['width'] = $width;
 118    $image->info['height'] = $height;
 119    return TRUE;
 120  }
 121  
 122  function imageapi_imagemagick_image_rotate(&$image, $degrees, $bgcolor) {
 123    if (is_int($bgcolor)) {
 124      $bgcolor = '#'. str_pad(dechex($bgcolor), 6, 0);
 125    }
 126    else {
 127      $bgcolor = str_replace('0x', '#', $bgcolor);
 128    }
 129    $image->ops[] = '-background '. escapeshellarg($bgcolor) .' -rotate '. (float) $degrees;
 130    return TRUE;
 131  }
 132  
 133  function imageapi_imagemagick_image_sharpen(&$image, $radius, $sigma, $amount, $threshold) {
 134    $unsharp_arg = $radius .'x'. $sigma .'+'. $amount/100 .'+'. $threshold;
 135    $image->ops[] = '-unsharp '. $unsharp_arg;
 136    return TRUE;
 137  }
 138  
 139  function imageapi_imagemagick_image_desaturate(&$image) {
 140    $image->ops[] = '-colorspace GRAY';
 141    return TRUE;
 142  }
 143  
 144  /**
 145   * Calls the convert executable with the specified filter.
 146   */
 147  function _imageapi_imagemagick_convert($source, $dest, $args) {
 148    $args['quality'] = '-quality '. escapeshellarg(variable_get('imageapi_imagemagick_quality', 75));
 149    // To make use of ImageMagick 6's parenthetical command grouping we need to make
 150    // the $source image the first parameter and $dest the last.
 151    // See http://www.imagemagick.org/Usage/basics/#cmdline for more info.
 152    $command = escapeshellarg($source) .' '. implode(' ', $args) .' '. escapeshellarg($dest);
 153  
 154    if (0 != _imageapi_imagemagick_convert_exec($command, $output, $errors)) {
 155      return FALSE;
 156    }
 157    return file_exists($dest);
 158  }
 159  
 160  function _imageapi_imagemagick_check_path($path) {
 161    $errors = array();
 162    if (!is_file($path)) {
 163      $errors[] = t('The specified ImageMagick path %file does not exist.', array('%file' => $path));
 164    }
 165    if (!$errors && !is_executable($path)) {
 166      $errors[] = t('The specified ImageMagick path %file is not executable.', array('%file' => $path));
 167    }
 168    if ($errors && $open_basedir = ini_get('open_basedir')) {
 169      $errors[] = t('PHP\'s <a href="!open-basedir">open_basedir</a> security restriction is set to %open-basedir, which may be interfering with attempts to locate ImageMagick.', array('%file' => $path, '%open-basedir' => $open_basedir, '!info-link' => url('http://php.net/features.safe-mode#ini.open-basedir')));
 170    }
 171    return $errors;
 172  }
 173  
 174  function _imageapi_imagemagick_convert_exec($command_args, &$output, &$errors) {
 175    $convert_path = variable_get('imageapi_imagemagick_convert', '/usr/bin/convert');
 176    if ($errors = _imageapi_imagemagick_check_path($convert_path)) {
 177      watchdog('imageapi imagemagick', '!errors', array('!errors' => implode('<br />', $errors)), WATCHDOG_ERROR);
 178      return FALSE;
 179    }
 180  
 181    // Specify Drupal's root as the working a working directory so that relative
 182    // paths are interpreted correctly.
 183    $drupal_path = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
 184  
 185    if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
 186      // Use Window's start command to avoid the "black window" from showing up:
 187      // http://us3.php.net/manual/en/function.exec.php#56599
 188      // Use /D to run the command from PHP's current working directory so the
 189      // file paths don't have to be absolute.
 190      $convert_path = 'start "window title" /D'. escapeshellarg($drupal_path) .' /B '. escapeshellarg($convert_path);
 191    }
 192  
 193    $descriptors = array(
 194      0 => array('pipe', 'r'), // stdin
 195      1 => array('pipe', 'w'), // stdout
 196      2 => array('pipe', 'w')  // stderr
 197    );
 198    if ($h = proc_open($convert_path .' '. $command_args, $descriptors, $pipes, $drupal_path)) {
 199      $output = '';
 200      while (!feof($pipes[1])) {
 201        $output .= fgets($pipes[1]);
 202      }
 203  
 204      $errors = '';
 205      while (!feof($pipes[2])) {
 206        $errors .= fgets($pipes[2]);
 207      }
 208  
 209      // Display debugging information to authorized users.
 210      if (variable_get('imageapi_imagemagick_debugging', FALSE) && user_access('administer site configuration')) {
 211        drupal_set_message(t('ImageMagick command: @command', array('@command' => $convert_path .' '. $command_args)));
 212        drupal_set_message(t('ImageMagick output: @output', array('@output' => $output)));
 213      }
 214  
 215      if ($errors) {
 216        drupal_set_message(t('ImageMagick reported an error: %error', array('%error' => $errors)), 'error');
 217      }
 218  
 219      fclose($pipes[0]);
 220      fclose($pipes[1]);
 221      fclose($pipes[2]);
 222      return proc_close($h);
 223    }
 224    return FALSE;
 225  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7