API: Fix list=deletedrevs paging bug pointed out by Splarka on IRC
[lhc/web/wiklou.git] / includes / media / Bitmap.php
index 5235521..80d5ff8 100644 (file)
@@ -1,7 +1,11 @@
 <?php
+/**
+ * @file
+ * @ingroup Media
+ */
 
 /**
- * @addtogroup Media
+ * @ingroup Media
  */
 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'];
-               
+
                if ( $params['physicalWidth'] >= $srcWidth ) {
                        $params['physicalWidth'] = $srcWidth;
                        $params['physicalHeight'] = $srcHeight;
@@ -35,11 +39,12 @@ class BitmapHandler extends ImageHandler {
 
                return true;
        }
-       
+
        function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
-               global $wgUseImageMagick, $wgImageMagickConvertCommand;
-               global $wgCustomConvertCommand;
+               global $wgUseImageMagick, $wgImageMagickConvertCommand, $wgImageMagickTempDir;
+               global $wgCustomConvertCommand, $wgUseImageResize;
                global $wgSharpenParameter, $wgSharpenReductionThreshold;
+               global $wgMaxAnimatedGifArea;
 
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
@@ -51,17 +56,22 @@ class BitmapHandler extends ImageHandler {
                $srcWidth = $image->getWidth();
                $srcHeight = $image->getHeight();
                $mimeType = $image->getMimeType();
-               $srcPath = $image->getImagePath();
+               $srcPath = $image->getPath();
                $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->getURL(), $clientWidth, $clientHeight, $srcPath );
+                       return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
                }
 
-               if ( $wgUseImageMagick ) {
+               if ( !$dstPath ) {
+                       // No output path available, client side scaling only
+                       $scaler = 'client';
+               } elseif( !$wgUseImageResize ) {
+                       $scaler = 'client';
+               } elseif ( $wgUseImageMagick ) {
                        $scaler = 'im';
                } elseif ( $wgCustomConvertCommand ) {
                        $scaler = 'custom';
@@ -70,26 +80,31 @@ class BitmapHandler extends ImageHandler {
                } else {
                        $scaler = 'client';
                }
+               wfDebug( __METHOD__.": scaler $scaler\n" );
 
                if ( $scaler == 'client' ) {
                        # Client-side image scaling, use the source URL
                        # Using the destination URL in a TRANSFORM_LATER request would be incorrect
-                       return new ThumbnailImage( $image->getURL(), $clientWidth, $clientHeight, $srcPath );
+                       return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
                }
 
                if ( $flags & self::TRANSFORM_LATER ) {
-                       return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
+                       wfDebug( __METHOD__.": Transforming later per flags.\n" );
+                       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
 
+                       $quality = '';
                        $sharpen = '';
+                       $frame = '';
+                       $animation = '';
                        if ( $mimeType == 'image/jpeg' ) {
                                $quality = "-quality 80"; // 80%
                                # Sharpening, see bug 6193
@@ -98,8 +113,21 @@ class BitmapHandler extends ImageHandler {
                                }
                        } 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 {
-                               $quality = ''; // default
+                               $tempEnv = '';
                        }
 
                        # Specify white background color, will be used for transparent images
@@ -109,11 +137,12 @@ class BitmapHandler extends ImageHandler {
                        # 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} ".
-                               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.
@@ -152,21 +181,48 @@ class BitmapHandler extends ImageHandler {
                        if( !isset( $typemap[$mimeType] ) ) {
                                $err = 'Image type not supported';
                                wfDebug( "$err\n" );
-                               return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
+                               $errMsg = wfMsg ( 'thumbnail_image-type' );
+                               return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
                        }
                        list( $loader, $colorStyle, $saveType ) = $typemap[$mimeType];
 
                        if( !function_exists( $loader ) ) {
                                $err = "Incomplete GD library configuration: missing function $loader";
                                wfDebug( "$err\n" );
-                               return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
+                               $errMsg = wfMsg ( 'thumbnail_gd-library', $loader );
+                               return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
+                       }
+
+                       if ( !file_exists( $srcPath ) ) {
+                               $err = "File seems to be missing: $srcPath";
+                               wfDebug( "$err\n" );
+                               $errMsg = wfMsg ( 'thumbnail_image-missing', $srcPath );
+                               return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
                        }
 
                        $src_image = call_user_func( $loader, $srcPath );
                        $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
-                       imagecopyresampled( $dst_image, $src_image,
-                                               0,0,0,0,
-                                               $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
+
+                       // 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 );
+
+                       if( $colorStyle == 'palette' ) {
+                               // Don't resample for paletted GIF images.
+                               // It may just uglify them, and completely breaks transparency.
+                               imagecopyresized( $dst_image, $src_image,
+                                       0,0,0,0,
+                                       $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
+                       } else {
+                               imagecopyresampled( $dst_image, $src_image,
+                                       0,0,0,0,
+                                       $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
+                       }
+
+                       imagesavealpha( $dst_image, true );
+
                        call_user_func( $saveType, $dst_image, $dstPath );
                        imagedestroy( $dst_image );
                        imagedestroy( $src_image );
@@ -180,7 +236,7 @@ class BitmapHandler extends ImageHandler {
                                        wfHostname(), $retval, trim($err), $cmd ) );
                        return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
                } else {
-                       return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
+                       return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
        }
 
@@ -225,11 +281,62 @@ class BitmapHandler extends ImageHandler {
                        $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
                {
                        # Wrong version
+                       wfDebug( __METHOD__.": wrong version\n" );
                        return false;
                }
                return true;
        }
 
-}
+       /**
+        * Get a list of EXIF metadata items which should be displayed when
+        * the metadata table is collapsed.
+        *
+        * @return array of strings
+        * @access private
+        */
+       function visibleMetadataFields() {
+               $fields = array();
+               $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
+               foreach( $lines as $line ) {
+                       $matches = array();
+                       if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
+                               $fields[] = $matches[1];
+                       }
+               }
+               $fields = array_map( 'strtolower', $fields );
+               return $fields;
+       }
 
-?>
+       function formatMetadata( $image ) {
+               $result = array(
+                       'visible' => array(),
+                       'collapsed' => array()
+               );
+               $metadata = $image->getMetadata();
+               if ( !$metadata ) {
+                       return false;
+               }
+               $exif = unserialize( $metadata );
+               if ( !$exif ) {
+                       return false;
+               }
+               unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
+               $format = new FormatExif( $exif );
+
+               $formatted = $format->getFormattedData();
+               // Sort fields into visible and collapsed
+               $visibleFields = $this->visibleMetadataFields();
+               foreach ( $formatted as $section => $tags ) {
+                       foreach ( $tags as $name => $value ) {
+                               $tag = strtolower( $name );
+                               self::addMeta( $result,
+                                       in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
+                                       'exif',
+                                       $tag,
+                                       $value
+                               );
+                       }
+               }
+               return $result;
+       }
+}