46 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
47 $useImageResize = $mainConfig->get( MainConfigNames::UseImageResize );
48 $useImageMagick = $mainConfig->get( MainConfigNames::UseImageMagick );
49 $customConvertCommand = $mainConfig->get( MainConfigNames::CustomConvertCommand );
50 if ( !$dstPath && $checkDstPath ) {
51 # No output path available, client side scaling only
53 } elseif ( !$useImageResize ) {
55 } elseif ( $useImageMagick ) {
57 } elseif ( $customConvertCommand ) {
59 } elseif ( $this->
hasGDSupport() && function_exists(
'imagecreatetruecolor' ) ) {
61 } elseif ( class_exists(
'Imagick' ) ) {
85 $res = parent::makeParamString(
$params );
87 return "interlaced-$res";
97 $remainder = preg_replace(
'/^interlaced-/',
'', $str );
98 $params = parent::parseParamString( $remainder );
102 $params[
'interlace'] = $str !== $remainder;
111 if ( $name ===
'interlace' ) {
112 return $value ===
false || $value ===
true;
114 return parent::validateParam( $name, $value );
124 $maxInterlacingAreas = MediaWikiServices::getInstance()->getMainConfig()
125 ->get( MainConfigNames::MaxInterlacingAreas );
126 if ( !parent::normaliseParams( $image,
$params ) ) {
129 $mimeType = $image->getMimeType();
130 $interlace = isset(
$params[
'interlace'] ) &&
$params[
'interlace']
131 && isset( $maxInterlacingAreas[$mimeType] )
132 && $this->
getImageArea( $image ) <= $maxInterlacingAreas[$mimeType];
133 $params[
'interlace'] = $interlace;
144 switch ( $pixelFormat ) {
146 return [
'1x1',
'1x1',
'1x1' ];
148 return [
'2x1',
'1x1',
'1x1' ];
150 return [
'2x2',
'1x1',
'1x1' ];
152 throw new UnexpectedValueException(
'Invalid pixel format for JPEG output' );
167 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
168 $sharpenReductionThreshold = $mainConfig->get( MainConfigNames::SharpenReductionThreshold );
169 $sharpenParameter = $mainConfig->get( MainConfigNames::SharpenParameter );
170 $maxAnimatedGifArea = $mainConfig->get( MainConfigNames::MaxAnimatedGifArea );
171 $imageMagickTempDir = $mainConfig->get( MainConfigNames::ImageMagickTempDir );
172 $imageMagickConvertCommand = $mainConfig->get( MainConfigNames::ImageMagickConvertCommand );
173 $jpegPixelFormat = $mainConfig->get( MainConfigNames::JpegPixelFormat );
174 $jpegQuality = $mainConfig->get( MainConfigNames::JpegQuality );
179 $animation_post = [];
183 if (
$params[
'mimeType'] ===
'image/jpeg' ) {
184 $qualityVal = isset(
$params[
'quality'] ) ? (string)
$params[
'quality'] :
null;
185 $quality = [
'-quality', $qualityVal ?: (string)$jpegQuality ];
187 $animation_post = [
'-interlace',
'JPEG' ];
189 # Sharpening, see T8193
192 < $sharpenReductionThreshold
194 $sharpen = [
'-sharpen', $sharpenParameter ];
198 $decoderHint = [
'-define',
"jpeg:size={$params['physicalDimensions']}" ];
200 if ( $jpegPixelFormat ) {
202 $subsampling = [
'-sampling-factor', implode(
',', $factors ) ];
204 } elseif (
$params[
'mimeType'] ===
'image/png' ) {
205 $quality = [
'-quality',
'95' ];
207 $animation_post = [
'-interlace',
'PNG' ];
209 } elseif (
$params[
'mimeType'] ===
'image/webp' ) {
210 $quality = [
'-quality',
'95' ];
211 } elseif (
$params[
'mimeType'] ===
'image/gif' ) {
212 if ( $this->
getImageArea( $image ) > $maxAnimatedGifArea ) {
218 $animation_pre = [
'-coalesce' ];
222 $animation_post = [
'-fuzz',
'5%',
'-layers',
'optimizeTransparency' ];
227 $animation_post[] =
'-interlace';
228 $animation_post[] =
'GIF';
230 } elseif (
$params[
'mimeType'] ===
'image/x-xcf' ) {
238 '-background',
'transparent',
240 '-background',
'white',
245 $env = [
'OMP_NUM_THREADS' => 1 ];
246 if ( (
string)$imageMagickTempDir !==
'' ) {
247 $env[
'MAGICK_TMPDIR'] = $imageMagickTempDir;
253 $cmd = Shell::escape( ...array_merge(
254 [ $imageMagickConvertCommand ],
258 [
'-background',
'white' ],
265 [
'-thumbnail',
"{$width}x{$height}!" ],
271 [
'+set',
'Thumb::URI' ],
274 [
'-rotate',
"-$rotation" ],
279 wfDebug( __METHOD__ .
": running ImageMagick: $cmd" );
283 if ( $retval !== 0 ) {
289 return false; # No error
301 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
302 $sharpenReductionThreshold = $mainConfig->get( MainConfigNames::SharpenReductionThreshold );
303 $sharpenParameter = $mainConfig->get( MainConfigNames::SharpenParameter );
304 $maxAnimatedGifArea = $mainConfig->get( MainConfigNames::MaxAnimatedGifArea );
305 $jpegPixelFormat = $mainConfig->get( MainConfigNames::JpegPixelFormat );
306 $jpegQuality = $mainConfig->get( MainConfigNames::JpegQuality );
309 $im->readImage(
$params[
'srcPath'] );
311 if (
$params[
'mimeType'] ===
'image/jpeg' ) {
315 < $sharpenReductionThreshold
318 [ $radius, $sigma ] = explode(
'x', $sharpenParameter, 2 );
319 $im->sharpenImage( (
float)$radius, (
float)$sigma );
321 $qualityVal = isset(
$params[
'quality'] ) ? (string)
$params[
'quality'] :
null;
322 $im->setCompressionQuality( $qualityVal ?: $jpegQuality );
324 $im->setInterlaceScheme( Imagick::INTERLACE_JPEG );
326 if ( $jpegPixelFormat ) {
328 $im->setSamplingFactors( $factors );
330 } elseif (
$params[
'mimeType'] ===
'image/png' ) {
331 $im->setCompressionQuality( 95 );
333 $im->setInterlaceScheme( Imagick::INTERLACE_PNG );
335 } elseif (
$params[
'mimeType'] ===
'image/gif' ) {
336 if ( $this->
getImageArea( $image ) > $maxAnimatedGifArea ) {
339 $im->setImageScene( 0 );
342 $im = $im->coalesceImages();
346 $im->setInterlaceScheme( Imagick::INTERLACE_GIF );
353 $im->setImageBackgroundColor(
new ImagickPixel(
'white' ) );
356 foreach ( $im as $i => $frame ) {
357 if ( !$frame->thumbnailImage( $width, $height,
false ) ) {
361 $im->setImageDepth( 8 );
363 if ( $rotation && !$im->rotateImage(
new ImagickPixel(
'white' ), 360 - $rotation ) ) {
368 wfDebug( __METHOD__ .
": Writing animated thumbnail" );
370 $result = $im->writeImages(
$params[
'dstPath'],
true );
372 $result = $im->writeImage(
$params[
'dstPath'] );
376 "Unable to write thumbnail to {$params['dstPath']}" );
378 }
catch ( ImagickException $e ) {
395 $customConvertCommand = MediaWikiServices::getInstance()->getMainConfig()
396 ->get( MainConfigNames::CustomConvertCommand );
400 $cmd = strtr( $customConvertCommand, [
401 '%s' => Shell::escape(
$params[
'srcPath'] ),
402 '%d' => Shell::escape(
$params[
'dstPath'] ),
403 '%w' => Shell::escape(
$params[
'physicalWidth'] ),
404 '%h' => Shell::escape(
$params[
'physicalHeight'] ),
406 wfDebug( __METHOD__ .
": Running custom convert command $cmd" );
410 if ( $retval !== 0 ) {
416 return false; # No error
428 # Use PHP's builtin GD library functions.
429 # First find out what kind of file this is, and select the correct
430 # input routine for this.
433 'image/gif' => [
'imagecreatefromgif',
'palette',
false,
'imagegif' ],
434 'image/jpeg' => [
'imagecreatefromjpeg',
'truecolor',
true,
435 [ __CLASS__,
'imageJpegWrapper' ] ],
436 'image/png' => [
'imagecreatefrompng',
'bits',
false,
'imagepng' ],
437 'image/vnd.wap.wbmp' => [
'imagecreatefromwbmp',
'palette',
false,
'imagewbmp' ],
438 'image/xbm' => [
'imagecreatefromxbm',
'palette',
false,
'imagexbm' ],
441 if ( !isset( $typemap[
$params[
'mimeType']] ) ) {
442 $err =
'Image type not supported';
444 $errMsg =
wfMessage(
'thumbnail_image-type' )->text();
448 [ $loader, $colorStyle, $useQuality, $saveType ] = $typemap[
$params[
'mimeType']];
450 if ( !function_exists( $loader ) ) {
451 $err =
"Incomplete GD library configuration: missing function $loader";
453 $errMsg =
wfMessage(
'thumbnail_gd-library', $loader )->text();
458 if ( !file_exists(
$params[
'srcPath'] ) ) {
459 $err =
"File seems to be missing: {$params['srcPath']}";
461 $errMsg =
wfMessage(
'thumbnail_image-missing',
$params[
'srcPath'] )->text();
466 if ( filesize(
$params[
'srcPath'] ) === 0 ) {
467 $err =
"Image file size seems to be zero.";
469 $errMsg =
wfMessage(
'thumbnail_image-size-zero',
$params[
'srcPath'] )->text();
474 $src_image = $loader(
$params[
'srcPath'] );
476 $rotation = function_exists(
'imagerotate' ) && !isset(
$params[
'disableRotation'] ) ?
480 $dst_image = imagecreatetruecolor( $width, $height );
484 $background = imagecolorallocate( $dst_image, 0, 0, 0 );
485 imagecolortransparent( $dst_image, $background );
486 imagealphablending( $dst_image,
false );
488 if ( $colorStyle ===
'palette' ) {
491 imagecopyresized( $dst_image, $src_image,
494 imagesx( $src_image ), imagesy( $src_image ) );
496 imagecopyresampled( $dst_image, $src_image,
499 imagesx( $src_image ), imagesy( $src_image ) );
502 if ( $rotation % 360 !== 0 && $rotation % 90 === 0 ) {
503 $rot_image = imagerotate( $dst_image, $rotation, 0 );
504 imagedestroy( $dst_image );
505 $dst_image = $rot_image;
508 imagesavealpha( $dst_image,
true );
510 $funcParams = [ $dst_image,
$params[
'dstPath'] ];
511 if ( $useQuality && isset(
$params[
'quality'] ) ) {
512 $funcParams[] =
$params[
'quality'];
515 $saveType( ...$funcParams );
517 imagedestroy( $dst_image );
518 imagedestroy( $src_image );
520 return false; # No error
533 $jpegQuality = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::JpegQuality );
535 imageinterlace( $dst_image );
536 imagejpeg( $dst_image, $thumbPath, $quality ?? $jpegQuality );
549 # ImageMagick supports autorotation
552 # Imagick::rotateImage
555 # GD's imagerotate function is used to rotate images, but not
556 # all precompiled PHP versions have that function
557 return function_exists(
'imagerotate' );
559 # Other scalers don't support rotation
570 $enableAutoRotation = MediaWikiServices::getInstance()->getMainConfig()
571 ->get( MainConfigNames::EnableAutoRotation );
573 if ( $enableAutoRotation ===
null ) {
578 return $enableAutoRotation;
590 $imageMagickConvertCommand = MediaWikiServices::getInstance()
591 ->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand );
599 $cmd = Shell::escape( $imageMagickConvertCommand ) .
" " .
601 " -rotate " . Shell::escape(
"-$rotation" ) .
" " .
603 wfDebug( __METHOD__ .
": running ImageMagick: $cmd" );
606 if ( $retval !== 0 ) {
615 $im->readImage(
$params[
'srcPath'] );
616 if ( !$im->rotateImage(
new ImagickPixel(
'white' ), 360 - $rotation ) ) {
618 "Error rotating $rotation degrees" );
620 $result = $im->writeImage(
$params[
'dstPath'] );
623 "Unable to write image to {$params['dstPath']}" );
629 "$scaler rotation not implemented" );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
Generic handler for bitmap images.
canRotate()
Returns whether the current scaler supports rotation (im and gd do)
static imageJpegWrapper( $dst_image, $thumbPath, $quality=null)
Callback for transformGd when transforming jpeg images.
hasGDSupport()
Whether the php-gd extension supports this type of file.
transformImageMagick( $image, $params)
Transform an image using ImageMagick.
getScalerType( $dstPath, $checkDstPath=true)
Returns which scaler type should be used.
imageMagickSubsampling( $pixelFormat)
Get ImageMagick subsampling factors for the target JPEG pixel format.
transformCustom( $image, $params)
Transform an image using a custom command.
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.stringto override
transformGd( $image, $params)
Transform an image using the built in GD library.
parseParamString( $str)
Parse a param string made with makeParamString back into an array.array|false Array of parameters or ...
normaliseParams( $image, &$params)
transformImageMagickExt( $image, $params)
Transform an image using the Imagick PHP extension.
validateParam( $name, $value)
Validate a thumbnail parameter at parse time.Return true to accept the parameter, and false to reject...
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
A class containing constants representing the names of configuration variables.