(follow-up r99910) Make $wgEnableAutoRotation work...
authorBrian Wolff <bawolff@users.mediawiki.org>
Fri, 11 Nov 2011 04:09:05 +0000 (04:09 +0000)
committerBrian Wolff <bawolff@users.mediawiki.org>
Fri, 11 Nov 2011 04:09:05 +0000 (04:09 +0000)
Also unit-tests. There's a bit of duplication in the unit tests, and I wasn't sure if there was a better way with less duplication.

includes/media/ExifBitmap.php
tests/phpunit/includes/media/ExifRotationTest.php

index 96f5d21..05ce161 100644 (file)
@@ -134,12 +134,17 @@ class ExifBitmapHandler extends BitmapHandler {
         * @return array
         */
        function getImageSize( $image, $path ) {
+               global $wgEnableAutoRotation;
                $gis = parent::getImageSize( $image, $path );
                
                // Don't just call $image->getMetadata(); File::getPropsFromPath() calls us with a bogus object.
                // This may mean we read EXIF data twice on initial upload.
-               $meta = $this->getMetadata( $image, $path );
-               $rotation = $this->getRotationForExif( $meta );
+               if ( $wgEnableAutoRotation ) {
+                       $meta = $this->getMetadata( $image, $path );
+                       $rotation = $this->getRotationForExif( $meta );
+               } else {
+                       $rotation = 0;
+               }
 
                if ($rotation == 90 || $rotation == 270) {
                        $width = $gis[0];
index 87aed29..91ea96c 100644 (file)
@@ -20,10 +20,15 @@ class ExifRotationTest extends MediaWikiTestCase {
                global $wgShowEXIF;
                $this->show = $wgShowEXIF;
                $wgShowEXIF = true;
+
+               global $wgEnableAutoRotation;
+               $this->oldAuto = $wgEnableAutoRotation;
+               $wgEnableAutoRotation = true;
        }
        public function tearDown() {
-               global $wgShowEXIF;
+               global $wgShowEXIF, $wgEnableAutoRotation;
                $wgShowEXIF = $this->show;
+               $wgEnableAutoRotation = $this->oldAuto;
        }
 
        /**
@@ -31,6 +36,9 @@ class ExifRotationTest extends MediaWikiTestCase {
         * @dataProvider providerFiles
         */
        function testMetadata( $name, $type, $info ) {
+               if ( !BitmapHandler::canRotate() ) {
+                       $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
+               }
                $file = UnregisteredLocalFile::newFromPath( $this->filePath . $name, $type );
                $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
                $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
@@ -41,6 +49,9 @@ class ExifRotationTest extends MediaWikiTestCase {
         * @dataProvider providerFiles
         */
        function testRotationRendering( $name, $type, $info, $thumbs ) {
+               if ( !BitmapHandler::canRotate() ) {
+                       $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
+               }
                foreach( $thumbs as $size => $out ) {
                        if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
                                $params = array(
@@ -109,6 +120,95 @@ class ExifRotationTest extends MediaWikiTestCase {
                        )
                );
        }
+
+       /**
+        * Same as before, but with auto-rotation disabled.
+        * @dataProvider providerFilesNoAutoRotate
+        */
+       function testMetadataNoAutoRotate( $name, $type, $info ) {
+               global $wgEnableAutoRotation;
+               $wgEnableAutoRotation = false;
+
+               $file = UnregisteredLocalFile::newFromPath( $this->filePath . $name, $type );
+               $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
+               $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
+
+               $wgEnableAutoRotation = true;
+       }
+
+       /**
+        *
+        * @dataProvider providerFilesNoAutoRotate
+        */
+       function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
+               global $wgEnableAutoRotation;
+               $wgEnableAutoRotation = false;
+
+               foreach( $thumbs as $size => $out ) {
+                       if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
+                               $params = array(
+                                       'width' => $matches[1],
+                               );
+                       } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
+                               $params = array(
+                                       'width' => $matches[1],
+                                       'height' => $matches[2]
+                               );
+                       } else {
+                               throw new MWException('bogus test data format ' . $size);
+                       }
+
+                       $file = $this->localFile( $name, $type );
+                       $thumb = $file->transform( $params, File::RENDER_NOW );
+
+                       $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
+                       $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
+
+                       $gis = getimagesize( $thumb->getPath() );
+                       if ($out[0] > $info['width']) {
+                               // Physical image won't be scaled bigger than the original.
+                               $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
+                               $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
+                       } else {
+                               $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
+                               $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
+                       }
+               }
+               $wgEnableAutoRotation = true;
+       }
+
+       function providerFilesNoAutoRotate() {
+               return array(
+                       array(
+                               'landscape-plain.jpg',
+                               'image/jpeg',
+                               array(
+                                       'width' => 1024,
+                                       'height' => 768,
+                               ),
+                               array(
+                                       '800x600px' => array( 800, 600 ),
+                                       '9999x800px' => array( 1067, 800 ),
+                                       '800px' => array( 800, 600 ),
+                                       '600px' => array( 600, 450 ),
+                               )
+                       ),
+                       array(
+                               'portrait-rotated.jpg',
+                               'image/jpeg',
+                               array(
+                                       'width' => 1024, // since not rotated
+                                       'height' => 768, // since not rotated
+                               ),
+                               array(
+                                       '800x600px' => array( 800, 600 ),
+                                       '9999x800px' => array( 1067, 800 ),
+                                       '800px' => array( 800, 600 ),
+                                       '600px' => array( 600, 450 ),
+                               )
+                       )
+               );
+       }
        
        
        const TEST_WIDTH = 100;