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