MediaWiki
REL1_24
|
00001 <?php 00010 class ExifRotationTest extends MediaWikiMediaTestCase { 00011 00012 protected function setUp() { 00013 parent::setUp(); 00014 $this->checkPHPExtension( 'exif' ); 00015 00016 $this->handler = new BitmapHandler(); 00017 00018 $this->setMwGlobals( array( 00019 'wgShowEXIF' => true, 00020 'wgEnableAutoRotation' => true, 00021 ) ); 00022 } 00023 00027 protected function createsThumbnails() { 00028 return true; 00029 } 00030 00034 public function testMetadata( $name, $type, $info ) { 00035 if ( !$this->handler->canRotate() ) { 00036 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); 00037 } 00038 $file = $this->dataFile( $name, $type ); 00039 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); 00040 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); 00041 } 00042 00050 public function testMetadataAutoRotate( $name, $type, $info ) { 00051 $this->setMwGlobals( 'wgEnableAutoRotation', null ); 00052 $this->setMwGlobals( 'wgUseImageMagick', true ); 00053 $this->setMwGlobals( 'wgUseImageResize', true ); 00054 00055 $file = $this->dataFile( $name, $type ); 00056 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); 00057 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); 00058 } 00059 00064 public function testRotationRendering( $name, $type, $info, $thumbs ) { 00065 if ( !$this->handler->canRotate() ) { 00066 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); 00067 } 00068 foreach ( $thumbs as $size => $out ) { 00069 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { 00070 $params = array( 00071 'width' => $matches[1], 00072 ); 00073 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) { 00074 $params = array( 00075 'width' => $matches[1], 00076 'height' => $matches[2] 00077 ); 00078 } else { 00079 throw new MWException( 'bogus test data format ' . $size ); 00080 } 00081 00082 $file = $this->dataFile( $name, $type ); 00083 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE ); 00084 00085 $this->assertEquals( 00086 $out[0], 00087 $thumb->getWidth(), 00088 "$name: thumb reported width check for $size" 00089 ); 00090 $this->assertEquals( 00091 $out[1], 00092 $thumb->getHeight(), 00093 "$name: thumb reported height check for $size" 00094 ); 00095 00096 $gis = getimagesize( $thumb->getLocalCopyPath() ); 00097 if ( $out[0] > $info['width'] ) { 00098 // Physical image won't be scaled bigger than the original. 00099 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); 00100 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); 00101 } else { 00102 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); 00103 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); 00104 } 00105 } 00106 } 00107 00108 public static function provideFiles() { 00109 return array( 00110 array( 00111 'landscape-plain.jpg', 00112 'image/jpeg', 00113 array( 00114 'width' => 1024, 00115 'height' => 768, 00116 ), 00117 array( 00118 '800x600px' => array( 800, 600 ), 00119 '9999x800px' => array( 1067, 800 ), 00120 '800px' => array( 800, 600 ), 00121 '600px' => array( 600, 450 ), 00122 ) 00123 ), 00124 array( 00125 'portrait-rotated.jpg', 00126 'image/jpeg', 00127 array( 00128 'width' => 768, // as rotated 00129 'height' => 1024, // as rotated 00130 ), 00131 array( 00132 '800x600px' => array( 450, 600 ), 00133 '9999x800px' => array( 600, 800 ), 00134 '800px' => array( 800, 1067 ), 00135 '600px' => array( 600, 800 ), 00136 ) 00137 ) 00138 ); 00139 } 00140 00145 public function testMetadataNoAutoRotate( $name, $type, $info ) { 00146 $this->setMwGlobals( 'wgEnableAutoRotation', false ); 00147 00148 $file = $this->dataFile( $name, $type ); 00149 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); 00150 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); 00151 } 00152 00157 public function testMetadataAutoRotateUnsupported( $name, $type, $info ) { 00158 $this->setMwGlobals( 'wgEnableAutoRotation', null ); 00159 $this->setMwGlobals( 'wgUseImageResize', false ); 00160 00161 $file = $this->dataFile( $name, $type ); 00162 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); 00163 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); 00164 } 00165 00170 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) { 00171 $this->setMwGlobals( 'wgEnableAutoRotation', false ); 00172 00173 foreach ( $thumbs as $size => $out ) { 00174 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { 00175 $params = array( 00176 'width' => $matches[1], 00177 ); 00178 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) { 00179 $params = array( 00180 'width' => $matches[1], 00181 'height' => $matches[2] 00182 ); 00183 } else { 00184 throw new MWException( 'bogus test data format ' . $size ); 00185 } 00186 00187 $file = $this->dataFile( $name, $type ); 00188 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE ); 00189 00190 $this->assertEquals( 00191 $out[0], 00192 $thumb->getWidth(), 00193 "$name: thumb reported width check for $size" 00194 ); 00195 $this->assertEquals( 00196 $out[1], 00197 $thumb->getHeight(), 00198 "$name: thumb reported height check for $size" 00199 ); 00200 00201 $gis = getimagesize( $thumb->getLocalCopyPath() ); 00202 if ( $out[0] > $info['width'] ) { 00203 // Physical image won't be scaled bigger than the original. 00204 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); 00205 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); 00206 } else { 00207 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); 00208 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); 00209 } 00210 } 00211 } 00212 00213 public static function provideFilesNoAutoRotate() { 00214 return array( 00215 array( 00216 'landscape-plain.jpg', 00217 'image/jpeg', 00218 array( 00219 'width' => 1024, 00220 'height' => 768, 00221 ), 00222 array( 00223 '800x600px' => array( 800, 600 ), 00224 '9999x800px' => array( 1067, 800 ), 00225 '800px' => array( 800, 600 ), 00226 '600px' => array( 600, 450 ), 00227 ) 00228 ), 00229 array( 00230 'portrait-rotated.jpg', 00231 'image/jpeg', 00232 array( 00233 'width' => 1024, // since not rotated 00234 'height' => 768, // since not rotated 00235 ), 00236 array( 00237 '800x600px' => array( 800, 600 ), 00238 '9999x800px' => array( 1067, 800 ), 00239 '800px' => array( 800, 600 ), 00240 '600px' => array( 600, 450 ), 00241 ) 00242 ) 00243 ); 00244 } 00245 00246 const TEST_WIDTH = 100; 00247 const TEST_HEIGHT = 200; 00248 00252 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) { 00253 $result = $this->handler->extractPreRotationDimensions( array( 00254 'physicalWidth' => self::TEST_WIDTH, 00255 'physicalHeight' => self::TEST_HEIGHT, 00256 ), $rotation ); 00257 $this->assertEquals( $expected, $result ); 00258 } 00259 00260 public static function provideBitmapExtractPreRotationDimensions() { 00261 return array( 00262 array( 00263 0, 00264 array( self::TEST_WIDTH, self::TEST_HEIGHT ) 00265 ), 00266 array( 00267 90, 00268 array( self::TEST_HEIGHT, self::TEST_WIDTH ) 00269 ), 00270 array( 00271 180, 00272 array( self::TEST_WIDTH, self::TEST_HEIGHT ) 00273 ), 00274 array( 00275 270, 00276 array( self::TEST_HEIGHT, self::TEST_WIDTH ) 00277 ), 00278 ); 00279 } 00280 }