123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?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>
- */
- function imagecache_autorotate_image_effect_info() {
- $effects = array();
- $effects['imagecache_autorotate'] = array(
- 'label' => t('Autorotate'),
- 'help' => t('Add autorotate image based on EXIF Orientation.'),
- 'effect callback' => 'imagecache_autorotate_image',
- 'dimensions callback' => 'imagecache_autorotate_dimensions',
- );
- return $effects;
- }
- /**
- * Declare dummy form, since we have no settings.
- *
- * @todo: A form is no longer needed, if this information still needs to be
- * displayed it should probably be moved to help.
- */
- function imagecache_autorotate_form() {
- $form = array();
- $form['help'] = array(
- '#type' => 'markup',
- '#value' => "<p>
- <b>There are no user-configurable options for this process.</b>
- </p><p>
- Certain cameras can embed <em>orientation</em> information into image
- files when they save them. This information is embedded in an EXIF tag
- and can be used to rotate images to their correct position for display.
- </p><p>
- <em>Not all cameras or images contain this information.</em>
- This process is only useful for those that do.
- </p><p>
- The expected/supported values are
- <br/><strong>Tag</strong>: <code>0x0112 Orientation</code>
- </p>
- <ul>
- <li>1 = Horizontal (normal)</li>
- <li>3 = Rotate 180</li>
- <li>6 = Rotate 90 CW</li>
- <li>8 = Rotate 270 CW</li>
- </ul>
- <p>
- <a href='http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html'>
- EXIF Reference</a>
- </p>
- ",
- );
- 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).
- if ($image->info['mime_type'] == 'image/jpeg') {
- if (!function_exists('exif_read_data')) {
- watchdog('image', 'The image %file could not be auto-rotated because the exif_read_data() function is not available in this PHP installation. You probably will have to enable the exif extension.', array('%file' => $image->source));
- return FALSE;
- }
- $exif = exif_read_data(drupal_realpath($image->source));
- if (isset($exif['Orientation'])) {
- switch ($exif['Orientation']) {
- case 3:
- $degrees = 180;
- break;
- case 6:
- $degrees = 90;
- break;
- case 8:
- $degrees = 270;
- break;
- default:
- $degrees = 0;
- }
- if ($degrees != 0) {
- $org_width = $image->info['width'];
- $org_height = $image->info['height'];
- image_rotate($image, $degrees);
- if (($degrees === 90 || $degrees === 270) && $image->info['width'] === $org_width) {
- // The toolkit failed to alter the dimensions (imagemagick currently
- // fails to do so). So we do it ourselves.
- $image->info['width'] = $org_height;
- $image->info['height'] = $org_width;
- }
- }
- }
- }
- return TRUE;
- }
- /**
- * Image dimensions callback; Auto-rotate.
- *
- * @param $dimensions
- * Dimensions to be modified - an array with components width and height, in
- * pixels.
- * @param $data
- * An array of attributes to use when performing the rotate effect containing
- * the following items:
- * - "degrees": The number of (clockwise) degrees to rotate the image.
- * - "random": A boolean indicating that a random rotation angle should be
- * used for this image. The angle specified in "degrees" is used as a
- * positive and negative maximum.
- */
- function imagecache_autorotate_dimensions(array &$dimensions, array $data) {
- $dimensions['width'] = $dimensions['height'] = NULL;
- }
|