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