3 * Tests related to auto rotation.
10 class ExifRotationTest
extends MediaWikiMediaTestCase
{
12 protected function setUp() {
14 $this->checkPHPExtension( 'exif' );
16 $this->handler
= new BitmapHandler();
18 $this->setMwGlobals( [
20 'wgEnableAutoRotation' => true,
25 * Mark this test as creating thumbnail files.
27 protected function createsThumbnails() {
32 * @dataProvider provideFiles
34 public function testMetadata( $name, $type, $info ) {
35 if ( !$this->handler
->canRotate() ) {
36 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
38 $file = $this->dataFile( $name, $type );
39 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
40 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
44 * Same as before, but with auto-rotation set to auto.
46 * This sets scaler to image magick, which we should detect as
47 * supporting rotation.
48 * @dataProvider provideFiles
50 public function testMetadataAutoRotate( $name, $type, $info ) {
51 $this->setMwGlobals( 'wgEnableAutoRotation', null );
52 $this->setMwGlobals( 'wgUseImageMagick', true );
53 $this->setMwGlobals( 'wgUseImageResize', true );
55 $file = $this->dataFile( $name, $type );
56 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
57 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
62 * @dataProvider provideFiles
64 public function testRotationRendering( $name, $type, $info, $thumbs ) {
65 if ( !$this->handler
->canRotate() ) {
66 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
68 foreach ( $thumbs as $size => $out ) {
69 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
71 'width' => $matches[1],
73 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
75 'width' => $matches[1],
76 'height' => $matches[2]
79 throw new MWException( 'bogus test data format ' . $size );
82 $file = $this->dataFile( $name, $type );
83 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
88 "$name: thumb reported width check for $size"
93 "$name: thumb reported height check for $size"
96 $gis = getimagesize( $thumb->getLocalCopyPath() );
97 if ( $out[0] > $info['width'] ) {
98 // Physical image won't be scaled bigger than the original.
99 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
100 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
102 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
103 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
108 public static function provideFiles() {
111 'landscape-plain.jpg',
118 '800x600px' => [ 800, 600 ],
119 '9999x800px' => [ 1067, 800 ],
120 '800px' => [ 800, 600 ],
121 '600px' => [ 600, 450 ],
125 'portrait-rotated.jpg',
128 'width' => 768, // as rotated
129 'height' => 1024, // as rotated
132 '800x600px' => [ 450, 600 ],
133 '9999x800px' => [ 600, 800 ],
134 '800px' => [ 800, 1067 ],
135 '600px' => [ 600, 800 ],
142 * Same as before, but with auto-rotation disabled.
143 * @dataProvider provideFilesNoAutoRotate
145 public function testMetadataNoAutoRotate( $name, $type, $info ) {
146 $this->setMwGlobals( 'wgEnableAutoRotation', false );
148 $file = $this->dataFile( $name, $type );
149 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
150 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
154 * Same as before, but with auto-rotation set to auto and an image scaler that doesn't support it.
155 * @dataProvider provideFilesNoAutoRotate
157 public function testMetadataAutoRotateUnsupported( $name, $type, $info ) {
158 $this->setMwGlobals( 'wgEnableAutoRotation', null );
159 $this->setMwGlobals( 'wgUseImageResize', false );
161 $file = $this->dataFile( $name, $type );
162 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
163 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
168 * @dataProvider provideFilesNoAutoRotate
170 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
171 $this->setMwGlobals( 'wgEnableAutoRotation', false );
173 foreach ( $thumbs as $size => $out ) {
174 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
176 'width' => $matches[1],
178 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
180 'width' => $matches[1],
181 'height' => $matches[2]
184 throw new MWException( 'bogus test data format ' . $size );
187 $file = $this->dataFile( $name, $type );
188 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
193 "$name: thumb reported width check for $size"
198 "$name: thumb reported height check for $size"
201 $gis = getimagesize( $thumb->getLocalCopyPath() );
202 if ( $out[0] > $info['width'] ) {
203 // Physical image won't be scaled bigger than the original.
204 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
205 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
207 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
208 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
213 public static function provideFilesNoAutoRotate() {
216 'landscape-plain.jpg',
223 '800x600px' => [ 800, 600 ],
224 '9999x800px' => [ 1067, 800 ],
225 '800px' => [ 800, 600 ],
226 '600px' => [ 600, 450 ],
230 'portrait-rotated.jpg',
233 'width' => 1024, // since not rotated
234 'height' => 768, // since not rotated
237 '800x600px' => [ 800, 600 ],
238 '9999x800px' => [ 1067, 800 ],
239 '800px' => [ 800, 600 ],
240 '600px' => [ 600, 450 ],
246 const TEST_WIDTH
= 100;
247 const TEST_HEIGHT
= 200;
250 * @dataProvider provideBitmapExtractPreRotationDimensions
252 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
253 $result = $this->handler
->extractPreRotationDimensions( [
254 'physicalWidth' => self
::TEST_WIDTH
,
255 'physicalHeight' => self
::TEST_HEIGHT
,
257 $this->assertEquals( $expected, $result );
260 public static function provideBitmapExtractPreRotationDimensions() {
264 [ self
::TEST_WIDTH
, self
::TEST_HEIGHT
]
268 [ self
::TEST_HEIGHT
, self
::TEST_WIDTH
]
272 [ self
::TEST_WIDTH
, self
::TEST_HEIGHT
]
276 [ self
::TEST_HEIGHT
, self
::TEST_WIDTH
]