6 class BitmapHandler
extends ImageHandler
{
7 function normaliseParams( $image, &$params ) {
8 global $wgMaxImageArea;
9 if ( !parent
::normaliseParams( $image, $params ) ) {
13 $mimeType = $image->getMimeType();
14 $srcWidth = $image->getWidth( $params['page'] );
15 $srcHeight = $image->getHeight( $params['page'] );
17 # Don't thumbnail an image so big that it will fill hard drives and send servers into swap
18 # JPEG has the handy property of allowing thumbnailing without full decompression, so we make
19 # an exception for it.
20 if ( $mimeType !== 'image/jpeg' &&
21 $srcWidth * $srcHeight > $wgMaxImageArea )
26 # Don't make an image bigger than the source
27 $params['physicalWidth'] = $params['width'];
28 $params['physicalHeight'] = $params['height'];
30 if ( $params['physicalWidth'] >= $srcWidth ) {
31 $params['physicalWidth'] = $srcWidth;
32 $params['physicalHeight'] = $srcHeight;
39 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
40 global $wgUseImageMagick, $wgImageMagickConvertCommand;
41 global $wgCustomConvertCommand;
42 global $wgSharpenParameter, $wgSharpenReductionThreshold;
44 if ( !$this->normaliseParams( $image, $params ) ) {
45 return new TransformParameterError( $params );
47 $physicalWidth = $params['physicalWidth'];
48 $physicalHeight = $params['physicalHeight'];
49 $clientWidth = $params['width'];
50 $clientHeight = $params['height'];
51 $srcWidth = $image->getWidth();
52 $srcHeight = $image->getHeight();
53 $mimeType = $image->getMimeType();
54 $srcPath = $image->getPath();
56 wfDebug( __METHOD__
.": creating {$physicalWidth}x{$physicalHeight} thumbnail at $dstPath\n" );
58 if ( $physicalWidth == $srcWidth && $physicalHeight == $srcHeight ) {
59 # normaliseParams (or the user) wants us to return the unscaled image
60 wfDebug( __METHOD__
.": returning unscaled image\n" );
61 return new ThumbnailImage( $image->getURL(), $clientWidth, $clientHeight, $srcPath );
65 // No output path available, client side scaling only
67 } elseif ( $wgUseImageMagick ) {
69 } elseif ( $wgCustomConvertCommand ) {
71 } elseif ( function_exists( 'imagecreatetruecolor' ) ) {
77 if ( $scaler == 'client' ) {
78 # Client-side image scaling, use the source URL
79 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
80 return new ThumbnailImage( $image->getURL(), $clientWidth, $clientHeight, $srcPath );
83 if ( $flags & self
::TRANSFORM_LATER
) {
84 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
87 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
88 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
89 wfMsg( 'thumbnail_dest_directory' ) );
92 if ( $scaler == 'im' ) {
96 if ( $mimeType == 'image/jpeg' ) {
97 $quality = "-quality 80"; // 80%
98 # Sharpening, see bug 6193
99 if ( ( $physicalWidth +
$physicalHeight ) / ( $srcWidth +
$srcHeight ) < $wgSharpenReductionThreshold ) {
100 $sharpen = "-sharpen " . wfEscapeShellArg( $wgSharpenParameter );
102 } elseif ( $mimeType == 'image/png' ) {
103 $quality = "-quality 95"; // zlib 9, adaptive filtering
105 $quality = ''; // default
108 # Specify white background color, will be used for transparent images
109 # in Internet Explorer/Windows instead of default black.
111 # Note, we specify "-size {$physicalWidth}" and NOT "-size {$physicalWidth}x{$physicalHeight}".
112 # It seems that ImageMagick has a bug wherein it produces thumbnails of
113 # the wrong size in the second case.
115 $cmd = wfEscapeShellArg($wgImageMagickConvertCommand) .
116 " {$quality} -background white -size {$physicalWidth} ".
117 wfEscapeShellArg($srcPath) .
118 // Coalesce is needed to scale animated GIFs properly (bug 1017).
120 // For the -resize option a "!" is needed to force exact size,
121 // or ImageMagick may decide your ratio is wrong and slice off
123 " -thumbnail " . wfEscapeShellArg( "{$physicalWidth}x{$physicalHeight}!" ) .
124 " -depth 8 $sharpen " .
125 wfEscapeShellArg($dstPath) . " 2>&1";
126 wfDebug( __METHOD__
.": running ImageMagick: $cmd\n");
127 wfProfileIn( 'convert' );
128 $err = wfShellExec( $cmd, $retval );
129 wfProfileOut( 'convert' );
130 } elseif( $scaler == 'custom' ) {
131 # Use a custom convert command
132 # Variables: %s %d %w %h
133 $src = wfEscapeShellArg( $srcPath );
134 $dst = wfEscapeShellArg( $dstPath );
135 $cmd = $wgCustomConvertCommand;
136 $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
137 $cmd = str_replace( '%h', $physicalHeight, str_replace( '%w', $physicalWidth, $cmd ) ); # Size
138 wfDebug( __METHOD__
.": Running custom convert command $cmd\n" );
139 wfProfileIn( 'convert' );
140 $err = wfShellExec( $cmd, $retval );
141 wfProfileOut( 'convert' );
142 } else /* $scaler == 'gd' */ {
143 # Use PHP's builtin GD library functions.
145 # First find out what kind of file this is, and select the correct
146 # input routine for this.
149 'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
150 'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__
, 'imageJpegWrapper' ) ),
151 'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
152 'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
153 'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
155 if( !isset( $typemap[$mimeType] ) ) {
156 $err = 'Image type not supported';
158 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
160 list( $loader, /* $colorStyle */, $saveType ) = $typemap[$mimeType];
162 if( !function_exists( $loader ) ) {
163 $err = "Incomplete GD library configuration: missing function $loader";
165 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
168 $src_image = call_user_func( $loader, $srcPath );
169 $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
170 imagecopyresampled( $dst_image, $src_image,
172 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
173 call_user_func( $saveType, $dst_image, $dstPath );
174 imagedestroy( $dst_image );
175 imagedestroy( $src_image );
179 $removed = $this->removeBadFile( $dstPath, $retval );
180 if ( $retval != 0 ||
$removed ) {
181 wfDebugLog( 'thumbnail',
182 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
183 wfHostname(), $retval, trim($err), $cmd ) );
184 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
186 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
190 static function imageJpegWrapper( $dst_image, $thumbPath ) {
191 imageinterlace( $dst_image );
192 imagejpeg( $dst_image, $thumbPath, 95 );
196 function getMetadata( $image, $filename ) {
198 if( $wgShowEXIF && file_exists( $filename ) ) {
199 $exif = new Exif( $filename );
200 $data = $exif->getFilteredData();
202 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
203 return serialize( $data );
212 function getMetadataType( $image ) {
216 function isMetadataValid( $image, $metadata ) {
218 if ( !$wgShowEXIF ) {
219 # Metadata disabled and so an empty field is expected
222 if ( $metadata === '0' ) {
223 # Special value indicating that there is no EXIF data in the file
226 $exif = @unserialize
( $metadata );
227 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
228 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif
::version() )
231 wfDebug( __METHOD__
.": wrong version\n" );
238 * Get a list of EXIF metadata items which should be displayed when
239 * the metadata table is collapsed.
241 * @return array of strings
244 function visibleMetadataFields() {
246 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
247 foreach( $lines as $line ) {
249 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
250 $fields[] = $matches[1];
253 $fields = array_map( 'strtolower', $fields );
257 function formatMetadata( $image ) {
259 'visible' => array(),
260 'collapsed' => array()
262 $metadata = $image->getMetadata();
266 $exif = unserialize( $metadata );
270 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
271 $format = new FormatExif( $exif );
273 $formatted = $format->getFormattedData();
274 // Sort fields into visible and collapsed
275 $visibleFields = $this->visibleMetadataFields();
276 foreach ( $formatted as $name => $value ) {
277 $tag = strtolower( $name );
278 self
::addMeta( $result,
279 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',