Index: trunk/phase3/tests/phpunit/includes/media/BitmapScalingTest.php |
— | — | @@ -0,0 +1,96 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class BitmapScalingTest extends MediaWikiTestCase { |
| 5 | + /** |
| 6 | + * @dataProvider provideNormaliseParams |
| 7 | + */ |
| 8 | + function testNormaliseParams( $fileDimensions, $expectedParams, $params, $msg ) { |
| 9 | + $file = new FakeDimensionFile( $fileDimensions ); |
| 10 | + $handler = new BitmapHandler; |
| 11 | + $handler->normaliseParams( $file, $params ); |
| 12 | + $this->assertEquals( $expectedParams, $params, $msg ); |
| 13 | + } |
| 14 | + |
| 15 | + function provideNormaliseParams() { |
| 16 | + return array( |
| 17 | + /* Regular resize operations */ |
| 18 | + array( |
| 19 | + array( 1024, 768 ), |
| 20 | + array( |
| 21 | + 'width' => 512, 'height' => 384, |
| 22 | + 'physicalWidth' => 512, 'physicalHeight' => 384, |
| 23 | + 'page' => 1, |
| 24 | + ), |
| 25 | + array( 'width' => 512 ), |
| 26 | + 'Resizing with width set', |
| 27 | + ), |
| 28 | + array( |
| 29 | + array( 1024, 768 ), |
| 30 | + array( |
| 31 | + 'width' => 512, 'height' => 384, |
| 32 | + 'physicalWidth' => 512, 'physicalHeight' => 384, |
| 33 | + 'page' => 1, |
| 34 | + ), |
| 35 | + array( 'width' => 512, 'height' => 768 ), |
| 36 | + 'Resizing with height set too high', |
| 37 | + ), |
| 38 | + array( |
| 39 | + array( 1024, 768 ), |
| 40 | + array( |
| 41 | + 'width' => 512, 'height' => 384, |
| 42 | + 'physicalWidth' => 512, 'physicalHeight' => 384, |
| 43 | + 'page' => 1, |
| 44 | + ), |
| 45 | + array( 'width' => 1024, 'height' => 384 ), |
| 46 | + 'Resizing with height set', |
| 47 | + ), |
| 48 | + |
| 49 | + /* Very tall images */ |
| 50 | + array( |
| 51 | + array( 1000, 100 ), |
| 52 | + array( |
| 53 | + 'width' => 5, 'height' => 1, |
| 54 | + 'physicalWidth' => 5, 'physicalHeight' => 1, |
| 55 | + 'page' => 1, |
| 56 | + ), |
| 57 | + array( 'width' => 5 ), |
| 58 | + 'Very wide image', |
| 59 | + ), |
| 60 | + |
| 61 | + array( |
| 62 | + array( 100, 1000 ), |
| 63 | + array( |
| 64 | + 'width' => 1, 'height' => 10, |
| 65 | + 'physicalWidth' => 1, 'physicalHeight' => 10, |
| 66 | + 'page' => 1, |
| 67 | + ), |
| 68 | + array( 'width' => 1 ), |
| 69 | + 'Very high image', |
| 70 | + ), |
| 71 | + array( |
| 72 | + array( 100, 1000 ), |
| 73 | + array( |
| 74 | + 'width' => 1, 'height' => 5, |
| 75 | + 'physicalWidth' => 1, 'physicalHeight' => 10, |
| 76 | + 'page' => 1, |
| 77 | + ), |
| 78 | + array( 'width' => 10, 'height' => 5 ), |
| 79 | + 'Very high image with height set', |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class FakeDimensionFile extends File { |
| 86 | + public function __construct( $dimensions ) { |
| 87 | + parent::__construct( Title::makeTitle( NS_FILE, 'Test' ), null ); |
| 88 | + |
| 89 | + $this->dimensions = $dimensions; |
| 90 | + } |
| 91 | + public function getWidth( $page = 1 ) { |
| 92 | + return $this->dimensions[0]; |
| 93 | + } |
| 94 | + public function getHeight( $page = 1 ) { |
| 95 | + return $this->dimensions[1]; |
| 96 | + } |
| 97 | +} |
\ No newline at end of file |
Property changes on: trunk/phase3/tests/phpunit/includes/media/BitmapScalingTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 98 | + native |
Index: trunk/phase3/includes/media/SVG.php |
— | — | @@ -59,8 +59,6 @@ |
60 | 60 | return false; |
61 | 61 | } |
62 | 62 | # Don't make an image bigger than wgMaxSVGSize |
63 | | - $params['physicalWidth'] = $params['width']; |
64 | | - $params['physicalHeight'] = $params['height']; |
65 | 63 | if ( $params['physicalWidth'] > $wgSVGMaxSize ) { |
66 | 64 | $srcWidth = $image->getWidth( $params['page'] ); |
67 | 65 | $srcHeight = $image->getHeight( $params['page'] ); |
Index: trunk/phase3/includes/media/DjVu.php |
— | — | @@ -147,7 +147,8 @@ |
148 | 148 | |
149 | 149 | # Use a subshell (brackets) to aggregate stderr from both pipeline commands |
150 | 150 | # before redirecting it to the overall stdout. This works in both Linux and Windows XP. |
151 | | - $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " . |
| 151 | + $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" . |
| 152 | + " -size={$params['physicalWidth']}x{$params['physicalHeight']} " . |
152 | 153 | wfEscapeShellArg( $srcPath ); |
153 | 154 | if ( $wgDjvuPostProcessor ) { |
154 | 155 | $cmd .= " | {$wgDjvuPostProcessor}"; |
Index: trunk/phase3/includes/media/Bitmap.php |
— | — | @@ -35,16 +35,14 @@ |
36 | 36 | wfDebug( __METHOD__ . ": Swapping width and height because the file will be rotated $rotation degrees\n" ); |
37 | 37 | |
38 | 38 | $swapDimensions = true; |
39 | | - $width = $params['width']; |
40 | | - $params['width'] = $params['height']; |
41 | | - $params['height'] = $width; |
| 39 | + list( $params['width'], $params['height'] ) = |
| 40 | + array( $params['width'], $params['height'] ); |
| 41 | + list( $params['physicalWidth'], $params['physicalHeight'] ) = |
| 42 | + array( $params['physicalWidth'], $params['physicalHeight'] ); |
42 | 43 | } |
43 | 44 | } |
44 | 45 | |
45 | 46 | # Don't make an image bigger than the source |
46 | | - $params['physicalWidth'] = $params['width']; |
47 | | - $params['physicalHeight'] = $params['height']; |
48 | | - |
49 | 47 | if ( $params['physicalWidth'] >= $srcWidth ) { |
50 | 48 | if ( $swapDimensions ) { |
51 | 49 | $params['physicalWidth'] = $srcHeight; |
— | — | @@ -53,10 +51,11 @@ |
54 | 52 | $params['physicalWidth'] = $srcWidth; |
55 | 53 | $params['physicalHeight'] = $srcHeight; |
56 | 54 | } |
57 | | - # Skip scaling limit checks if no scaling is required |
58 | | - if ( !$image->mustRender() ) |
59 | | - return true; |
60 | 55 | } |
| 56 | + |
| 57 | + # Skip scaling limit checks if no scaling is required |
| 58 | + if ( !$image->mustRender() ) |
| 59 | + return true; |
61 | 60 | |
62 | 61 | # Don't thumbnail an image so big that it will fill hard drives and send servers into swap |
63 | 62 | # JPEG has the handy property of allowing thumbnailing without full decompression, so we make |
Index: trunk/phase3/includes/media/Generic.php |
— | — | @@ -570,13 +570,44 @@ |
571 | 571 | |
572 | 572 | $srcWidth = $image->getWidth( $params['page'] ); |
573 | 573 | $srcHeight = $image->getHeight( $params['page'] ); |
| 574 | + |
574 | 575 | if ( isset( $params['height'] ) && $params['height'] != -1 ) { |
| 576 | + # Height & width were both set |
575 | 577 | if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) { |
| 578 | + # Height is the relative smaller dimension, so scale width accordingly |
576 | 579 | $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] ); |
| 580 | + |
| 581 | + if ( $params['width'] == 0 ) { |
| 582 | + # Very small image, so we need to rely on client side scaling :( |
| 583 | + $params['width'] = 1; |
| 584 | + } |
| 585 | + |
| 586 | + $params['physicalWidth'] = $params['width']; |
| 587 | + } else { |
| 588 | + # Height was crap, unset it so that it will be calculated later |
| 589 | + unset( $params['height'] ); |
577 | 590 | } |
578 | 591 | } |
579 | | - $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] ); |
580 | | - if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) { |
| 592 | + |
| 593 | + if ( !isset( $params['physicalWidth'] ) ) { |
| 594 | + # Passed all validations, so set the physicalWidth |
| 595 | + $params['physicalWidth'] = $params['width']; |
| 596 | + } |
| 597 | + |
| 598 | + # Because thumbs are only referred to by width, the height always needs |
| 599 | + # to be scaled by the width to keep the thumbnail sizes consistent, |
| 600 | + # even if it was set inside the if block above |
| 601 | + $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, |
| 602 | + $params['physicalWidth'] ); |
| 603 | + |
| 604 | + # Set the height if it was not validated in the if block higher up |
| 605 | + if ( !isset( $params['height'] ) || $params['height'] == -1 ) { |
| 606 | + $params['height'] = $params['physicalHeight']; |
| 607 | + } |
| 608 | + |
| 609 | + |
| 610 | + if ( !$this->validateThumbParams( $params['physicalWidth'], |
| 611 | + $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) { |
581 | 612 | return false; |
582 | 613 | } |
583 | 614 | return true; |
— | — | @@ -613,6 +644,10 @@ |
614 | 645 | } |
615 | 646 | |
616 | 647 | $height = File::scaleHeight( $srcWidth, $srcHeight, $width ); |
| 648 | + if ( $height == 0 ) { |
| 649 | + # Force height to be at least 1 pixel |
| 650 | + $height = 1; |
| 651 | + } |
617 | 652 | return true; |
618 | 653 | } |
619 | 654 | |