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