]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - includes/media/Bitmap.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.git] / includes / media / Bitmap.php
index ca82aab0dd44e7a27a5a7c8be67ca8be0425462f..b949ae3dca3938fafbda97d8fc1c3b93836be798 100644 (file)
@@ -1,7 +1,11 @@
 <?php
 <?php
+/**
+ * @file
+ * @ingroup Media
+ */
 
 /**
 
 /**
- * @addtogroup Media
+ * @ingroup Media
  */
 class BitmapHandler extends ImageHandler {
        function normaliseParams( $image, &$params ) {
  */
 class BitmapHandler extends ImageHandler {
        function normaliseParams( $image, &$params ) {
@@ -26,7 +30,7 @@ class BitmapHandler extends ImageHandler {
                # Don't make an image bigger than the source
                $params['physicalWidth'] = $params['width'];
                $params['physicalHeight'] = $params['height'];
                # Don't make an image bigger than the source
                $params['physicalWidth'] = $params['width'];
                $params['physicalHeight'] = $params['height'];
-               
+
                if ( $params['physicalWidth'] >= $srcWidth ) {
                        $params['physicalWidth'] = $srcWidth;
                        $params['physicalHeight'] = $srcHeight;
                if ( $params['physicalWidth'] >= $srcWidth ) {
                        $params['physicalWidth'] = $srcWidth;
                        $params['physicalHeight'] = $srcHeight;
@@ -35,11 +39,12 @@ class BitmapHandler extends ImageHandler {
 
                return true;
        }
 
                return true;
        }
-       
+
        function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
        function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
-               global $wgUseImageMagick, $wgImageMagickConvertCommand;
+               global $wgUseImageMagick, $wgImageMagickConvertCommand, $wgImageMagickTempDir;
                global $wgCustomConvertCommand;
                global $wgSharpenParameter, $wgSharpenReductionThreshold;
                global $wgCustomConvertCommand;
                global $wgSharpenParameter, $wgSharpenReductionThreshold;
+               global $wgMaxAnimatedGifArea;
 
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
 
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
@@ -55,7 +60,7 @@ class BitmapHandler extends ImageHandler {
                $retval = 0;
                wfDebug( __METHOD__.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
 
                $retval = 0;
                wfDebug( __METHOD__.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
 
-               if ( $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
+               if ( !$image->mustRender() && $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
                        # normaliseParams (or the user) wants us to return the unscaled image
                        wfDebug( __METHOD__.": returning unscaled image\n" );
                        return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
                        # normaliseParams (or the user) wants us to return the unscaled image
                        wfDebug( __METHOD__.": returning unscaled image\n" );
                        return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
@@ -73,6 +78,7 @@ class BitmapHandler extends ImageHandler {
                } else {
                        $scaler = 'client';
                }
                } else {
                        $scaler = 'client';
                }
+               wfDebug( __METHOD__.": scaler $scaler\n" );
 
                if ( $scaler == 'client' ) {
                        # Client-side image scaling, use the source URL
 
                if ( $scaler == 'client' ) {
                        # Client-side image scaling, use the source URL
@@ -81,18 +87,22 @@ class BitmapHandler extends ImageHandler {
                }
 
                if ( $flags & self::TRANSFORM_LATER ) {
                }
 
                if ( $flags & self::TRANSFORM_LATER ) {
+                       wfDebug( __METHOD__.": Transforming later per flags.\n" );
                        return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
 
                if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
                        return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
 
                if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
-                       return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, 
-                               wfMsg( 'thumbnail_dest_directory' ) );
+                       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
 
                }
 
                if ( $scaler == 'im' ) {
                        # use ImageMagick
 
+                       $quality = '';
                        $sharpen = '';
                        $sharpen = '';
+                       $frame = '';
+                       $animation = '';
                        if ( $mimeType == 'image/jpeg' ) {
                                $quality = "-quality 80"; // 80%
                                # Sharpening, see bug 6193
                        if ( $mimeType == 'image/jpeg' ) {
                                $quality = "-quality 80"; // 80%
                                # Sharpening, see bug 6193
@@ -101,8 +111,21 @@ class BitmapHandler extends ImageHandler {
                                }
                        } elseif ( $mimeType == 'image/png' ) {
                                $quality = "-quality 95"; // zlib 9, adaptive filtering
                                }
                        } elseif ( $mimeType == 'image/png' ) {
                                $quality = "-quality 95"; // zlib 9, adaptive filtering
+                       } 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 ';
+                               }
+                       }
+
+                       if ( strval( $wgImageMagickTempDir ) !== '' ) {
+                               $tempEnv = 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
                        } else {
                        } else {
-                               $quality = ''; // default
+                               $tempEnv = '';
                        }
 
                        # Specify white background color, will be used for transparent images
                        }
 
                        # Specify white background color, will be used for transparent images
@@ -112,11 +135,12 @@ class BitmapHandler extends ImageHandler {
                        # It seems that ImageMagick has a bug wherein it produces thumbnails of
                        # the wrong size in the second case.
 
                        # It seems that ImageMagick has a bug wherein it produces thumbnails of
                        # the wrong size in the second case.
 
-                       $cmd  =  wfEscapeShellArg($wgImageMagickConvertCommand) .
+                       $cmd  = 
+                               $tempEnv .
+                               wfEscapeShellArg($wgImageMagickConvertCommand) .
                                " {$quality} -background white -size {$physicalWidth} ".
                                " {$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.
                                // For the -resize option a "!" is needed to force exact size,
                                // or ImageMagick may decide your ratio is wrong and slice off
                                // a pixel.
@@ -167,12 +191,12 @@ class BitmapHandler extends ImageHandler {
 
                        $src_image = call_user_func( $loader, $srcPath );
                        $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
 
                        $src_image = call_user_func( $loader, $srcPath );
                        $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
-                       
+
                        // Initialise the destination image to transparent instead of
                        // the default solid black, to support PNG and GIF transparency nicely
                        $background = imagecolorallocate( $dst_image, 0, 0, 0 );
                        imagecolortransparent( $dst_image, $background );
                        // Initialise the destination image to transparent instead of
                        // the default solid black, to support PNG and GIF transparency nicely
                        $background = imagecolorallocate( $dst_image, 0, 0, 0 );
                        imagecolortransparent( $dst_image, $background );
-                       imagealphablending( $dst_image, false ); 
+                       imagealphablending( $dst_image, false );
 
                        if( $colorStyle == 'palette' ) {
                                // Don't resample for paletted GIF images.
 
                        if( $colorStyle == 'palette' ) {
                                // Don't resample for paletted GIF images.
@@ -187,7 +211,7 @@ class BitmapHandler extends ImageHandler {
                        }
 
                        imagesavealpha( $dst_image, true );
                        }
 
                        imagesavealpha( $dst_image, true );
-                       
+
                        call_user_func( $saveType, $dst_image, $dstPath );
                        imagedestroy( $dst_image );
                        imagedestroy( $src_image );
                        call_user_func( $saveType, $dst_image, $dstPath );
                        imagedestroy( $dst_image );
                        imagedestroy( $src_image );
@@ -303,5 +327,3 @@ class BitmapHandler extends ImageHandler {
                return $result;
        }
 }
                return $result;
        }
 }
-
-