3 * Generic handler for bitmap images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Generic handler for bitmap images
29 class BitmapHandler
extends TransformationalImageHandler
{
32 * Returns which scaler type should be used. Creates parent directories
33 * for $dstPath and returns 'client' on error
35 * @param string $dstPath
36 * @param bool $checkDstPath
37 * @return string|Callable One of client, im, custom, gd, imext or an array( object, method )
39 protected function getScalerType( $dstPath, $checkDstPath = true ) {
40 global $wgUseImageResize, $wgUseImageMagick, $wgCustomConvertCommand;
42 if ( !$dstPath && $checkDstPath ) {
43 # No output path available, client side scaling only
45 } elseif ( !$wgUseImageResize ) {
47 } elseif ( $wgUseImageMagick ) {
49 } elseif ( $wgCustomConvertCommand ) {
51 } elseif ( function_exists( 'imagecreatetruecolor' ) ) {
53 } elseif ( class_exists( 'Imagick' ) ) {
62 public function makeParamString( $params ) {
63 $res = parent
::makeParamString( $params );
64 if ( isset( $params['interlace'] ) && $params['interlace'] ) {
65 return "interlaced-{$res}";
71 public function parseParamString( $str ) {
72 $remainder = preg_replace( '/^interlaced-/', '', $str );
73 $params = parent
::parseParamString( $remainder );
74 if ( $params === false ) {
77 $params['interlace'] = $str !== $remainder;
81 public function validateParam( $name, $value ) {
82 if ( $name === 'interlace' ) {
83 return $value === false ||
$value === true;
85 return parent
::validateParam( $name, $value );
91 * @param array $params
94 function normaliseParams( $image, &$params ) {
95 global $wgMaxInterlacingAreas;
96 if ( !parent
::normaliseParams( $image, $params ) ) {
99 $mimeType = $image->getMimeType();
100 $interlace = isset( $params['interlace'] ) && $params['interlace']
101 && isset( $wgMaxInterlacingAreas[$mimeType] )
102 && $this->getImageArea( $image ) <= $wgMaxInterlacingAreas[$mimeType];
103 $params['interlace'] = $interlace;
108 * Get ImageMagick subsampling factors for the target JPEG pixel format.
110 * @param string $pixelFormat one of 'yuv444', 'yuv422', 'yuv420'
111 * @return array of string keys
113 protected function imageMagickSubsampling( $pixelFormat ) {
114 switch ( $pixelFormat ) {
116 return [ '1x1', '1x1', '1x1' ];
118 return [ '2x1', '1x1', '1x1' ];
120 return [ '2x2', '1x1', '1x1' ];
122 throw new MWException( 'Invalid pixel format for JPEG output' );
127 * Transform an image using ImageMagick
129 * @param File $image File associated with this thumbnail
130 * @param array $params Array with scaler params
132 * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
134 protected function transformImageMagick( $image, $params ) {
136 global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea,
137 $wgImageMagickTempDir, $wgImageMagickConvertCommand, $wgJpegPixelFormat;
143 $animation_post = [];
147 if ( $params['mimeType'] == 'image/jpeg' ) {
148 $qualityVal = isset( $params['quality'] ) ?
(string)$params['quality'] : null;
149 $quality = [ '-quality', $qualityVal ?
: '80' ]; // 80%
150 if ( $params['interlace'] ) {
151 $animation_post = [ '-interlace', 'JPEG' ];
153 # Sharpening, see bug 6193
154 if ( ( $params['physicalWidth'] +
$params['physicalHeight'] )
155 / ( $params['srcWidth'] +
$params['srcHeight'] )
156 < $wgSharpenReductionThreshold
158 $sharpen = [ '-sharpen', $wgSharpenParameter ];
160 if ( version_compare( $this->getMagickVersion(), "6.5.6" ) >= 0 ) {
161 // JPEG decoder hint to reduce memory, available since IM 6.5.6-2
162 $decoderHint = [ '-define', "jpeg:size={$params['physicalDimensions']}" ];
164 if ( $wgJpegPixelFormat ) {
165 $factors = $this->imageMagickSubsampling( $wgJpegPixelFormat );
166 $subsampling = [ '-sampling-factor', implode( ',', $factors ) ];
168 } elseif ( $params['mimeType'] == 'image/png' ) {
169 $quality = [ '-quality', '95' ]; // zlib 9, adaptive filtering
170 if ( $params['interlace'] ) {
171 $animation_post = [ '-interlace', 'PNG' ];
173 } elseif ( $params['mimeType'] == 'image/webp' ) {
174 $quality = [ '-quality', '95' ]; // zlib 9, adaptive filtering
175 } elseif ( $params['mimeType'] == 'image/gif' ) {
176 if ( $this->getImageArea( $image ) > $wgMaxAnimatedGifArea ) {
177 // Extract initial frame only; we're so big it'll
178 // be a total drag. :P
180 } elseif ( $this->isAnimatedImage( $image ) ) {
181 // Coalesce is needed to scale animated GIFs properly (bug 1017).
182 $animation_pre = [ '-coalesce' ];
183 // We optimize the output, but -optimize is broken,
184 // use optimizeTransparency instead (bug 11822)
185 if ( version_compare( $this->getMagickVersion(), "6.3.5" ) >= 0 ) {
186 $animation_post = [ '-fuzz', '5%', '-layers', 'optimizeTransparency' ];
189 if ( $params['interlace'] && version_compare( $this->getMagickVersion(), "6.3.4" ) >= 0
190 && !$this->isAnimatedImage( $image ) ) { // interlacing animated GIFs is a bad idea
191 $animation_post[] = '-interlace';
192 $animation_post[] = 'GIF';
194 } elseif ( $params['mimeType'] == 'image/x-xcf' ) {
195 // Before merging layers, we need to set the background
196 // to be transparent to preserve alpha, as -layers merge
197 // merges all layers on to a canvas filled with the
198 // background colour. After merging we reset the background
199 // to be white for the default background colour setting
200 // in the PNG image (which is used in old IE)
202 '-background', 'transparent',
204 '-background', 'white',
206 MediaWiki\
suppressWarnings();
207 $xcfMeta = unserialize( $image->getMetadata() );
208 MediaWiki\restoreWarnings
();
210 && isset( $xcfMeta['colorType'] )
211 && $xcfMeta['colorType'] === 'greyscale-alpha'
212 && version_compare( $this->getMagickVersion(), "6.8.9-3" ) < 0
214 // bug 66323 - Greyscale images not rendered properly.
215 // So only take the "red" channel.
216 $channelOnly = [ '-channel', 'R', '-separate' ];
217 $animation_pre = array_merge( $animation_pre, $channelOnly );
221 // Use one thread only, to avoid deadlock bugs on OOM
222 $env = [ 'OMP_NUM_THREADS' => 1 ];
223 if ( strval( $wgImageMagickTempDir ) !== '' ) {
224 $env['MAGICK_TMPDIR'] = $wgImageMagickTempDir;
227 $rotation = isset( $params['disableRotation'] ) ?
0 : $this->getRotation( $image );
228 list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
230 $cmd = call_user_func_array( 'wfEscapeShellArg', array_merge(
231 [ $wgImageMagickConvertCommand ],
233 // Specify white background color, will be used for transparent images
234 // in Internet Explorer/Windows instead of default black.
235 [ '-background', 'white' ],
237 [ $this->escapeMagickInput( $params['srcPath'], $scene ) ],
239 // For the -thumbnail option a "!" is needed to force exact size,
240 // or ImageMagick may decide your ratio is wrong and slice off
242 [ '-thumbnail', "{$width}x{$height}!" ],
243 // Add the source url as a comment to the thumb, but don't add the flag if there's no comment
244 ( $params['comment'] !== ''
245 ?
[ '-set', 'comment', $this->escapeMagickProperty( $params['comment'] ) ]
247 // T108616: Avoid exposure of local file path
248 [ '+set', 'Thumb::URI' ],
251 [ '-rotate', "-$rotation" ],
254 [ $this->escapeMagickOutput( $params['dstPath'] ) ] ) );
256 wfDebug( __METHOD__
. ": running ImageMagick: $cmd\n" );
258 $err = wfShellExecWithStderr( $cmd, $retval, $env );
260 if ( $retval !== 0 ) {
261 $this->logErrorForExternalProcess( $retval, $err, $cmd );
263 return $this->getMediaTransformError( $params, "$err\nError code: $retval" );
266 return false; # No error
270 * Transform an image using the Imagick PHP extension
272 * @param File $image File associated with this thumbnail
273 * @param array $params Array with scaler params
275 * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
277 protected function transformImageMagickExt( $image, $params ) {
278 global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea,
283 $im->readImage( $params['srcPath'] );
285 if ( $params['mimeType'] == 'image/jpeg' ) {
286 // Sharpening, see bug 6193
287 if ( ( $params['physicalWidth'] +
$params['physicalHeight'] )
288 / ( $params['srcWidth'] +
$params['srcHeight'] )
289 < $wgSharpenReductionThreshold
291 // Hack, since $wgSharpenParameter is written specifically for the command line convert
292 list( $radius, $sigma ) = explode( 'x', $wgSharpenParameter );
293 $im->sharpenImage( $radius, $sigma );
295 $qualityVal = isset( $params['quality'] ) ?
(string)$params['quality'] : null;
296 $im->setCompressionQuality( $qualityVal ?
: 80 );
297 if ( $params['interlace'] ) {
298 $im->setInterlaceScheme( Imagick
::INTERLACE_JPEG
);
300 if ( $wgJpegPixelFormat ) {
301 $factors = $this->imageMagickSubsampling( $wgJpegPixelFormat );
302 $im->setSamplingFactors( $factors );
304 } elseif ( $params['mimeType'] == 'image/png' ) {
305 $im->setCompressionQuality( 95 );
306 if ( $params['interlace'] ) {
307 $im->setInterlaceScheme( Imagick
::INTERLACE_PNG
);
309 } elseif ( $params['mimeType'] == 'image/gif' ) {
310 if ( $this->getImageArea( $image ) > $wgMaxAnimatedGifArea ) {
311 // Extract initial frame only; we're so big it'll
312 // be a total drag. :P
313 $im->setImageScene( 0 );
314 } elseif ( $this->isAnimatedImage( $image ) ) {
315 // Coalesce is needed to scale animated GIFs properly (bug 1017).
316 $im = $im->coalesceImages();
318 // GIF interlacing is only available since 6.3.4
319 $v = Imagick
::getVersion();
320 preg_match( '/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $v['versionString'], $v );
322 if ( $params['interlace'] && version_compare( $v[1], '6.3.4' ) >= 0 ) {
323 $im->setInterlaceScheme( Imagick
::INTERLACE_GIF
);
327 $rotation = isset( $params['disableRotation'] ) ?
0 : $this->getRotation( $image );
328 list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
330 $im->setImageBackgroundColor( new ImagickPixel( 'white' ) );
332 // Call Imagick::thumbnailImage on each frame
333 foreach ( $im as $i => $frame ) {
334 if ( !$frame->thumbnailImage( $width, $height, /* fit */ false ) ) {
335 return $this->getMediaTransformError( $params, "Error scaling frame $i" );
338 $im->setImageDepth( 8 );
341 if ( !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) {
342 return $this->getMediaTransformError( $params, "Error rotating $rotation degrees" );
346 if ( $this->isAnimatedImage( $image ) ) {
347 wfDebug( __METHOD__
. ": Writing animated thumbnail\n" );
348 // This is broken somehow... can't find out how to fix it
349 $result = $im->writeImages( $params['dstPath'], true );
351 $result = $im->writeImage( $params['dstPath'] );
354 return $this->getMediaTransformError( $params,
355 "Unable to write thumbnail to {$params['dstPath']}" );
357 } catch ( ImagickException
$e ) {
358 return $this->getMediaTransformError( $params, $e->getMessage() );
365 * Transform an image using a custom command
367 * @param File $image File associated with this thumbnail
368 * @param array $params Array with scaler params
370 * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
372 protected function transformCustom( $image, $params ) {
373 # Use a custom convert command
374 global $wgCustomConvertCommand;
376 # Variables: %s %d %w %h
377 $src = wfEscapeShellArg( $params['srcPath'] );
378 $dst = wfEscapeShellArg( $params['dstPath'] );
379 $cmd = $wgCustomConvertCommand;
380 $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
381 $cmd = str_replace( '%h', wfEscapeShellArg( $params['physicalHeight'] ),
382 str_replace( '%w', wfEscapeShellArg( $params['physicalWidth'] ), $cmd ) ); # Size
383 wfDebug( __METHOD__
. ": Running custom convert command $cmd\n" );
385 $err = wfShellExecWithStderr( $cmd, $retval );
387 if ( $retval !== 0 ) {
388 $this->logErrorForExternalProcess( $retval, $err, $cmd );
390 return $this->getMediaTransformError( $params, $err );
393 return false; # No error
397 * Transform an image using the built in GD library
399 * @param File $image File associated with this thumbnail
400 * @param array $params Array with scaler params
402 * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
404 protected function transformGd( $image, $params ) {
405 # Use PHP's builtin GD library functions.
406 # First find out what kind of file this is, and select the correct
407 # input routine for this.
410 'image/gif' => [ 'imagecreatefromgif', 'palette', false, 'imagegif' ],
411 'image/jpeg' => [ 'imagecreatefromjpeg', 'truecolor', true,
412 [ __CLASS__
, 'imageJpegWrapper' ] ],
413 'image/png' => [ 'imagecreatefrompng', 'bits', false, 'imagepng' ],
414 'image/vnd.wap.wbmp' => [ 'imagecreatefromwbmp', 'palette', false, 'imagewbmp' ],
415 'image/xbm' => [ 'imagecreatefromxbm', 'palette', false, 'imagexbm' ],
418 if ( !isset( $typemap[$params['mimeType']] ) ) {
419 $err = 'Image type not supported';
421 $errMsg = wfMessage( 'thumbnail_image-type' )->text();
423 return $this->getMediaTransformError( $params, $errMsg );
425 list( $loader, $colorStyle, $useQuality, $saveType ) = $typemap[$params['mimeType']];
427 if ( !function_exists( $loader ) ) {
428 $err = "Incomplete GD library configuration: missing function $loader";
430 $errMsg = wfMessage( 'thumbnail_gd-library', $loader )->text();
432 return $this->getMediaTransformError( $params, $errMsg );
435 if ( !file_exists( $params['srcPath'] ) ) {
436 $err = "File seems to be missing: {$params['srcPath']}";
438 $errMsg = wfMessage( 'thumbnail_image-missing', $params['srcPath'] )->text();
440 return $this->getMediaTransformError( $params, $errMsg );
443 $src_image = call_user_func( $loader, $params['srcPath'] );
445 $rotation = function_exists( 'imagerotate' ) && !isset( $params['disableRotation'] ) ?
446 $this->getRotation( $image ) :
448 list( $width, $height ) = $this->extractPreRotationDimensions( $params, $rotation );
449 $dst_image = imagecreatetruecolor( $width, $height );
451 // Initialise the destination image to transparent instead of
452 // the default solid black, to support PNG and GIF transparency nicely
453 $background = imagecolorallocate( $dst_image, 0, 0, 0 );
454 imagecolortransparent( $dst_image, $background );
455 imagealphablending( $dst_image, false );
457 if ( $colorStyle == 'palette' ) {
458 // Don't resample for paletted GIF images.
459 // It may just uglify them, and completely breaks transparency.
460 imagecopyresized( $dst_image, $src_image,
463 imagesx( $src_image ), imagesy( $src_image ) );
465 imagecopyresampled( $dst_image, $src_image,
468 imagesx( $src_image ), imagesy( $src_image ) );
471 if ( $rotation %
360 != 0 && $rotation %
90 == 0 ) {
472 $rot_image = imagerotate( $dst_image, $rotation, 0 );
473 imagedestroy( $dst_image );
474 $dst_image = $rot_image;
477 imagesavealpha( $dst_image, true );
479 $funcParams = [ $dst_image, $params['dstPath'] ];
480 if ( $useQuality && isset( $params['quality'] ) ) {
481 $funcParams[] = $params['quality'];
483 call_user_func_array( $saveType, $funcParams );
485 imagedestroy( $dst_image );
486 imagedestroy( $src_image );
488 return false; # No error
492 * Callback for transformGd when transforming jpeg images.
494 // FIXME: transformImageMagick() & transformImageMagickExt() uses JPEG quality 80, here it's 95?
495 static function imageJpegWrapper( $dst_image, $thumbPath, $quality = 95 ) {
496 imageinterlace( $dst_image );
497 imagejpeg( $dst_image, $thumbPath, $quality );
501 * Returns whether the current scaler supports rotation (im and gd do)
505 public function canRotate() {
506 $scaler = $this->getScalerType( null, false );
509 # ImageMagick supports autorotation
512 # Imagick::rotateImage
515 # GD's imagerotate function is used to rotate images, but not
516 # all precompiled PHP versions have that function
517 return function_exists( 'imagerotate' );
519 # Other scalers don't support rotation
525 * @see $wgEnableAutoRotation
526 * @return bool Whether auto rotation is enabled
528 public function autoRotateEnabled() {
529 global $wgEnableAutoRotation;
531 if ( $wgEnableAutoRotation === null ) {
532 // Only enable auto-rotation when we actually can
533 return $this->canRotate();
536 return $wgEnableAutoRotation;
541 * @param array $params Rotate parameters.
542 * 'rotation' clockwise rotation in degrees, allowed are multiples of 90
544 * @return bool|MediaTransformError
546 public function rotate( $file, $params ) {
547 global $wgImageMagickConvertCommand;
549 $rotation = ( $params['rotation'] +
$this->getRotation( $file ) ) %
360;
552 $scaler = $this->getScalerType( null, false );
555 $cmd = wfEscapeShellArg( $wgImageMagickConvertCommand ) . " " .
556 wfEscapeShellArg( $this->escapeMagickInput( $params['srcPath'], $scene ) ) .
557 " -rotate " . wfEscapeShellArg( "-$rotation" ) . " " .
558 wfEscapeShellArg( $this->escapeMagickOutput( $params['dstPath'] ) );
559 wfDebug( __METHOD__
. ": running ImageMagick: $cmd\n" );
561 $err = wfShellExecWithStderr( $cmd, $retval );
562 if ( $retval !== 0 ) {
563 $this->logErrorForExternalProcess( $retval, $err, $cmd );
565 return new MediaTransformError( 'thumbnail_error', 0, 0, $err );
571 $im->readImage( $params['srcPath'] );
572 if ( !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) {
573 return new MediaTransformError( 'thumbnail_error', 0, 0,
574 "Error rotating $rotation degrees" );
576 $result = $im->writeImage( $params['dstPath'] );
578 return new MediaTransformError( 'thumbnail_error', 0, 0,
579 "Unable to write image to {$params['dstPath']}" );
584 return new MediaTransformError( 'thumbnail_error', 0, 0,
585 "$scaler rotation not implemented" );