You are here

function image_im_advanced_imagemagick_alter in Image 7

Same name and namespace in other branches
  1. 5.2 contrib/image_im_advanced/image_im_advanced.module \image_im_advanced_imagemagick_alter()
  2. 5 contrib/image_im_advanced/image_im_advanced.module \image_im_advanced_imagemagick_alter()
  3. 6 contrib/image_im_advanced/image_im_advanced.module \image_im_advanced_imagemagick_alter()

Implementation of hook_imagemagick_alter().

File

contrib/image_im_advanced/image_im_advanced.module, line 146

Code

function image_im_advanced_imagemagick_alter($op, $filepath, &$args) {
  $options = image_im_advanced_options();
  $image = image_get_info($filepath);
  switch ($op) {
    case 'resize':

      // Examine the 'resize' argument to determine the new target size.
      $size = preg_replace('/[^\\d]*(\\d+x\\d+)[^\\d]*/', '$1', $args['resize']);
      list($width, $height) = explode('x', $size);

      // Add sharpening filter.
      if ($options['unsharp']['amount'] && $options['unsharp']['radius']) {

        // 0.7 and 0.02 are reasonable values for Sigma and Threshold.
        $args['unsharp'] = '-unsharp ' . $options['unsharp']['radius'] . 'x0.7+' . round($options['unsharp']['amount'] / 100, 2) . '+0.02';
      }
      break;
    case 'crop':

      // Examine the 'crop' argument to determine the new target size.
      $size = preg_replace('/[^\\d]*(\\d+x\\d+)[^\\d]*/', '$1', $args['crop']);
      list($width, $height) = explode('x', $size);
      break;
    case 'rotate':

      // For lack of a better guess, use the current image size.
      $width = $image['width'];
      $height = $image['height'];
      break;
  }

  // Convert to specified color profile.
  if (!empty($options['profile']['path']) && is_readable($options['profile']['path'])) {
    $args['profile'] = '-profile ' . $options['profile']['path'];
  }

  // Assign a color space. Skip this if a color profile has been provided,
  // as it will be more accurate.
  if ($options['colorspace'] && !isset($args['profile'])) {
    $args['colorspace'] = ' -colorspace ' . $options['colorspace'];
  }

  // Determine if the -strip parameter should be used.
  list($strip_width, $strip_height) = explode('x', $options['strip']);
  if ((int) $width <= (int) $strip_width && (int) $height <= (int) $strip_height) {
    $args['strip'] = '-strip';
  }

  // Set JPEG quality.
  if ($image['mime_type'] == 'image/jpeg') {
    if (empty($args['quality']) && $options['jpeg_quality']) {
      $args['quality'] = '-quality ' . $options['jpeg_quality'];
    }
  }

  // Change image density (this doesn't affect the image dimensions/resolution).
  if ($options['density']) {
    $args['density'] = '-density 72 -units PixelsPerInch';
  }
}