r101677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101676‎ | r101677 | r101678 >
Date:20:48, 2 November 2011
Author:btongminh
Status:ok
Tags:
Comment:
Per comments on r99911 move $wgMaxImageArea check back to normaliseParams(). Added hook BitmapHandlerCheckImageArea to override the area check. I'm not very happy with this overly specific hook, but I don't see a clear way to obtain the functionallity required otherwise.

Remove the width and height params from BitmapHandler::getImageArea(). There is really no reason for them to be there.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.19 (modified) (history)
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/media/Bitmap.php (modified) (history)
  • /trunk/phase3/includes/media/GIF.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/media/BitmapScalingTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -624,6 +624,11 @@
625625 &$scalerParams: Array with scaler parameters
626626 &$mto: null, set to a MediaTransformOutput
627627
 628+'BitmapHandlerCheckImageArea': by BitmapHandler::normaliseParams, after all normalizations have been performed, except for the $wgMaxImageArea check
 629+$image: File
 630+&$params: Array of parameters
 631+&$checkImageAreaHookResult: null, set to true or false to override the $wgMaxImageArea check result
 632+
628633 'PerformRetroactiveAutoblock': called before a retroactive autoblock is applied to a user
629634 $block: Block object (which is set to be autoblocking)
630635 &$blockIds: Array of block IDs of the autoblock
Index: trunk/phase3/RELEASE-NOTES-1.19
@@ -76,6 +76,7 @@
7777 syntax.
7878 * The default user signature now contains a talk link in addition to the user link.
7979 * (bug 25306) Add link of old page title to MediaWiki:Delete_and_move_reason
 80+* Added hook BitmapHandlerCheckImageArea
8081
8182 === Bug fixes in 1.19 ===
8283 * $wgUploadNavigationUrl should be used for file redlinks if
Index: trunk/phase3/tests/phpunit/includes/media/BitmapScalingTest.php
@@ -119,6 +119,12 @@
120120 $this->assertEquals( 'TransformParameterError',
121121 get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) );
122122 }
 123+
 124+ function testImageArea() {
 125+ $file = new FakeDimensionFile( array( 7, 9 ) );
 126+ $handler = new BitmapHandler;
 127+ $this->assertEquals( 63, $handler->getImageArea( $file ) );
 128+ }
