You are here

function image_imagemagick_imagecache_autorotate in ImageCache Actions 8

Same name and namespace in other branches
  1. 7 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 178
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;
      }
    }
  }
  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.
    $image->info['width'] = $image->info['height'] = NULL;
  }
  return TRUE;
}