Quick hack: add $wgMaxAnimatedGifArea so we don't try to render animated thumbs for...
authorBrion Vibber <brion@users.mediawiki.org>
Mon, 13 Oct 2008 21:50:16 +0000 (21:50 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Mon, 13 Oct 2008 21:50:16 +0000 (21:50 +0000)
'Tugra Mahmuds II Big Size.gif' on Commons otherwise seems to eat as much CPU and memory as you can throw at it (1,876 × 1,500 pixel, file size: 4.36 MB, MIME type: image/gif)

includes/DefaultSettings.php
includes/media/Bitmap.php

index 0a2b817..123e23a 100644 (file)
@@ -1956,6 +1956,13 @@ $wgSVGMaxSize = 2048;
  * 12.5 million pixels or 3500x3500
  */
 $wgMaxImageArea = 1.25e7;
+/**
+ * Force thumbnailing of animated GIFs above this size to a single
+ * frame instead of an animated thumbnail. ImageMagick seems to
+ * get real unhappy and doesn't play well with resource limits. :P
+ * Defaulting to 1 megapixel (1000x1000)
+ */
+$wgMaxAnimatedGifArea = 1.0e6;
 /**
  * If rendered thumbnail files are older than this timestamp, they
  * will be rerendered on demand as if the file didn't already exist.
index 39d70a6..9dbc40d 100644 (file)
@@ -44,6 +44,7 @@ class BitmapHandler extends ImageHandler {
                global $wgUseImageMagick, $wgImageMagickConvertCommand;
                global $wgCustomConvertCommand;
                global $wgSharpenParameter, $wgSharpenReductionThreshold;
+               global $wgMaxAnimatedGifArea;
 
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
@@ -77,6 +78,7 @@ class BitmapHandler extends ImageHandler {
                } else {
                        $scaler = 'client';
                }
+               wfDebug( __METHOD__.": scaler $scaler\n" );
 
                if ( $scaler == 'client' ) {
                        # Client-side image scaling, use the source URL
@@ -85,18 +87,22 @@ class BitmapHandler extends ImageHandler {
                }
 
                if ( $flags & self::TRANSFORM_LATER ) {
+                       wfDebug( __METHOD__.": Transforming later per flags.\n" );
                        return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
 
                if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
-                       wfDebug( "Unable to create thumbnail destination directory, falling back to client scaling\n" );
+                       wfDebug( __METHOD__.": Unable to create thumbnail destination directory, falling back to client scaling\n" );
                        return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
                }
 
                if ( $scaler == 'im' ) {
                        # use ImageMagick
 
+                       $quality = '';
                        $sharpen = '';
+                       $frame = '';
+                       $animation = '';
                        if ( $mimeType == 'image/jpeg' ) {
                                $quality = "-quality 80"; // 80%
                                # Sharpening, see bug 6193
@@ -105,8 +111,15 @@ class BitmapHandler extends ImageHandler {
                                }
                        } elseif ( $mimeType == 'image/png' ) {
                                $quality = "-quality 95"; // zlib 9, adaptive filtering
-                       } else {
-                               $quality = ''; // default
+                       } elseif( $mimeType == 'image/gif' ) {
+                               if( $srcWidth * $srcHeight > $wgMaxAnimatedGifArea ) {
+                                       // Extract initial frame only; we're so big it'll
+                                       // be a total drag. :P
+                                       $frame = '[0]';
+                               } else {
+                                       // Coalesce is needed to scale animated GIFs properly (bug 1017).
+                                       $animation = ' -coalesce ';
+                               }
                        }
 
                        # Specify white background color, will be used for transparent images
@@ -118,9 +131,8 @@ class BitmapHandler extends ImageHandler {
 
                        $cmd  =  wfEscapeShellArg($wgImageMagickConvertCommand) .
                                " {$quality} -background white -size {$physicalWidth} ".
-                               wfEscapeShellArg($srcPath) .
-                               // Coalesce is needed to scale animated GIFs properly (bug 1017).
-                               ' -coalesce ' .
+                               wfEscapeShellArg($srcPath . $frame) .
+                               $animation .
                                // For the -resize option a "!" is needed to force exact size,
                                // or ImageMagick may decide your ratio is wrong and slice off
                                // a pixel.