You are here

function image_imagemagick_imagecache_autorotate in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 autorotate/imagecache_autorotate.module \image_imagemagick_imagecache_autorotate()

Imagemagick toolkit specific implementation of this image effect.

Parameters

stdClass $image: An image object.

Return value

bool true on success, false otherwise.

See also

http://www.imagemagick.org/script/command-line-options.php#auto-orient

File

autorotate/imagecache_autorotate.module, line 203
Autorotate image based on EXIF Orientation tag.

Code

function image_imagemagick_imagecache_autorotate(stdClass $image) {

  // Use the exif extension, if enabled, to figure out the new dimensions.
  // Moreover (see [#2366163]): to prevent a bug in IM to incorrectly rotate the
  // image when it should not, we only pass the auto-orient argument when the
  // exif extension could detect the 'Orientation' tag.
  if (function_exists('exif_read_data')) {
    $exif = @exif_read_data(drupal_realpath($image->source));
    if (isset($exif['Orientation'])) {
      switch ($exif['Orientation']) {
        case 1:

          // Normal orientation: no need to rotate or to change the dimensions.
          break;
        case 5:
        case 6:
        case 7:
        case 8:

          // 90 or 270 degrees rotation (+ optional mirror): swap dimensions.
          $image->ops[] = '-auto-orient';
          $tmp = $image->info['width'];
          $image->info['width'] = $image->info['height'];
          $image->info['height'] = $tmp;
          break;
        default:

          // All other orientations: pass the arguments, but the dimensions
          // remain the same.
          $image->ops[] = '-auto-orient';
          break;
      }
    }
    elseif ($exif === FALSE && $image->extension === 'jpg') {
      watchdog('imagecache_actions', 'Image %file could not be auto-rotated: !message', array(
        '%file' => $image->source,
        '!message' => t('The exif_read_data() function returned FALSE.'),
      ));
    }
  }
  else {

    // We do add the auto-orient argument to IM. IM will determine itself
    // whether to rotate or not.
    $image->ops[] = '-auto-orient';

    // However we cannot keep track of the dimensions anymore.
    if ($image->info['width'] !== $image->info['height']) {
      $image->info['width'] = $image->info['height'] = NULL;
    }
  }
  return TRUE;
}