fff101f3a2149f6b49a5798a4d80fc9cb0487fd4
3 * Tests related to auto rotation.
8 * @covers BitmapHandler
10 class ExifRotationTest
extends MediaWikiMediaTestCase
{
12 /** @var BitmapHandler */
15 protected function setUp() {
17 $this->checkPHPExtension( 'exif' );
19 $this->handler
= new BitmapHandler();
21 $this->setMwGlobals( [
23 'wgEnableAutoRotation' => true,
28 * Mark this test as creating thumbnail files.
30 protected function createsThumbnails() {
35 * @dataProvider provideFiles
37 public function testMetadata( $name, $type, $info ) {
38 if ( !$this->handler
->canRotate() ) {
39 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
41 $file = $this->dataFile( $name, $type );
42 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
43 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
47 * Same as before, but with auto-rotation set to auto.
49 * This sets scaler to image magick, which we should detect as
50 * supporting rotation.
51 * @dataProvider provideFiles
53 public function testMetadataAutoRotate( $name, $type, $info ) {
54 $this->setMwGlobals( 'wgEnableAutoRotation', null );
55 $this->setMwGlobals( 'wgUseImageMagick', true );
56 $this->setMwGlobals( 'wgUseImageResize', true );
58 $file = $this->dataFile( $name, $type );
59 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
60 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
65 * @dataProvider provideFiles
67 public function testRotationRendering( $name, $type, $info, $thumbs ) {
68 if ( !$this->handler
->canRotate() ) {
69 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
71 foreach ( $thumbs as $size => $out ) {
72 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
74 'width' => $matches[1],
76 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
78 'width' => $matches[1],
79 'height' => $matches[2]
82 throw new MWException( 'bogus test data format ' . $size );
85 $file = $this->dataFile( $name, $type );
86 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
91 "$name: thumb reported width check for $size"
96 "$name: thumb reported height check for $size"
99 $gis = getimagesize( $thumb->getLocalCopyPath() );
100 if ( $out[0] > $info['width'] ) {
101 // Physical image won't be scaled bigger than the original.
102 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
103 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
105 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
106 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
111 public static function provideFiles() {
114 'landscape-plain.jpg',
121 '800x600px' => [ 800, 600 ],
122 '9999x800px' => [ 1067, 800 ],
123 '800px' => [ 800, 600 ],
124 '600px' => [ 600, 450 ],
128 'portrait-rotated.jpg',
131 'width' => 768, // as rotated
132 'height' => 1024, // as rotated
135 '800x600px' => [ 450, 600 ],
136 '9999x800px' => [ 600, 800 ],
137 '800px' => [ 800, 1067 ],
138 '600px' => [ 600, 800 ],
145 * Same as before, but with auto-rotation disabled.
146 * @dataProvider provideFilesNoAutoRotate
148 public function testMetadataNoAutoRotate( $name, $type, $info ) {
149 $this->setMwGlobals( 'wgEnableAutoRotation', false );
151 $file = $this->dataFile( $name, $type );
152 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
153 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
157 * Same as before, but with auto-rotation set to auto and an image scaler that doesn't support it.
158 * @dataProvider provideFilesNoAutoRotate
160 public function testMetadataAutoRotateUnsupported( $name, $type, $info ) {
161 $this->setMwGlobals( 'wgEnableAutoRotation', null );
162 $this->setMwGlobals( 'wgUseImageResize', false );
164 $file = $this->dataFile( $name, $type );
165 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
166 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
171 * @dataProvider provideFilesNoAutoRotate
173 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
174 $this->setMwGlobals( 'wgEnableAutoRotation', false );
176 foreach ( $thumbs as $size => $out ) {
177 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
179 'width' => $matches[1],
181 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
183 'width' => $matches[1],
184 'height' => $matches[2]
187 throw new MWException( 'bogus test data format ' . $size );
190 $file = $this->dataFile( $name, $type );
191 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
196 "$name: thumb reported width check for $size"
201 "$name: thumb reported height check for $size"
204 $gis = getimagesize( $thumb->getLocalCopyPath() );
205 if ( $out[0] > $info['width'] ) {
206 // Physical image won't be scaled bigger than the original.
207 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
208 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
210 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
211 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
216 public static function provideFilesNoAutoRotate() {
219 'landscape-plain.jpg',
226 '800x600px' => [ 800, 600 ],
227 '9999x800px' => [ 1067, 800 ],
228 '800px' => [ 800, 600 ],
229 '600px' => [ 600, 450 ],
233 'portrait-rotated.jpg',
236 'width' => 1024, // since not rotated
237 'height' => 768, // since not rotated
240 '800x600px' => [ 800, 600 ],
241 '9999x800px' => [ 1067, 800 ],
242 '800px' => [ 800, 600 ],
243 '600px' => [ 600, 450 ],
249 const TEST_WIDTH
= 100;
250 const TEST_HEIGHT
= 200;
253 * @dataProvider provideBitmapExtractPreRotationDimensions
255 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
256 $result = $this->handler
->extractPreRotationDimensions( [
257 'physicalWidth' => self
::TEST_WIDTH
,
258 'physicalHeight' => self
::TEST_HEIGHT
,
260 $this->assertEquals( $expected, $result );
263 public static function provideBitmapExtractPreRotationDimensions() {
267 [ self
::TEST_WIDTH
, self
::TEST_HEIGHT
]
271 [ self
::TEST_HEIGHT
, self
::TEST_WIDTH
]
275 [ self
::TEST_WIDTH
, self
::TEST_HEIGHT
]
279 [ self
::TEST_HEIGHT
, self
::TEST_WIDTH
]