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