123129 }
124130
125131 class FakeDimensionFile extends File {
Index: trunk/phase3/includes/media/GIF.php
@@ -50,17 +50,16 @@
5151
5252 /**
5353 * @param $image File
54 - * @param $width
55 - * @param $height
56 - * @return
 54+ * @todo unittests
 55+ * @return bool
5756 */
58 - function getImageArea( $image, $width, $height ) {
 57+ function getImageArea( $image ) {
5958 $ser = $image->getMetadata();
6059 if ( $ser ) {
6160 $metadata = unserialize( $ser );
62 - return $width * $height * $metadata['frameCount'];
 61+ return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
6362 } else {
64 - return $width * $height;
 63+ return $image->getWidth() * $image->getHeight();
6564 }
6665 }
6766
Index: trunk/phase3/includes/media/Bitmap.php
@@ -12,7 +12,6 @@
1313 * @ingroup Media
1414 */
1515 class BitmapHandler extends ImageHandler {
16 -
1716 /**
1817 * @param $image File
1918 * @param $params array Transform parameters. Entries with the keys 'width'
@@ -21,7 +20,6 @@
2221 * @return bool
2322 */
2423 function normaliseParams( $image, &$params ) {
25 -
2624 if ( !parent::normaliseParams( $image, $params ) ) {
2725 return false;
2826 }
@@ -41,44 +39,29 @@
4240 return true;
4341 }
4442 }
 43+
4544
 45+ # Check if the file is smaller than the maximum image area for thumbnailing
 46+ $checkImageAreaHookResult = null;
 47+ wfRunHooks( 'BitmapHandlerCheckImageArea', array( $image, &$params, &$checkImageAreaHookResult ) );
 48+ if ( is_null( $checkImageAreaHookResult ) ) {
 49+ global $wgMaxImageArea;
 50+
 51+ if ( $this->getImageArea( $image ) > $wgMaxImageArea &&
 52+ !( $image->getMimeType() == 'image/jpeg' &&
 53+ self::getScalerType( false, false ) == 'im' ) ) {
 54+ # Only ImageMagick can efficiently downsize jpg images without loading
 55+ # the entire file in memory
 56+ return false;
 57+ }
 58+ } else {
 59+ return $checkImageAreaHookResult;
 60+ }
 61+
4662 return true;
4763 }
4864
49 - /**
50 - * Check if the file is smaller than the maximum image area for
51 - * thumbnailing. Check will always pass if the scaler is 'hookaborted' or
52 - * if the scaler is 'im' and the mime type is 'image/jpeg'
53 - *
54 - * @param File $image
55 - * @param string $scaler
56 - */
57 - function checkImageArea( $image, $scaler ) {
58 - global $wgMaxImageArea;
59 - # Don't thumbnail an image so big that it will fill hard drives and send servers into swap
60 - # JPEG has the handy property of allowing thumbnailing without full decompression, so we make
61 - # an exception for it.
6265
63 -
64 - if ( $image->getMimeType() == 'image/jpeg' && $scaler == 'im' )
65 - {
66 - # ImageMagick can efficiently downsize jpg images without loading
67 - # the entire file in memory
68 - return true;
69 - }
70 -
71 - if ( $scaler == 'hookaborted' )
72 - {
73 - # If a hook wants to transform the image, it is responsible for
74 - # checking the image size, so abort here
75 - return true;
76 - }
77 -
78 - # Do the actual check
79 - return $this->getImageArea( $image, $image->getWidth(), $image->getHeight() ) <= $wgMaxImageArea;
80 - }
81 -
82 -
8366 /**
8467 * Extracts the width/height if the image will be scaled before rotating
8568 *
@@ -104,10 +87,15 @@
10588 }
10689
10790
108 - // Function that returns the number of pixels to be thumbnailed.
109 - // Intended for animated GIFs to multiply by the number of frames.
110 - function getImageArea( $image, $width, $height ) {
111 - return $width * $height;
 91+ /**
 92+ * Function that returns the number of pixels to be thumbnailed.
 93+ * Intended for animated GIFs to multiply by the number of frames.
 94+ *
 95+ * @param File $image
 96+ * @return int
 97+ */
 98+ function getImageArea( $image ) {
 99+ return $image->getWidth() * $image->getHeight();
112100 }
113101
114102 /**
@@ -143,7 +131,10 @@
144132 'dstUrl' => $dstUrl,
145133 );
146134
147 - wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} thumbnail at $dstPath\n" );
 135+ # Determine scaler type
 136+ $scaler = self::getScalerType( $dstPath );
 137+
 138+ wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} thumbnail at $dstPath using scaler $scaler\n" );
148139
149140 if ( !$image->mustRender() &&
150141 $scalerParams['physicalWidth'] == $scalerParams['srcWidth']
@@ -154,9 +145,6 @@
155146 return $this->getClientScalingThumbnailImage( $image, $scalerParams );
156147 }
157148
158 - # Determine scaler type
159 - $scaler = self::getScalerType( $dstPath );
160 - wfDebug( __METHOD__ . ": scaler $scaler\n" );
161149
162150 if ( $scaler == 'client' ) {
163151 # Client-side image scaling, use the source URL
@@ -184,12 +172,6 @@
185173 $scaler = 'hookaborted';
186174 }
187175
188 - # Check max image area
189 - if ( !$this->checkImageArea( $image, $scaler ) )
190 - {
191 - return new TransformParameterError( $params );
192 - }
193 -
194176 switch ( $scaler ) {
195177 case 'hookaborted':
196178 # Handled by the hook above

Sign-offs

UserFlagDate
Aaron Schulzinspected21:05, 2 November 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r104387Fix for r101677: remove the extra parameters from the other getImageArea() ca...tstarling04:40, 28 November 2011
r104389MFT VipsScaler dependencies r99911, r99916, r99917, r101677, r104386, r104387...tstarling05:04, 28 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r99911Per bug 28135, disable $wgMaxImageArea check when transforming using a hook, ...btongminh20:36, 15 October 2011

Status & tagging log