MediaWiki  REL1_22
ExifRotationTest.php
Go to the documentation of this file.
00001 <?php
00009 class ExifRotationTest extends MediaWikiTestCase {
00010 
00011     protected function setUp() {
00012         parent::setUp();
00013         if ( !extension_loaded( 'exif' ) ) {
00014             $this->markTestSkipped( "This test needs the exif extension." );
00015         }
00016 
00017         $this->handler = new BitmapHandler();
00018         $filePath = __DIR__ . '/../../data/media';
00019 
00020         $tmpDir = $this->getNewTempDirectory();
00021 
00022         $this->repo = new FSRepo( array(
00023             'name' => 'temp',
00024             'url' => 'http://localhost/thumbtest',
00025             'backend' => new FSFileBackend( array(
00026                 'name' => 'localtesting',
00027                 'lockManager' => 'nullLockManager',
00028                 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
00029             ) )
00030         ) );
00031 
00032         $this->setMwGlobals( array(
00033             'wgShowEXIF' => true,
00034             'wgEnableAutoRotation' => true,
00035         ) );
00036     }
00037 
00041     public function testMetadata( $name, $type, $info ) {
00042         if ( !BitmapHandler::canRotate() ) {
00043             $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
00044         }
00045         $file = $this->dataFile( $name, $type );
00046         $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
00047         $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
00048     }
00049 
00054     public function testRotationRendering( $name, $type, $info, $thumbs ) {
00055         if ( !BitmapHandler::canRotate() ) {
00056             $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
00057         }
00058         foreach ( $thumbs as $size => $out ) {
00059             if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
00060                 $params = array(
00061                     'width' => $matches[1],
00062                 );
00063             } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
00064                 $params = array(
00065                     'width' => $matches[1],
00066                     'height' => $matches[2]
00067                 );
00068             } else {
00069                 throw new MWException( 'bogus test data format ' . $size );
00070             }
00071 
00072             $file = $this->dataFile( $name, $type );
00073             $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
00074 
00075             $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
00076             $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
00077 
00078             $gis = getimagesize( $thumb->getLocalCopyPath() );
00079             if ( $out[0] > $info['width'] ) {
00080                 // Physical image won't be scaled bigger than the original.
00081                 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
00082                 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
00083             } else {
00084                 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
00085                 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
00086             }
00087         }
00088     }
00089 
00090     /* Utility function */
00091     private function dataFile( $name, $type ) {
00092         return new UnregisteredLocalFile( false, $this->repo,
00093             "mwstore://localtesting/data/$name", $type );
00094     }
00095 
00096     public static function provideFiles() {
00097         return array(
00098             array(
00099                 'landscape-plain.jpg',
00100                 'image/jpeg',
00101                 array(
00102                     'width' => 1024,
00103                     'height' => 768,
00104                 ),
00105                 array(
00106                     '800x600px' => array( 800, 600 ),
00107                     '9999x800px' => array( 1067, 800 ),
00108                     '800px' => array( 800, 600 ),
00109                     '600px' => array( 600, 450 ),
00110                 )
00111             ),
00112             array(
00113                 'portrait-rotated.jpg',
00114                 'image/jpeg',
00115                 array(
00116                     'width' => 768, // as rotated
00117                     'height' => 1024, // as rotated
00118                 ),
00119                 array(
00120                     '800x600px' => array( 450, 600 ),
00121                     '9999x800px' => array( 600, 800 ),
00122                     '800px' => array( 800, 1067 ),
00123                     '600px' => array( 600, 800 ),
00124                 )
00125             )
00126         );
00127     }
00128 
00133     public function testMetadataNoAutoRotate( $name, $type, $info ) {
00134         $this->setMwGlobals( 'wgEnableAutoRotation', false );
00135 
00136         $file = $this->dataFile( $name, $type );
00137         $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
00138         $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
00139     }
00140 
00145     public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
00146         $this->setMwGlobals( 'wgEnableAutoRotation', false );
00147 
00148         foreach ( $thumbs as $size => $out ) {
00149             if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
00150                 $params = array(
00151                     'width' => $matches[1],
00152                 );
00153             } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
00154                 $params = array(
00155                     'width' => $matches[1],
00156                     'height' => $matches[2]
00157                 );
00158             } else {
00159                 throw new MWException( 'bogus test data format ' . $size );
00160             }
00161 
00162             $file = $this->dataFile( $name, $type );
00163             $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
00164 
00165             $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
00166             $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
00167 
00168             $gis = getimagesize( $thumb->getLocalCopyPath() );
00169             if ( $out[0] > $info['width'] ) {
00170                 // Physical image won't be scaled bigger than the original.
00171                 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
00172                 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
00173             } else {
00174                 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
00175                 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
00176             }
00177         }
00178     }
00179 
00180     public static function provideFilesNoAutoRotate() {
00181         return array(
00182             array(
00183                 'landscape-plain.jpg',
00184                 'image/jpeg',
00185                 array(
00186                     'width' => 1024,
00187                     'height' => 768,
00188                 ),
00189                 array(
00190                     '800x600px' => array( 800, 600 ),
00191                     '9999x800px' => array( 1067, 800 ),
00192                     '800px' => array( 800, 600 ),
00193                     '600px' => array( 600, 450 ),
00194                 )
00195             ),
00196             array(
00197                 'portrait-rotated.jpg',
00198                 'image/jpeg',
00199                 array(
00200                     'width' => 1024, // since not rotated
00201                     'height' => 768, // since not rotated
00202                 ),
00203                 array(
00204                     '800x600px' => array( 800, 600 ),
00205                     '9999x800px' => array( 1067, 800 ),
00206                     '800px' => array( 800, 600 ),
00207                     '600px' => array( 600, 450 ),
00208                 )
00209             )
00210         );
00211     }
00212 
00213 
00214     const TEST_WIDTH = 100;
00215     const TEST_HEIGHT = 200;
00216 
00220     public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
00221         $result = $this->handler->extractPreRotationDimensions( array(
00222             'physicalWidth' => self::TEST_WIDTH,
00223             'physicalHeight' => self::TEST_HEIGHT,
00224         ), $rotation );
00225         $this->assertEquals( $expected, $result );
00226     }
00227 
00228     public static function provideBitmapExtractPreRotationDimensions() {
00229         return array(
00230             array(
00231                 0,
00232                 array( self::TEST_WIDTH, self::TEST_HEIGHT )
00233             ),
00234             array(
00235                 90,
00236                 array( self::TEST_HEIGHT, self::TEST_WIDTH )
00237             ),
00238             array(
00239                 180,
00240                 array( self::TEST_WIDTH, self::TEST_HEIGHT )
00241             ),
00242             array(
00243                 270,
00244                 array( self::TEST_HEIGHT, self::TEST_WIDTH )
00245             ),
00246         );
00247     }
00248 }