[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/image/contrib/image_im_advanced/ -> image_im_advanced.module (source)

   1  <?php
   2  // $Id: image_im_advanced.module,v 1.7.2.1 2010/08/03 17:43:00 sun Exp $
   3  
   4  /**
   5   * Retrieve the settings array.
   6   */
   7  function image_im_advanced_options() {
   8    $defaults = array(
   9      'jpeg_quality' => 75,
  10      'strip' => '150x150',
  11      'colorspace' => 'rgb',
  12      'density' => 1,
  13      'unsharp' => array(
  14        'radius' => 0.9,
  15        'amount' => 0,
  16      ),
  17      'profile' => array('path' => ''),
  18    );
  19    return variable_get('image_im_advanced_options', $defaults);
  20  }
  21  
  22  /**
  23   * Implementation of hook_form_alter().
  24   *
  25   * Add options to the Image toolkit settings form.
  26   */
  27  function image_im_advanced_form_alter(&$form, $form_state, $form_id) {
  28    if ($form_id == 'system_image_toolkit_settings' && 'imagemagick' == image_get_toolkit()) {
  29      $options = image_im_advanced_options();
  30  
  31      $form['image_im_advanced_options'] = array(
  32        '#type' => 'fieldset',
  33        '#title' => t('ImageMagick advanced options'),
  34        '#collapsible' => FALSE,
  35        '#description' => t("These settings let you control some of ImageMagick's more advanced options."),
  36        '#element_validate' => array('image_im_advanced_settings_validate'),
  37        '#tree' => TRUE,
  38      );
  39      $form['image_im_advanced_options']['jpeg_quality'] = array(
  40        '#type' => 'textfield',
  41        '#title' => t('JPEG quality'),
  42        '#size' => 10,
  43        '#maxlength' => 3,
  44        '#default_value' => $options['jpeg_quality'],
  45        '#field_suffix' => t('%'),
  46        '#description' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files. <a href="@link">More information on -quality</a>.', array('@link' => 'http://www.imagemagick.org/script/command-line-options.php#quality')),
  47      );
  48      $form['image_im_advanced_options']['strip'] = array(
  49        '#type' => 'textfield',
  50        '#title' => t('Strip metadata from images at this size and below'),
  51        '#default_value' => $options['strip'],
  52        '#description' => t('You may choose to strip all metadata, such as camera information and color profiles, from the processed images in order to reduce their file size. Please choose at what maximum size you want images to be stripped of their metadata. Example: "150x150". Enter "0x0" to disable this feature. This option requires ImageMagick 6.0.0 or higher. <a href="@link">More information on -strip</a>.', array('@link' => 'http://www.imagemagick.org/script/command-line-options.php#strip')),
  53      );
  54      $form['image_im_advanced_options']['colorspace'] = array(
  55        '#type' => 'select',
  56        '#title' => t('Convert colorspace'),
  57        '#default_value' => $options['colorspace'],
  58        '#options' => array(0 => t('- None -'), 'RGB' => t('RGB'), 'GRAY' => t('Gray')),
  59        '#description' => t('This option lets you convert images to the specified colorspace. This will be overridden by the Color profile option, if used. <a href="@link">More information on -colorspace</a>.', array('@link' => 'http://www.imagemagick.org/script/command-line-options.php#colorspace')),
  60      );
  61      $form['image_im_advanced_options']['density'] = array(
  62        '#type' => 'checkbox',
  63        '#title' => t('Change image resolution to 72 ppi'),
  64        '#default_value' => $options['density'],
  65        '#description' => t('If checked, this option will set the print resolution of the image to 72 pixels per inch, which is suitable for web use. This does not affect the pixel size or quality of the image. <a href="@link">More information on -density</a>.', array('@link' => 'http://www.imagemagick.org/script/command-line-options.php#density')),
  66      );
  67  
  68      $form['image_im_advanced_options']['unsharp'] = array(
  69        '#type' => 'fieldset',
  70        '#title' => t('Sharpening filter'),
  71        '#collapsible' => TRUE,
  72        '#collapsed' => ($options['unsharp']['amount'] == 0),
  73        '#description' => t('The sharpness filter is used to regain some of the sharpness that is always lost when a digital photograph is scaled down. This is equivalent to the commonly used "Unsharp Mask" filter. It is important that these values are not set too high as it can easily make the images look artificial. <a href="@link">More information on -unsharp</a>.', array('@link' => 'http://www.imagemagick.org/script/command-line-options.php#unsharp')),
  74      );
  75      $form['image_im_advanced_options']['unsharp']['amount'] = array(
  76        '#type' => 'textfield',
  77        '#title' => t('Sharpness filter strength'),
  78        '#size' => 4,
  79        '#maxlength' => 3,
  80        '#default_value' => $options['unsharp']['amount'],
  81        '#field_suffix' => t('%'),
  82        '#description' => t('Apply this percentage of sharpness when scaling. 90 is recommended, although values higher than 100 are also valid. Set to 0 to disable this feature.'),
  83      );
  84      $form['image_im_advanced_options']['unsharp']['radius'] = array(
  85        '#type' => 'textfield',
  86        '#title' => t('Sharpness filter radius'),
  87        '#default_value' => $options['unsharp']['radius'],
  88        '#size' => 4,
  89        '#maxlength' => 4,
  90        '#description' => t('Use this pixel radius for the sharpness filter. 0.9 is recommended.'),
  91      );
  92  
  93      $form['image_im_advanced_options']['profile'] = array(
  94        '#type' => 'fieldset',
  95        '#title' => t('Color profile'),
  96        '#collapsible' => TRUE,
  97        '#collapsed' => empty($options['profile']['path']),
  98        '#description' => t('Processed images may be converted to a color profile specified here. This is especially important when working with images that use a wide-gamut color profile such as ColorMatch or Adobe RGB, which is often the case with professional photography. sRGB (which may be downloaded from <a href="http://www.color.org/profiles.html">ICC</a>) is recommended since it is likely to look good on most displays.<br />Note that this conversion is still useful even if you choose to strip all metadata from your images (see above). This is because the conversion happens first and changes the actual image data before the profile is stripped.'),
  99      );
 100      $form['image_im_advanced_options']['profile']['path'] = array(
 101        '#type' => 'textfield',
 102        '#title' => t('Path to color profile'),
 103        '#default_value' => $options['profile']['path'],
 104        '#description' => t('The path to a color profile file that all scaled down images will be converted to. Leave empty to disable.'),
 105      );
 106  
 107      // Move the buttons below our additions.
 108      $form['buttons']['#weight'] = 10;
 109    }
 110  }
 111  
 112  /**
 113   * Validate settings form.
 114   */
 115  function image_im_advanced_settings_validate($element, &$form_state) {
 116    $options = $form_state['values']['image_im_advanced_options'];
 117  
 118    // Check that the JPEG quality is a valid number.
 119    if (!is_numeric($options['jpeg_quality']) || $options['jpeg_quality'] < 1 || $options['jpeg_quality'] > 100) {
 120      form_set_error('image_im_advanced_options][jpeg_quality', t('The JPEG quality must be a positive number between 1 and 100.'));
 121    }
 122  
 123    // Check that the strip dimensions are valid.
 124    if (!preg_match('/^\d+x\d+$/', $options['strip'])) {
 125      form_set_error('image_im_advanced_options][strip', t('The strip metadata threshold must be specified in the form <em>NxN</em>. Example: <em>150x150</em>.'));
 126    }
 127  
 128    // Check the unsharp mask values.
 129    if (!empty($options['unsharp']['amount'])) {
 130      if (!is_numeric($options['unsharp']['amount']) || $options['unsharp']['amount'] < 0) {
 131        form_set_error('image_im_advanced_options][unsharp][amount', t('The sharpness amount must be specified as a positive number.'));
 132      }
 133      if (!is_numeric($options['unsharp']['radius']) || $options['unsharp']['radius'] < 0) {
 134        form_set_error('image_im_advanced_options][unsharp][radius', t('The sharpness radius must be specified as a positive value.'));
 135      }
 136    }
 137  
 138    // Check that if the color profile is provided that it's a readable file.
 139    if (!empty($options['profile']['path']) && (!file_exists($options['profile']['path']) || !is_readable($options['profile']['path']))) {
 140      form_set_error('image_im_advanced_options][profile][path', t('The ICC profile could not be read.'));
 141    }
 142  }
 143  
 144  /**
 145   * Implementation of hook_imagemagick_alter().
 146   */
 147  function image_im_advanced_imagemagick_alter($op, $filepath, &$args) {
 148    $options = image_im_advanced_options();
 149    $image = image_get_info($filepath);
 150  
 151    switch ($op) {
 152      case 'resize':
 153        // Examine the 'resize' argument to determine the new target size.
 154        $size = preg_replace('/[^\d]*(\d+x\d+)[^\d]*/', '$1', $args['resize']);
 155        list($width, $height) = explode('x', $size);
 156  
 157        // Add sharpening filter.
 158        if ($options['unsharp']['amount'] && $options['unsharp']['radius']) {
 159          // 0.7 and 0.02 are reasonable values for Sigma and Threshold.
 160          $args['unsharp'] = '-unsharp ' . $options['unsharp']['radius'] . 'x0.7+' . round($options['unsharp']['amount'] / 100, 2) . '+0.02';
 161        }
 162        break;
 163  
 164      case 'crop':
 165        // Examine the 'crop' argument to determine the new target size.
 166        $size = preg_replace('/[^\d]*(\d+x\d+)[^\d]*/', '$1', $args['crop']);
 167        list($width, $height) = explode('x', $size);
 168        break;
 169  
 170      case 'rotate':
 171        // For lack of a better guess, use the current image size.
 172        $width = $image['width'];
 173        $height = $image['height'];
 174        break;
 175    }
 176  
 177    // Convert to specified color profile.
 178    if (!empty($options['profile']['path']) && is_readable($options['profile']['path'])) {
 179      $args['profile'] = '-profile ' . $options['profile']['path'];
 180    }
 181  
 182    // Assign a color space. Skip this if a color profile has been provided,
 183    // as it will be more accurate.
 184    if ($options['colorspace'] && !isset($args['profile'])) {
 185      $args['colorspace'] = ' -colorspace ' . $options['colorspace'];
 186    }
 187  
 188    // Determine if the -strip parameter should be used.
 189    list($strip_width, $strip_height) = explode('x', $options['strip']);
 190    if ((int) $width <= (int) $strip_width && (int) $height <= (int) $strip_height) {
 191      $args['strip'] = '-strip';
 192    }
 193  
 194    // Set JPEG quality.
 195    if ($image['mime_type'] == 'image/jpeg') {
 196      if (empty($args['quality']) && $options['jpeg_quality']) {
 197        $args['quality'] = '-quality ' . $options['jpeg_quality'];
 198      }
 199    }
 200  
 201    // Change image density (this doesn't affect the image dimensions/resolution).
 202    if ($options['density']) {
 203      $args['density'] = '-density 72 -units PixelsPerInch';
 204    }
 205  }
 206  


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