80d5ff8f44254242f7adc3c20d6a03e76b7257ba
10 class BitmapHandler
extends ImageHandler
{
11 function normaliseParams( $image, &$params ) {
12 global $wgMaxImageArea;
13 if ( !parent
::normaliseParams( $image, $params ) ) {
17 $mimeType = $image->getMimeType();
18 $srcWidth = $image->getWidth( $params['page'] );
19 $srcHeight = $image->getHeight( $params['page'] );
21 # Don't thumbnail an image so big that it will fill hard drives and send servers into swap
22 # JPEG has the handy property of allowing thumbnailing without full decompression, so we make
23 # an exception for it.
24 if ( $mimeType !== 'image/jpeg' &&
25 $srcWidth * $srcHeight > $wgMaxImageArea )
30 # Don't make an image bigger than the source
31 $params['physicalWidth'] = $params['width'];
32 $params['physicalHeight'] = $params['height'];
34 if ( $params['physicalWidth'] >= $srcWidth ) {
35 $params['physicalWidth'] = $srcWidth;
36 $params['physicalHeight'] = $srcHeight;
43 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
44 global $wgUseImageMagick, $wgImageMagickConvertCommand, $wgImageMagickTempDir;
45 global $wgCustomConvertCommand, $wgUseImageResize;
46 global $wgSharpenParameter, $wgSharpenReductionThreshold;
47 global $wgMaxAnimatedGifArea;
49 if ( !$this->normaliseParams( $image, $params ) ) {
50 return new TransformParameterError( $params );
52 $physicalWidth = $params['physicalWidth'];
53 $physicalHeight = $params['physicalHeight'];
54 $clientWidth = $params['width'];
55 $clientHeight = $params['height'];
56 $srcWidth = $image->getWidth();
57 $srcHeight = $image->getHeight();
58 $mimeType = $image->getMimeType();
59 $srcPath = $image->getPath();
61 wfDebug( __METHOD__
.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
63 if ( !$image->mustRender() && $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
64 # normaliseParams (or the user) wants us to return the unscaled image
65 wfDebug( __METHOD__
.": returning unscaled image\n" );
66 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
70 // No output path available, client side scaling only
72 } elseif( !$wgUseImageResize ) {
74 } elseif ( $wgUseImageMagick ) {
76 } elseif ( $wgCustomConvertCommand ) {
78 } elseif ( function_exists( 'imagecreatetruecolor' ) ) {
83 wfDebug( __METHOD__
.": scaler $scaler\n" );
85 if ( $scaler == 'client' ) {
86 # Client-side image scaling, use the source URL
87 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
88 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
91 if ( $flags & self
::TRANSFORM_LATER
) {
92 wfDebug( __METHOD__
.": Transforming later per flags.\n" );
93 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
96 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
97 wfDebug( __METHOD__
.": Unable to create thumbnail destination directory, falling back to client scaling\n" );
98 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
101 if ( $scaler == 'im' ) {
108 if ( $mimeType == 'image/jpeg' ) {
109 $quality = "-quality 80"; // 80%
110 # Sharpening, see bug 6193
111 if ( ( $physicalWidth +
$physicalHeight ) / ( $srcWidth +
$srcHeight ) < $wgSharpenReductionThreshold ) {
112 $sharpen = "-sharpen " . wfEscapeShellArg( $wgSharpenParameter );
114 } elseif ( $mimeType == 'image/png' ) {
115 $quality = "-quality 95"; // zlib 9, adaptive filtering
116 } elseif( $mimeType == 'image/gif' ) {
117 if( $srcWidth * $srcHeight > $wgMaxAnimatedGifArea ) {
118 // Extract initial frame only; we're so big it'll
119 // be a total drag. :P
122 // Coalesce is needed to scale animated GIFs properly (bug 1017).
123 $animation = ' -coalesce ';
127 if ( strval( $wgImageMagickTempDir ) !== '' ) {
128 $tempEnv = 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
133 # Specify white background color, will be used for transparent images
134 # in Internet Explorer/Windows instead of default black.
136 # Note, we specify "-size {$physicalWidth}" and NOT "-size {$physicalWidth}x{$physicalHeight}".
137 # It seems that ImageMagick has a bug wherein it produces thumbnails of
138 # the wrong size in the second case.
142 wfEscapeShellArg($wgImageMagickConvertCommand) .
143 " {$quality} -background white -size {$physicalWidth} ".
144 wfEscapeShellArg($srcPath . $frame) .
146 // For the -resize option a "!" is needed to force exact size,
147 // or ImageMagick may decide your ratio is wrong and slice off
149 " -thumbnail " . wfEscapeShellArg( "{$physicalWidth}x{$physicalHeight}!" ) .
150 " -depth 8 $sharpen " .
151 wfEscapeShellArg($dstPath) . " 2>&1";
152 wfDebug( __METHOD__
.": running ImageMagick: $cmd\n");
153 wfProfileIn( 'convert' );
154 $err = wfShellExec( $cmd, $retval );
155 wfProfileOut( 'convert' );
156 } elseif( $scaler == 'custom' ) {
157 # Use a custom convert command
158 # Variables: %s %d %w %h
159 $src = wfEscapeShellArg( $srcPath );
160 $dst = wfEscapeShellArg( $dstPath );
161 $cmd = $wgCustomConvertCommand;
162 $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
163 $cmd = str_replace( '%h', $physicalHeight, str_replace( '%w', $physicalWidth, $cmd ) ); # Size
164 wfDebug( __METHOD__
.": Running custom convert command $cmd\n" );
165 wfProfileIn( 'convert' );
166 $err = wfShellExec( $cmd, $retval );
167 wfProfileOut( 'convert' );
168 } else /* $scaler == 'gd' */ {
169 # Use PHP's builtin GD library functions.
171 # First find out what kind of file this is, and select the correct
172 # input routine for this.
175 'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
176 'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__
, 'imageJpegWrapper' ) ),
177 'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
178 'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
179 'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
181 if( !isset( $typemap[$mimeType] ) ) {
182 $err = 'Image type not supported';
184 $errMsg = wfMsg ( 'thumbnail_image-type' );
185 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
187 list( $loader, $colorStyle, $saveType ) = $typemap[$mimeType];
189 if( !function_exists( $loader ) ) {
190 $err = "Incomplete GD library configuration: missing function $loader";
192 $errMsg = wfMsg ( 'thumbnail_gd-library', $loader );
193 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
196 if ( !file_exists( $srcPath ) ) {
197 $err = "File seems to be missing: $srcPath";
199 $errMsg = wfMsg ( 'thumbnail_image-missing', $srcPath );
200 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
203 $src_image = call_user_func( $loader, $srcPath );
204 $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
206 // Initialise the destination image to transparent instead of
207 // the default solid black, to support PNG and GIF transparency nicely
208 $background = imagecolorallocate( $dst_image, 0, 0, 0 );
209 imagecolortransparent( $dst_image, $background );
210 imagealphablending( $dst_image, false );
212 if( $colorStyle == 'palette' ) {
213 // Don't resample for paletted GIF images.
214 // It may just uglify them, and completely breaks transparency.
215 imagecopyresized( $dst_image, $src_image,
217 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
219 imagecopyresampled( $dst_image, $src_image,
221 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
224 imagesavealpha( $dst_image, true );
226 call_user_func( $saveType, $dst_image, $dstPath );
227 imagedestroy( $dst_image );
228 imagedestroy( $src_image );
232 $removed = $this->removeBadFile( $dstPath, $retval );
233 if ( $retval != 0 ||
$removed ) {
234 wfDebugLog( 'thumbnail',
235 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
236 wfHostname(), $retval, trim($err), $cmd ) );
237 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
239 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
243 static function imageJpegWrapper( $dst_image, $thumbPath ) {
244 imageinterlace( $dst_image );
245 imagejpeg( $dst_image, $thumbPath, 95 );
249 function getMetadata( $image, $filename ) {
251 if( $wgShowEXIF && file_exists( $filename ) ) {
252 $exif = new Exif( $filename );
253 $data = $exif->getFilteredData();
255 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
256 return serialize( $data );
265 function getMetadataType( $image ) {
269 function isMetadataValid( $image, $metadata ) {
271 if ( !$wgShowEXIF ) {
272 # Metadata disabled and so an empty field is expected
275 if ( $metadata === '0' ) {
276 # Special value indicating that there is no EXIF data in the file
279 $exif = @unserialize
( $metadata );
280 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
281 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif
::version() )
284 wfDebug( __METHOD__
.": wrong version\n" );
291 * Get a list of EXIF metadata items which should be displayed when
292 * the metadata table is collapsed.
294 * @return array of strings
297 function visibleMetadataFields() {
299 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
300 foreach( $lines as $line ) {
302 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
303 $fields[] = $matches[1];
306 $fields = array_map( 'strtolower', $fields );
310 function formatMetadata( $image ) {
312 'visible' => array(),
313 'collapsed' => array()
315 $metadata = $image->getMetadata();
319 $exif = unserialize( $metadata );
323 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
324 $format = new FormatExif( $exif );
326 $formatted = $format->getFormattedData();
327 // Sort fields into visible and collapsed
328 $visibleFields = $this->visibleMetadataFields();
329 foreach ( $formatted as $section => $tags ) {
330 foreach ( $tags as $name => $value ) {
331 $tag = strtolower( $name );
332 self
::addMeta( $result,
333 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',