3 * Tests related to auto rotation.
9 class ExifRotationTest
extends MediaWikiTestCase
{
11 protected function setUp() {
13 if ( !extension_loaded( 'exif' ) ) {
14 $this->markTestSkipped( "This test needs the exif extension." );
17 $this->handler
= new BitmapHandler();
18 $filePath = __DIR__
. '/../../data/media';
20 $tmpDir = $this->getNewTempDirectory();
22 $this->repo
= new FSRepo( array(
24 'url' => 'http://localhost/thumbtest',
25 'backend' => new FSFileBackend( array(
26 'name' => 'localtesting',
27 'lockManager' => 'nullLockManager',
28 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
32 $this->setMwGlobals( array(
34 'wgEnableAutoRotation' => true,
39 * @dataProvider provideFiles
41 public function testMetadata( $name, $type, $info ) {
42 if ( !BitmapHandler
::canRotate() ) {
43 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
45 $file = $this->dataFile( $name, $type );
46 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
47 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
52 * @dataProvider provideFiles
54 public function testRotationRendering( $name, $type, $info, $thumbs ) {
55 if ( !BitmapHandler
::canRotate() ) {
56 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
58 foreach ( $thumbs as $size => $out ) {
59 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
61 'width' => $matches[1],
63 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
65 'width' => $matches[1],
66 'height' => $matches[2]
69 throw new MWException( 'bogus test data format ' . $size );
72 $file = $this->dataFile( $name, $type );
73 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
75 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
76 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
78 $gis = getimagesize( $thumb->getLocalCopyPath() );
79 if ( $out[0] > $info['width'] ) {
80 // Physical image won't be scaled bigger than the original.
81 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
82 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
84 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
85 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
90 /* Utility function */
91 private function dataFile( $name, $type ) {
92 return new UnregisteredLocalFile( false, $this->repo
,
93 "mwstore://localtesting/data/$name", $type );
96 public static function provideFiles() {
99 'landscape-plain.jpg',
106 '800x600px' => array( 800, 600 ),
107 '9999x800px' => array( 1067, 800 ),
108 '800px' => array( 800, 600 ),
109 '600px' => array( 600, 450 ),
113 'portrait-rotated.jpg',
116 'width' => 768, // as rotated
117 'height' => 1024, // as rotated
120 '800x600px' => array( 450, 600 ),
121 '9999x800px' => array( 600, 800 ),
122 '800px' => array( 800, 1067 ),
123 '600px' => array( 600, 800 ),
130 * Same as before, but with auto-rotation disabled.
131 * @dataProvider provideFilesNoAutoRotate
133 public function testMetadataNoAutoRotate( $name, $type, $info ) {
134 $this->setMwGlobals( 'wgEnableAutoRotation', false );
136 $file = $this->dataFile( $name, $type );
137 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
138 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
143 * @dataProvider provideFilesNoAutoRotate
145 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
146 $this->setMwGlobals( 'wgEnableAutoRotation', false );
148 foreach ( $thumbs as $size => $out ) {
149 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
151 'width' => $matches[1],
153 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
155 'width' => $matches[1],
156 'height' => $matches[2]
159 throw new MWException( 'bogus test data format ' . $size );
162 $file = $this->dataFile( $name, $type );
163 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
165 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
166 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
168 $gis = getimagesize( $thumb->getLocalCopyPath() );
169 if ( $out[0] > $info['width'] ) {
170 // Physical image won't be scaled bigger than the original.
171 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
172 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
174 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
175 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
180 public static function provideFilesNoAutoRotate() {
183 'landscape-plain.jpg',
190 '800x600px' => array( 800, 600 ),
191 '9999x800px' => array( 1067, 800 ),
192 '800px' => array( 800, 600 ),
193 '600px' => array( 600, 450 ),
197 'portrait-rotated.jpg',
200 'width' => 1024, // since not rotated
201 'height' => 768, // since not rotated
204 '800x600px' => array( 800, 600 ),
205 '9999x800px' => array( 1067, 800 ),
206 '800px' => array( 800, 600 ),
207 '600px' => array( 600, 450 ),
214 const TEST_WIDTH
= 100;
215 const TEST_HEIGHT
= 200;
218 * @dataProvider provideBitmapExtractPreRotationDimensions
220 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
221 $result = $this->handler
->extractPreRotationDimensions( array(
222 'physicalWidth' => self
::TEST_WIDTH
,
223 'physicalHeight' => self
::TEST_HEIGHT
,
225 $this->assertEquals( $expected, $result );
228 public static function provideBitmapExtractPreRotationDimensions() {
232 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
236 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
240 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
244 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)