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 $this->getImageArea( $image, $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;
44 // Function that returns the number of pixels to be thumbnailed.
45 // Intended for animated GIFs to multiply by the number of frames.
46 function getImageArea( $image, $width, $height ) {
47 return $width * $height;
50 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
51 global $wgUseImageMagick, $wgImageMagickConvertCommand, $wgImageMagickTempDir;
52 global $wgCustomConvertCommand, $wgUseImageResize;
53 global $wgSharpenParameter, $wgSharpenReductionThreshold;
54 global $wgMaxAnimatedGifArea;
56 if ( !$this->normaliseParams( $image, $params ) ) {
57 return new TransformParameterError( $params );
59 $physicalWidth = $params['physicalWidth'];
60 $physicalHeight = $params['physicalHeight'];
61 $clientWidth = $params['width'];
62 $clientHeight = $params['height'];
63 $srcWidth = $image->getWidth();
64 $srcHeight = $image->getHeight();
65 $mimeType = $image->getMimeType();
66 $srcPath = $image->getPath();
68 wfDebug( __METHOD__
.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
70 if ( !$image->mustRender() && $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
71 # normaliseParams (or the user) wants us to return the unscaled image
72 wfDebug( __METHOD__
.": returning unscaled image\n" );
73 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
77 // No output path available, client side scaling only
79 } elseif( !$wgUseImageResize ) {
81 } elseif ( $wgUseImageMagick ) {
83 } elseif ( $wgCustomConvertCommand ) {
85 } elseif ( function_exists( 'imagecreatetruecolor' ) ) {
90 wfDebug( __METHOD__
.": scaler $scaler\n" );
92 if ( $scaler == 'client' ) {
93 # Client-side image scaling, use the source URL
94 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
95 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
98 if ( $flags & self
::TRANSFORM_LATER
) {
99 wfDebug( __METHOD__
.": Transforming later per flags.\n" );
100 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
103 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
104 wfDebug( __METHOD__
.": Unable to create thumbnail destination directory, falling back to client scaling\n" );
105 return new ThumbnailImage( $image, $image->getURL(), $clientWidth, $clientHeight, $srcPath );
108 if ( $scaler == 'im' ) {
115 if ( $mimeType == 'image/jpeg' ) {
116 $quality = "-quality 80"; // 80%
117 # Sharpening, see bug 6193
118 if ( ( $physicalWidth +
$physicalHeight ) / ( $srcWidth +
$srcHeight ) < $wgSharpenReductionThreshold ) {
119 $sharpen = "-sharpen " . wfEscapeShellArg( $wgSharpenParameter );
121 } elseif ( $mimeType == 'image/png' ) {
122 $quality = "-quality 95"; // zlib 9, adaptive filtering
123 } elseif( $mimeType == 'image/gif' ) {
124 if( $srcWidth * $srcHeight > $wgMaxAnimatedGifArea ) {
125 // Extract initial frame only; we're so big it'll
126 // be a total drag. :P
129 // Coalesce is needed to scale animated GIFs properly (bug 1017).
130 $animation = ' -coalesce ';
134 if ( strval( $wgImageMagickTempDir ) !== '' ) {
135 $tempEnv = 'MAGICK_TMPDIR=' . wfEscapeShellArg( $wgImageMagickTempDir ) . ' ';
140 # Specify white background color, will be used for transparent images
141 # in Internet Explorer/Windows instead of default black.
143 # Note, we specify "-size {$physicalWidth}" and NOT "-size {$physicalWidth}x{$physicalHeight}".
144 # It seems that ImageMagick has a bug wherein it produces thumbnails of
145 # the wrong size in the second case.
149 wfEscapeShellArg($wgImageMagickConvertCommand) .
150 " {$quality} -background white -size {$physicalWidth} ".
151 wfEscapeShellArg($srcPath . $frame) .
153 // For the -resize option a "!" is needed to force exact size,
154 // or ImageMagick may decide your ratio is wrong and slice off
156 " -thumbnail " . wfEscapeShellArg( "{$physicalWidth}x{$physicalHeight}!" ) .
157 " -depth 8 $sharpen " .
158 wfEscapeShellArg($dstPath) . " 2>&1";
159 wfDebug( __METHOD__
.": running ImageMagick: $cmd\n");
160 wfProfileIn( 'convert' );
161 $err = wfShellExec( $cmd, $retval );
162 wfProfileOut( 'convert' );
163 } elseif( $scaler == 'custom' ) {
164 # Use a custom convert command
165 # Variables: %s %d %w %h
166 $src = wfEscapeShellArg( $srcPath );
167 $dst = wfEscapeShellArg( $dstPath );
168 $cmd = $wgCustomConvertCommand;
169 $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
170 $cmd = str_replace( '%h', $physicalHeight, str_replace( '%w', $physicalWidth, $cmd ) ); # Size
171 wfDebug( __METHOD__
.": Running custom convert command $cmd\n" );
172 wfProfileIn( 'convert' );
173 $err = wfShellExec( $cmd, $retval );
174 wfProfileOut( 'convert' );
175 } else /* $scaler == 'gd' */ {
176 # Use PHP's builtin GD library functions.
178 # First find out what kind of file this is, and select the correct
179 # input routine for this.
182 'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
183 'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__
, 'imageJpegWrapper' ) ),
184 'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
185 'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
186 'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
188 if( !isset( $typemap[$mimeType] ) ) {
189 $err = 'Image type not supported';
191 $errMsg = wfMsg ( 'thumbnail_image-type' );
192 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
194 list( $loader, $colorStyle, $saveType ) = $typemap[$mimeType];
196 if( !function_exists( $loader ) ) {
197 $err = "Incomplete GD library configuration: missing function $loader";
199 $errMsg = wfMsg ( 'thumbnail_gd-library', $loader );
200 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
203 if ( !file_exists( $srcPath ) ) {
204 $err = "File seems to be missing: $srcPath";
206 $errMsg = wfMsg ( 'thumbnail_image-missing', $srcPath );
207 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $errMsg );
210 $src_image = call_user_func( $loader, $srcPath );
211 $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
213 // Initialise the destination image to transparent instead of
214 // the default solid black, to support PNG and GIF transparency nicely
215 $background = imagecolorallocate( $dst_image, 0, 0, 0 );
216 imagecolortransparent( $dst_image, $background );
217 imagealphablending( $dst_image, false );
219 if( $colorStyle == 'palette' ) {
220 // Don't resample for paletted GIF images.
221 // It may just uglify them, and completely breaks transparency.
222 imagecopyresized( $dst_image, $src_image,
224 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
226 imagecopyresampled( $dst_image, $src_image,
228 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
231 imagesavealpha( $dst_image, true );
233 call_user_func( $saveType, $dst_image, $dstPath );
234 imagedestroy( $dst_image );
235 imagedestroy( $src_image );
239 $removed = $this->removeBadFile( $dstPath, $retval );
240 if ( $retval != 0 ||
$removed ) {
241 wfDebugLog( 'thumbnail',
242 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
243 wfHostname(), $retval, trim($err), $cmd ) );
244 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
246 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
250 static function imageJpegWrapper( $dst_image, $thumbPath ) {
251 imageinterlace( $dst_image );
252 imagejpeg( $dst_image, $thumbPath, 95 );
256 function getMetadata( $image, $filename ) {
258 if( $wgShowEXIF && file_exists( $filename ) ) {
259 $exif = new Exif( $filename );
260 $data = $exif->getFilteredData();
262 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
263 return serialize( $data );
272 function getMetadataType( $image ) {
276 function isMetadataValid( $image, $metadata ) {
278 if ( !$wgShowEXIF ) {
279 # Metadata disabled and so an empty field is expected
282 if ( $metadata === '0' ) {
283 # Special value indicating that there is no EXIF data in the file
286 $exif = @unserialize
( $metadata );
287 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
288 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif
::version() )
291 wfDebug( __METHOD__
.": wrong version\n" );
298 * Get a list of EXIF metadata items which should be displayed when
299 * the metadata table is collapsed.
301 * @return array of strings
304 function visibleMetadataFields() {
306 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
307 foreach( $lines as $line ) {
309 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
310 $fields[] = $matches[1];
313 $fields = array_map( 'strtolower', $fields );
317 function formatMetadata( $image ) {
319 'visible' => array(),
320 'collapsed' => array()
322 $metadata = $image->getMetadata();
326 $exif = unserialize( $metadata );
330 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
331 $format = new FormatExif( $exif );
333 $formatted = $format->getFormattedData();
334 // Sort fields into visible and collapsed
335 $visibleFields = $this->visibleMetadataFields();
336 foreach ( $formatted as $name => $value ) {
337 $tag = strtolower( $name );
338 self
::addMeta( $result,
339 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',