3 * Tests related to auto rotation.
9 class ExifRotationTest
extends MediaWikiTestCase
{
11 protected function setUp() {
13 $this->checkPHPExtension( 'exif' );
15 $this->handler
= new BitmapHandler();
16 $filePath = __DIR__
. '/../../data/media';
18 $tmpDir = $this->getNewTempDirectory();
20 $this->repo
= new FSRepo( array(
22 'url' => 'http://localhost/thumbtest',
23 'backend' => new FSFileBackend( array(
24 'name' => 'localtesting',
25 'wikiId' => wfWikiId(),
26 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
30 $this->setMwGlobals( array(
32 'wgEnableAutoRotation' => true,
37 * @dataProvider provideFiles
39 public function testMetadata( $name, $type, $info ) {
40 if ( !BitmapHandler
::canRotate() ) {
41 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
43 $file = $this->dataFile( $name, $type );
44 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
45 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
50 * @dataProvider provideFiles
52 public function testRotationRendering( $name, $type, $info, $thumbs ) {
53 if ( !BitmapHandler
::canRotate() ) {
54 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
56 foreach ( $thumbs as $size => $out ) {
57 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
59 'width' => $matches[1],
61 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
63 'width' => $matches[1],
64 'height' => $matches[2]
67 throw new MWException( 'bogus test data format ' . $size );
70 $file = $this->dataFile( $name, $type );
71 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
73 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
74 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
76 $gis = getimagesize( $thumb->getLocalCopyPath() );
77 if ( $out[0] > $info['width'] ) {
78 // Physical image won't be scaled bigger than the original.
79 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
80 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
82 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
83 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
88 /* Utility function */
89 private function dataFile( $name, $type ) {
90 return new UnregisteredLocalFile( false, $this->repo
,
91 "mwstore://localtesting/data/$name", $type );
94 public static function provideFiles() {
97 'landscape-plain.jpg',
104 '800x600px' => array( 800, 600 ),
105 '9999x800px' => array( 1067, 800 ),
106 '800px' => array( 800, 600 ),
107 '600px' => array( 600, 450 ),
111 'portrait-rotated.jpg',
114 'width' => 768, // as rotated
115 'height' => 1024, // as rotated
118 '800x600px' => array( 450, 600 ),
119 '9999x800px' => array( 600, 800 ),
120 '800px' => array( 800, 1067 ),
121 '600px' => array( 600, 800 ),
128 * Same as before, but with auto-rotation disabled.
129 * @dataProvider provideFilesNoAutoRotate
131 public function testMetadataNoAutoRotate( $name, $type, $info ) {
132 $this->setMwGlobals( 'wgEnableAutoRotation', false );
134 $file = $this->dataFile( $name, $type );
135 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
136 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
141 * @dataProvider provideFilesNoAutoRotate
143 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
144 $this->setMwGlobals( 'wgEnableAutoRotation', false );
146 foreach ( $thumbs as $size => $out ) {
147 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
149 'width' => $matches[1],
151 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
153 'width' => $matches[1],
154 'height' => $matches[2]
157 throw new MWException( 'bogus test data format ' . $size );
160 $file = $this->dataFile( $name, $type );
161 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
163 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
164 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
166 $gis = getimagesize( $thumb->getLocalCopyPath() );
167 if ( $out[0] > $info['width'] ) {
168 // Physical image won't be scaled bigger than the original.
169 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
170 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
172 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
173 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
178 public static function provideFilesNoAutoRotate() {
181 'landscape-plain.jpg',
188 '800x600px' => array( 800, 600 ),
189 '9999x800px' => array( 1067, 800 ),
190 '800px' => array( 800, 600 ),
191 '600px' => array( 600, 450 ),
195 'portrait-rotated.jpg',
198 'width' => 1024, // since not rotated
199 'height' => 768, // since not rotated
202 '800x600px' => array( 800, 600 ),
203 '9999x800px' => array( 1067, 800 ),
204 '800px' => array( 800, 600 ),
205 '600px' => array( 600, 450 ),
211 const TEST_WIDTH
= 100;
212 const TEST_HEIGHT
= 200;
215 * @dataProvider provideBitmapExtractPreRotationDimensions
217 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
218 $result = $this->handler
->extractPreRotationDimensions( array(
219 'physicalWidth' => self
::TEST_WIDTH
,
220 'physicalHeight' => self
::TEST_HEIGHT
,
222 $this->assertEquals( $expected, $result );
225 public static function provideBitmapExtractPreRotationDimensions() {
229 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
233 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
237 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
241 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)