Index: trunk/phase3/tests/phpunit/includes/media/ExifRotationTest.php |
— | — | @@ -1,11 +1,12 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * @group Broken |
| 5 | + * Tests related to auto rotation |
6 | 6 | */ |
7 | 7 | class ExifRotationTest extends MediaWikiTestCase { |
8 | 8 | |
9 | 9 | function setUp() { |
| 10 | + parent::setUp(); |
10 | 11 | } |
11 | 12 | |
12 | 13 | /** |
— | — | @@ -14,10 +15,32 @@ |
15 | 16 | */ |
16 | 17 | function testMetadata( $name, $type, $info ) { |
17 | 18 | $handler = new BitmapHandler(); |
18 | | - |
| 19 | + # Force client side resizing |
| 20 | + $params = array( 'width' => 10000, 'height' => 10000 ); |
19 | 21 | $file = UnregisteredLocalFile::newFromPath( dirname( __FILE__ ) . '/' . $name, $type ); |
20 | | - $this->assertEquals( $file->getWidth(), $info['width'], "$name: width check" ); |
21 | | - $this->assertEquals( $file->getHeight(), $info['height'], "$name: height check" ); |
| 22 | + |
| 23 | + # Normalize parameters |
| 24 | + $this->assertTrue( $handler->normaliseParams( $file, $params ) ); |
| 25 | + $rotation = $handler->getRotation( $file ); |
| 26 | + |
| 27 | + # Check if pre-rotation dimensions are still good |
| 28 | + list( $width, $height ) = $handler->extractPreRotationDimensions( $params, $rotation ); |
| 29 | + $this->assertEquals( $file->getWidth(), $width, |
| 30 | + "$name: pre-rotation width check, $rotation:$width" ); |
| 31 | + $this->assertEquals( $file->getHeight(), $height, |
| 32 | + "$name: pre-rotation height check, $rotation" ); |
| 33 | + |
| 34 | + # Any further test require a scaler that can rotate |
| 35 | + if ( !BitmapHandler::canRotate() ) { |
| 36 | + $this->markTestIncomplete( 'Scaler does not support rotation' ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + # Check post-rotation width |
| 41 | + $this->assertEquals( $params['physicalWidth'], $info['width'], |
| 42 | + "$name: post-rotation width check" ); |
| 43 | + $this->assertEquals( $params['physicalHeight'], $info['height'], |
| 44 | + "$name: post-rotation height check" ); |
22 | 45 | } |
23 | 46 | |
24 | 47 | function providerFiles() { |
— | — | @@ -40,5 +63,42 @@ |
41 | 64 | ) |
42 | 65 | ); |
43 | 66 | } |
| 67 | + |
| 68 | + |
| 69 | + const TEST_WIDTH = 100; |
| 70 | + const TEST_HEIGHT = 200; |
| 71 | + |
| 72 | + /** |
| 73 | + * @dataProvider provideBitmapExtractPreRotationDimensions |
| 74 | + */ |
| 75 | + function testBitmapExtractPreRotationDimensions( $rotation, $expected ) { |
| 76 | + $handler = new BitmapHandler; |
| 77 | + $result = $handler->extractPreRotationDimensions( array( |
| 78 | + 'physicalWidth' => self::TEST_WIDTH, |
| 79 | + 'physicalHeight' => self::TEST_HEIGHT, |
| 80 | + ), $rotation ); |
| 81 | + $this->assertEquals( $expected, $result ); |
| 82 | + } |
| 83 | + |
| 84 | + function provideBitmapExtractPreRotationDimensions() { |
| 85 | + return array( |
| 86 | + array( |
| 87 | + 0, |
| 88 | + array( self::TEST_WIDTH, self::TEST_HEIGHT ) |
| 89 | + ), |
| 90 | + array( |
| 91 | + 90, |
| 92 | + array( self::TEST_HEIGHT, self::TEST_WIDTH ) |
| 93 | + ), |
| 94 | + array( |
| 95 | + 180, |
| 96 | + array( self::TEST_WIDTH, self::TEST_HEIGHT ) |
| 97 | + ), |
| 98 | + array( |
| 99 | + 270, |
| 100 | + array( self::TEST_HEIGHT, self::TEST_WIDTH ) |
| 101 | + ), |
| 102 | + ); |
| 103 | + } |
44 | 104 | } |
45 | 105 | |
Index: trunk/phase3/includes/media/Bitmap.php |
— | — | @@ -28,11 +28,13 @@ |
29 | 29 | $srcWidth = $image->getWidth( $params['page'] ); |
30 | 30 | $srcHeight = $image->getHeight( $params['page'] ); |
31 | 31 | |
| 32 | + $swapDimensions = false; |
32 | 33 | if ( self::canRotate() ) { |
33 | 34 | $rotation = $this->getRotation( $image ); |
34 | 35 | if ( $rotation == 90 || $rotation == 270 ) { |
35 | 36 | wfDebug( __METHOD__ . ": Swapping width and height because the file will be rotated $rotation degrees\n" ); |
36 | 37 | |
| 38 | + $swapDimensions = true; |
37 | 39 | $width = $params['width']; |
38 | 40 | $params['width'] = $params['height']; |
39 | 41 | $params['height'] = $width; |
— | — | @@ -44,8 +46,13 @@ |
45 | 47 | $params['physicalHeight'] = $params['height']; |
46 | 48 | |
47 | 49 | if ( $params['physicalWidth'] >= $srcWidth ) { |
48 | | - $params['physicalWidth'] = $srcWidth; |
49 | | - $params['physicalHeight'] = $srcHeight; |
| 50 | + if ( $swapDimensions ) { |
| 51 | + $params['physicalWidth'] = $srcHeight; |
| 52 | + $params['physicalHeight'] = $srcWidth; |
| 53 | + } else { |
| 54 | + $params['physicalWidth'] = $srcWidth; |
| 55 | + $params['physicalHeight'] = $srcHeight; |
| 56 | + } |
50 | 57 | # Skip scaling limit checks if no scaling is required |
51 | 58 | if ( !$image->mustRender() ) |
52 | 59 | return true; |
— | — | @@ -63,6 +70,25 @@ |
64 | 71 | |
65 | 72 | return true; |
66 | 73 | } |
| 74 | + |
| 75 | + /** |
| 76 | + * Extracts the width/height if the image will be scaled before rotating |
| 77 | + * |
| 78 | + * @param $params array Parameters as returned by normaliseParams |
| 79 | + * @param $rotation int The rotation angle that will be applied |
| 80 | + * @return array ($width, $height) array |
| 81 | + */ |
| 82 | + public function extractPreRotationDimensions( $params, $rotation ) { |
| 83 | + if ( $rotation == 90 || $rotation == 270 ) { |
| 84 | + # We'll resize before rotation, so swap the dimensions again |
| 85 | + $width = $params['physicalHeight']; |
| 86 | + $height = $params['physicalWidth']; |
| 87 | + } else { |
| 88 | + $width = $params['physicalWidth']; |
| 89 | + $height = $params['physicalHeight']; |
| 90 | + } |
| 91 | + return array( $width, $height ); |
| 92 | + } |
67 | 93 | |
68 | 94 | |
69 | 95 | // Function that returns the number of pixels to be thumbnailed. |
— | — | @@ -287,6 +313,9 @@ |
288 | 314 | if ( strval( $wgImageMagickTempDir ) !== '' ) { |
289 | 315 | $env['MAGICK_TMPDIR'] = $wgImageMagickTempDir; |
290 | 316 | } |
| 317 | + |
| 318 | + $rotation = $this->getRotation( $image ); |
| 319 | + list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation ); |
291 | 320 | |
292 | 321 | $cmd = |
293 | 322 | wfEscapeShellArg( $wgImageMagickConvertCommand ) . |
— | — | @@ -299,7 +328,7 @@ |
300 | 329 | // For the -thumbnail option a "!" is needed to force exact size, |
301 | 330 | // or ImageMagick may decide your ratio is wrong and slice off |
302 | 331 | // a pixel. |
303 | | - " -thumbnail " . wfEscapeShellArg( "{$params['physicalDimensions']}!" ) . |
| 332 | + " -thumbnail " . wfEscapeShellArg( "{$width}x{$height}!" ) . |
304 | 333 | // Add the source url as a comment to the thumb, but don't add the flag if there's no comment |
305 | 334 | ( $params['comment'] !== '' |
306 | 335 | ? " -set comment " . wfEscapeShellArg( $this->escapeMagickProperty( $params['comment'] ) ) |
— | — | @@ -362,14 +391,7 @@ |
363 | 392 | } |
364 | 393 | |
365 | 394 | $rotation = $this->getRotation( $image ); |
366 | | - if ( $rotation == 90 || $rotation == 270 ) { |
367 | | - // We'll resize before rotation, so swap the dimensions again |
368 | | - $width = $params['physicalHeight']; |
369 | | - $height = $params['physicalWidth']; |
370 | | - } else { |
371 | | - $width = $params['physicalWidth']; |
372 | | - $height = $params['physicalHeight']; |
373 | | - } |
| 395 | + list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation ); |
374 | 396 | |
375 | 397 | $im->setImageBackgroundColor( new ImagickPixel( 'white' ) ); |
376 | 398 | |
— | — | @@ -509,14 +531,7 @@ |
510 | 532 | $src_image = call_user_func( $loader, $params['srcPath'] ); |
511 | 533 | |
512 | 534 | $rotation = function_exists( 'imagerotate' ) ? $this->getRotation( $image ) : 0; |
513 | | - if ( $rotation == 90 || $rotation == 270 ) { |
514 | | - # We'll resize before rotation, so swap the dimensions again |
515 | | - $width = $params['physicalHeight']; |
516 | | - $height = $params['physicalWidth']; |
517 | | - } else { |
518 | | - $width = $params['physicalWidth']; |
519 | | - $height = $params['physicalHeight']; |
520 | | - } |
| 535 | + list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation ); |
521 | 536 | $dst_image = imagecreatetruecolor( $width, $height ); |
522 | 537 | |
523 | 538 | // Initialise the destination image to transparent instead of |