You are here

function imagecache_autorotate_image in ImageCache Actions 6

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

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.

File

./imagecache_autorotate.module, line 91
Autorotate image based on EXIF Orientation tag. http://sylvana.net/jpegcrop/exif_orientation.html

Code

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;
}