Fix regression in even sizing of diff columns; forgot to restore a couple bits I...
[mediawiki.git] / includes / media / Bitmap.php
blob8024cb3611b855ed024788dcf4c7e7437784d949
1 <?php
3 /**
4 * @addtogroup Media
5 */
6 class BitmapHandler extends ImageHandler {
7 function normaliseParams( $image, &$params ) {
8 global $wgMaxImageArea;
9 if ( !parent::normaliseParams( $image, $params ) ) {
10 return false;
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 )
23 return false;
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;
33 return true;
36 return true;
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->getImagePath();
55 $retval = 0;
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 );
64 if ( $wgUseImageMagick ) {
65 $scaler = 'im';
66 } elseif ( $wgCustomConvertCommand ) {
67 $scaler = 'custom';
68 } elseif ( function_exists( 'imagecreatetruecolor' ) ) {
69 $scaler = 'gd';
70 } else {
71 $scaler = 'client';
74 if ( $scaler == 'client' ) {
75 # Client-side image scaling, use the source URL
76 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
77 return new ThumbnailImage( $image->getURL(), $clientWidth, $clientHeight, $srcPath );
80 if ( $flags & self::TRANSFORM_LATER ) {
81 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
84 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
85 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
86 wfMsg( 'thumbnail_dest_directory' ) );
89 if ( $scaler == 'im' ) {
90 # use ImageMagick
92 $sharpen = '';
93 if ( $mimeType == 'image/jpeg' ) {
94 $quality = "-quality 80"; // 80%
95 # Sharpening, see bug 6193
96 if ( ( $physicalWidth + $physicalHeight ) / ( $srcWidth + $srcHeight ) < $wgSharpenReductionThreshold ) {
97 $sharpen = "-sharpen " . wfEscapeShellArg( $wgSharpenParameter );
99 } elseif ( $mimeType == 'image/png' ) {
100 $quality = "-quality 95"; // zlib 9, adaptive filtering
101 } else {
102 $quality = ''; // default
105 # Specify white background color, will be used for transparent images
106 # in Internet Explorer/Windows instead of default black.
108 # Note, we specify "-size {$physicalWidth}" and NOT "-size {$physicalWidth}x{$physicalHeight}".
109 # It seems that ImageMagick has a bug wherein it produces thumbnails of
110 # the wrong size in the second case.
112 $cmd = wfEscapeShellArg($wgImageMagickConvertCommand) .
113 " {$quality} -background white -size {$physicalWidth} ".
114 wfEscapeShellArg($srcPath) .
115 // Coalesce is needed to scale animated GIFs properly (bug 1017).
116 ' -coalesce ' .
117 // For the -resize option a "!" is needed to force exact size,
118 // or ImageMagick may decide your ratio is wrong and slice off
119 // a pixel.
120 " -thumbnail " . wfEscapeShellArg( "{$physicalWidth}x{$physicalHeight}!" ) .
121 " -depth 8 $sharpen " .
122 wfEscapeShellArg($dstPath) . " 2>&1";
123 wfDebug( __METHOD__.": running ImageMagick: $cmd\n");
124 wfProfileIn( 'convert' );
125 $err = wfShellExec( $cmd, $retval );
126 wfProfileOut( 'convert' );
127 } elseif( $scaler == 'custom' ) {
128 # Use a custom convert command
129 # Variables: %s %d %w %h
130 $src = wfEscapeShellArg( $srcPath );
131 $dst = wfEscapeShellArg( $dstPath );
132 $cmd = $wgCustomConvertCommand;
133 $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
134 $cmd = str_replace( '%h', $physicalHeight, str_replace( '%w', $physicalWidth, $cmd ) ); # Size
135 wfDebug( __METHOD__.": Running custom convert command $cmd\n" );
136 wfProfileIn( 'convert' );
137 $err = wfShellExec( $cmd, $retval );
138 wfProfileOut( 'convert' );
139 } else /* $scaler == 'gd' */ {
140 # Use PHP's builtin GD library functions.
142 # First find out what kind of file this is, and select the correct
143 # input routine for this.
145 $typemap = array(
146 'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
147 'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imageJpegWrapper' ) ),
148 'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
149 'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
150 'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
152 if( !isset( $typemap[$mimeType] ) ) {
153 $err = 'Image type not supported';
154 wfDebug( "$err\n" );
155 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
157 list( $loader, /* $colorStyle */, $saveType ) = $typemap[$mimeType];
159 if( !function_exists( $loader ) ) {
160 $err = "Incomplete GD library configuration: missing function $loader";
161 wfDebug( "$err\n" );
162 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
165 $src_image = call_user_func( $loader, $srcPath );
166 $dst_image = imagecreatetruecolor( $physicalWidth, $physicalHeight );
167 imagecopyresampled( $dst_image, $src_image,
168 0,0,0,0,
169 $physicalWidth, $physicalHeight, imagesx( $src_image ), imagesy( $src_image ) );
170 call_user_func( $saveType, $dst_image, $dstPath );
171 imagedestroy( $dst_image );
172 imagedestroy( $src_image );
173 $retval = 0;
176 $removed = $this->removeBadFile( $dstPath, $retval );
177 if ( $retval != 0 || $removed ) {
178 wfDebugLog( 'thumbnail',
179 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
180 wfHostname(), $retval, trim($err), $cmd ) );
181 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
182 } else {
183 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
187 static function imageJpegWrapper( $dst_image, $thumbPath ) {
188 imageinterlace( $dst_image );
189 imagejpeg( $dst_image, $thumbPath, 95 );
193 function getMetadata( $image, $filename ) {
194 global $wgShowEXIF;
195 if( $wgShowEXIF && file_exists( $filename ) ) {
196 $exif = new Exif( $filename );
197 $data = $exif->getFilteredData();
198 if ( $data ) {
199 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
200 return serialize( $data );
201 } else {
202 return '0';
204 } else {
205 return '';
209 function getMetadataType( $image ) {
210 return 'exif';
213 function isMetadataValid( $image, $metadata ) {
214 global $wgShowEXIF;
215 if ( !$wgShowEXIF ) {
216 # Metadata disabled and so an empty field is expected
217 return true;
219 if ( $metadata === '0' ) {
220 # Special value indicating that there is no EXIF data in the file
221 return true;
223 $exif = @unserialize( $metadata );
224 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
225 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
227 # Wrong version
228 wfDebug( __METHOD__.": wrong version\n" );
229 return false;
231 return true;