X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fmedia%2FBitmap.php;h=39d70a659430bcb61f9aa9e32a327f3dda834e96;hb=56aa7029b972b19421addc477a81a5c133aae79c;hp=52355213c68c47183a085ddb5ab0b44fa6f1058e;hpb=bd23ec29c6f3d34b4ef45a8a70921660de2282aa;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/Bitmap.php b/includes/media/Bitmap.php index 52355213c6..39d70a6594 100644 --- a/includes/media/Bitmap.php +++ b/includes/media/Bitmap.php @@ -1,7 +1,11 @@ = $srcWidth ) { $params['physicalWidth'] = $srcWidth; $params['physicalHeight'] = $srcHeight; @@ -35,7 +39,7 @@ class BitmapHandler extends ImageHandler { return true; } - + function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { global $wgUseImageMagick, $wgImageMagickConvertCommand; global $wgCustomConvertCommand; @@ -51,17 +55,20 @@ 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 ( $wgUseImageMagick ) { $scaler = 'im'; } elseif ( $wgCustomConvertCommand ) { $scaler = 'custom'; @@ -74,16 +81,16 @@ class BitmapHandler extends ImageHandler { 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 ); + return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); } if ( !wfMkdirParents( dirname( $dstPath ) ) ) { - return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, - wfMsg( 'thumbnail_dest_directory' ) ); + wfDebug( "Unable to create thumbnail destination directory, falling back to client scaling\n" ); + return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath ); } if ( $scaler == 'im' ) { @@ -164,9 +171,27 @@ class BitmapHandler extends ImageHandler { $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 +205,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 +250,60 @@ 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 $name => $value ) { + $tag = strtolower( $name ); + self::addMeta( $result, + in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', + 'exif', + $tag, + $value + ); + } + return $result; + } +}