You are here

imagecache_autorotate.module in ImageCache Actions 6

Autorotate image based on EXIF Orientation tag. http://sylvana.net/jpegcrop/exif_orientation.html

This mini-module contributed by jonathan_hunt http://drupal.org/user/28976 September 1, 2009

Tweaked by dman to add documentation

File

imagecache_autorotate.module
View source
<?php

/**
 * @file
 * Autorotate image based on EXIF Orientation tag.
 * http://sylvana.net/jpegcrop/exif_orientation.html
 * 
 * This mini-module contributed by jonathan_hunt http://drupal.org/user/28976
 * September 1, 2009
 * 
 * Tweaked by dman to add documentation
 */

/* In Adobe PNGs and TIFF, this information MAY be present in the XMP
 * metadata like so:

 <x:xmpmeta xmlns:x="adobe:ns:meta/">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about="" xmlns:tiff="http://ns.adobe.com/tiff/1.0/">
         <tiff:Orientation>6</tiff:Orientation>
      </rdf:Description>
   </rdf:RDF>
 </x:xmpmeta>

 */

/**
 * Implementation of hook_imagecache_actions.
 *
 * @return array
 *   An array of information on the actions implemented by a module.
 */
function imagecache_autorotate_imagecache_actions() {
  $actions = array(
    'imagecache_autorotate' => array(
      'name' => 'Autorotate',
      'description' => t('Add autorotate image based on EXIF Orientation.'),
    ),
  );
  return $actions;
}

/**
 * Declare dummy form, since we have no settings.
 */
function imagecache_autorotate_form() {
  $form = array();
  $form['help'] = array(
    '#type' => 'markup',
    '#value' => "<p>\n      <b>There are no user-configurable options for this process.</b>\n      </p><p>\n      Certain cameras can embed <em>orientation</em> information into image\n      files when they save them. This information is embedded in an EXIF tag\n      and can be used to rotate images to their correct position for display.\n      </p><p>\n      <em>Not all cameras or images contain this information.</em> \n      This process is only useful for those that do.\n      </p><p>\n      The expected/supported values are\n      <br/><strong>Tag</strong>: <code>0x0112  Orientation</code>\n      </p>\n      <ul>\n      <li>1 = Horizontal (normal)</li>\n      <li>3 = Rotate 180</li>\n      <li>6 = Rotate 90 CW</li>\n      <li>8 = Rotate 270 CW</li>\n      </ul>\n      <p>\n      <a href='http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html'>\nEXIF Reference</a>\n      </p>\n    ",
  );
  return $form;
}

/**
 * Autorotate image based on EXIF Orientation tag.
 * 
 * See code at 
 * http://sylvana.net/jpegcrop/exif_orientation.html
 * 
 * and reference at 
 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
 * 
 * @todo:
 * Add horizontal and vertical flips etc.
 * Need to create sample set for tests.
 */
function imagecache_autorotate_image(&$image, $data) {

  // Test to see if EXIF is supported for image type (e.g. not PNG).
  // @todo: Add mimetype for TIFF also
  if ($image->info['mime_type'] == 'image/jpeg') {
    $exif = exif_read_data($image->source);

    //debug(__FUNCTION__ .': exif[Orientation]='. print_r($exif['Orientation'], TRUE));
    if (!isset($exif['Orientation'])) {
      return TRUE;
    }
    switch ($exif['Orientation']) {
      case 3:
        $degrees = 180;
        break;
      case 6:
        $degrees = 90;

        // Rotate 90 CW
        break;
      case 8:
        $degrees = 270;

        // Rotate 270 CW
        break;
      default:
        $degrees = 0;
    }
    if ($degrees) {

      //debug(__FUNCTION__ .': rotating degrees='. $degrees);
      imageapi_image_rotate($image, $degrees);
    }
  }
  return TRUE;
}

Functions

Namesort descending Description
imagecache_autorotate_form Declare dummy form, since we have no settings.
imagecache_autorotate_image Autorotate image based on EXIF Orientation tag.
imagecache_autorotate_imagecache_actions Implementation of hook_imagecache_actions.