| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\Shell\Shell; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class BitmapHandler extends TransformationalImageHandler { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected function getScalerType( $dstPath, $checkDstPath = true ) { |
| 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 |
|
| 52 |
$scaler = 'client'; |
| 53 |
} elseif ( !$useImageResize ) { |
| 54 |
$scaler = 'client'; |
| 55 |
} elseif ( $useImageMagick ) { |
| 56 |
$scaler = 'im'; |
| 57 |
} elseif ( $customConvertCommand ) { |
| 58 |
$scaler = 'custom'; |
| 59 |
} elseif ( $this->hasGDSupport() && function_exists( 'imagecreatetruecolor' ) ) { |
| 60 |
$scaler = 'gd'; |
| 61 |
} elseif ( class_exists( 'Imagick' ) ) { |
| 62 |
$scaler = 'imext'; |
| 63 |
} else { |
| 64 |
$scaler = 'client'; |
| 65 |
} |
| 66 |
|
| 67 |
return $scaler; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected function hasGDSupport() { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function makeParamString( $params ) { |
| 85 |
$res = parent::makeParamString( $params ); |
| 86 |
if ( isset( $params['interlace'] ) && $params['interlace'] ) { |
| 87 |
return "interlaced-$res"; |
| 88 |
} |
| 89 |
return $res; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function parseParamString( $str ) { |
| 97 |
$remainder = preg_replace( '/^interlaced-/', '', $str ); |
| 98 |
$params = parent::parseParamString( $remainder ); |
| 99 |
if ( $params === false ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
$params['interlace'] = $str !== $remainder; |
| 103 |
return $params; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function validateParam( $name, $value ) { |
| 111 |
if ( $name === 'interlace' ) { |
| 112 |
return $value === false || $value === true; |
| 113 |
} |
| 114 |
return parent::validateParam( $name, $value ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function normaliseParams( $image, &$params ) { |
| 124 |
$maxInterlacingAreas = MediaWikiServices::getInstance()->getMainConfig() |
| 125 |
->get( MainConfigNames::MaxInterlacingAreas ); |
| 126 |
if ( !parent::normaliseParams( $image, $params ) ) { |
| 127 |
return false; |
| 128 |
} |
| 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; |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
protected function imageMagickSubsampling( $pixelFormat ) { |
| 144 |
switch ( $pixelFormat ) { |
| 145 |
case 'yuv444': |
| 146 |
return [ '1x1', '1x1', '1x1' ]; |
| 147 |
case 'yuv422': |
| 148 |
return [ '2x1', '1x1', '1x1' ]; |
| 149 |
case 'yuv420': |
| 150 |
return [ '2x2', '1x1', '1x1' ]; |
| 151 |
default: |
| 152 |
throw new UnexpectedValueException( 'Invalid pixel format for JPEG output' ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected function transformImageMagick( $image, $params ) { |
| 166 |
|
| 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 ); |
| 175 |
$quality = []; |
| 176 |
$sharpen = []; |
| 177 |
$scene = false; |
| 178 |
$animation_pre = []; |
| 179 |
$animation_post = []; |
| 180 |
$decoderHint = []; |
| 181 |
$subsampling = []; |
| 182 |
|
| 183 |
if ( $params['mimeType'] === 'image/jpeg' ) { |
| 184 |
$qualityVal = isset( $params['quality'] ) ? (string)$params['quality'] : null; |
| 185 |
$quality = [ '-quality', $qualityVal ?: (string)$jpegQuality ]; |
| 186 |
if ( $params['interlace'] ) { |
| 187 |
$animation_post = [ '-interlace', 'JPEG' ]; |
| 188 |
} |
| 189 |
|
| 190 |
if ( ( $params['physicalWidth'] + $params['physicalHeight'] ) |
| 191 |
/ ( $params['srcWidth'] + $params['srcHeight'] ) |
| 192 |
< $sharpenReductionThreshold |
| 193 |
) { |
| 194 |
$sharpen = [ '-sharpen', $sharpenParameter ]; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
$decoderHint = [ '-define', "jpeg:size={$params['physicalDimensions']}" ]; |
| 199 |
|
| 200 |
if ( $jpegPixelFormat ) { |
| 201 |
$factors = $this->imageMagickSubsampling( $jpegPixelFormat ); |
| 202 |
$subsampling = [ '-sampling-factor', implode( ',', $factors ) ]; |
| 203 |
} |
| 204 |
} elseif ( $params['mimeType'] === 'image/png' ) { |
| 205 |
$quality = [ '-quality', '95' ]; |
| 206 |
if ( $params['interlace'] ) { |
| 207 |
$animation_post = [ '-interlace', 'PNG' ]; |
| 208 |
} |
| 209 |
} elseif ( $params['mimeType'] === 'image/webp' ) { |
| 210 |
$quality = [ '-quality', '95' ]; |
| 211 |
} elseif ( $params['mimeType'] === 'image/gif' ) { |
| 212 |
if ( $this->getImageArea( $image ) > $maxAnimatedGifArea ) { |
| 213 |
|
| 214 |
|
| 215 |
$scene = 0; |
| 216 |
} elseif ( $this->isAnimatedImage( $image ) ) { |
| 217 |
|
| 218 |
$animation_pre = [ '-coalesce' ]; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
$animation_post = [ '-fuzz', '5%', '-layers', 'optimizeTransparency' ]; |
| 223 |
} |
| 224 |
if ( $params['interlace'] && !$this->isAnimatedImage( $image ) ) { |
| 225 |
|
| 226 |
|
| 227 |
$animation_post[] = '-interlace'; |
| 228 |
$animation_post[] = 'GIF'; |
| 229 |
} |
| 230 |
} elseif ( $params['mimeType'] === 'image/x-xcf' ) { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
$animation_pre = [ |
| 238 |
'-background', 'transparent', |
| 239 |
'-layers', 'merge', |
| 240 |
'-background', 'white', |
| 241 |
]; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
$env = [ 'OMP_NUM_THREADS' => 1 ]; |
| 246 |
if ( (string)$imageMagickTempDir !== '' ) { |
| 247 |
$env['MAGICK_TMPDIR'] = $imageMagickTempDir; |
| 248 |
} |
| 249 |
|
| 250 |
$rotation = isset( $params['disableRotation'] ) ? 0 : $this->getRotation( $image ); |
| 251 |
[ $width, $height ] = $this->extractPreRotationDimensions( $params, $rotation ); |
| 252 |
|
| 253 |
$cmd = Shell::escape( ...array_merge( |
| 254 |
[ $imageMagickConvertCommand ], |
| 255 |
$quality, |
| 256 |
|
| 257 |
|
| 258 |
[ '-background', 'white' ], |
| 259 |
$decoderHint, |
| 260 |
[ $this->escapeMagickInput( $params['srcPath'], $scene ) ], |
| 261 |
$animation_pre, |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
[ '-thumbnail', "{$width}x{$height}!" ], |
| 266 |
|
| 267 |
( $params['comment'] !== '' |
| 268 |
? [ '-set', 'comment', $this->escapeMagickProperty( $params['comment'] ) ] |
| 269 |
: [] ), |
| 270 |
|
| 271 |
[ '+set', 'Thumb::URI' ], |
| 272 |
[ '-depth', 8 ], |
| 273 |
$sharpen, |
| 274 |
[ '-rotate', "-$rotation" ], |
| 275 |
$subsampling, |
| 276 |
$animation_post, |
| 277 |
[ $this->escapeMagickOutput( $params['dstPath'] ) ] ) ); |
| 278 |
|
| 279 |
wfDebug( __METHOD__ . ": running ImageMagick: $cmd" ); |
| 280 |
$retval = 0; |
| 281 |
$err = wfShellExecWithStderr( $cmd, $retval, $env ); |
| 282 |
|
| 283 |
if ( $retval !== 0 ) { |
| 284 |
$this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 285 |
|
| 286 |
return $this->getMediaTransformError( $params, "$err\nError code: $retval" ); |
| 287 |
} |
| 288 |
|
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
protected function transformImageMagickExt( $image, $params ) { |
| 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 ); |
| 307 |
try { |
| 308 |
$im = new Imagick(); |
| 309 |
$im->readImage( $params['srcPath'] ); |
| 310 |
|
| 311 |
if ( $params['mimeType'] === 'image/jpeg' ) { |
| 312 |
|
| 313 |
if ( ( $params['physicalWidth'] + $params['physicalHeight'] ) |
| 314 |
/ ( $params['srcWidth'] + $params['srcHeight'] ) |
| 315 |
< $sharpenReductionThreshold |
| 316 |
) { |
| 317 |
|
| 318 |
[ $radius, $sigma ] = explode( 'x', $sharpenParameter, 2 ); |
| 319 |
$im->sharpenImage( (float)$radius, (float)$sigma ); |
| 320 |
} |
| 321 |
$qualityVal = isset( $params['quality'] ) ? (string)$params['quality'] : null; |
| 322 |
$im->setCompressionQuality( $qualityVal ?: $jpegQuality ); |
| 323 |
if ( $params['interlace'] ) { |
| 324 |
$im->setInterlaceScheme( Imagick::INTERLACE_JPEG ); |
| 325 |
} |
| 326 |
if ( $jpegPixelFormat ) { |
| 327 |
$factors = $this->imageMagickSubsampling( $jpegPixelFormat ); |
| 328 |
$im->setSamplingFactors( $factors ); |
| 329 |
} |
| 330 |
} elseif ( $params['mimeType'] === 'image/png' ) { |
| 331 |
$im->setCompressionQuality( 95 ); |
| 332 |
if ( $params['interlace'] ) { |
| 333 |
$im->setInterlaceScheme( Imagick::INTERLACE_PNG ); |
| 334 |
} |
| 335 |
} elseif ( $params['mimeType'] === 'image/gif' ) { |
| 336 |
if ( $this->getImageArea( $image ) > $maxAnimatedGifArea ) { |
| 337 |
|
| 338 |
|
| 339 |
$im->setImageScene( 0 ); |
| 340 |
} elseif ( $this->isAnimatedImage( $image ) ) { |
| 341 |
|
| 342 |
$im = $im->coalesceImages(); |
| 343 |
} |
| 344 |
|
| 345 |
if ( $params['interlace'] ) { |
| 346 |
$im->setInterlaceScheme( Imagick::INTERLACE_GIF ); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
$rotation = isset( $params['disableRotation'] ) ? 0 : $this->getRotation( $image ); |
| 351 |
[ $width, $height ] = $this->extractPreRotationDimensions( $params, $rotation ); |
| 352 |
|
| 353 |
$im->setImageBackgroundColor( new ImagickPixel( 'white' ) ); |
| 354 |
|
| 355 |
|
| 356 |
foreach ( $im as $i => $frame ) { |
| 357 |
if ( !$frame->thumbnailImage( $width, $height, false ) ) { |
| 358 |
return $this->getMediaTransformError( $params, "Error scaling frame $i" ); |
| 359 |
} |
| 360 |
} |
| 361 |
$im->setImageDepth( 8 ); |
| 362 |
|
| 363 |
if ( $rotation && !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) { |
| 364 |
return $this->getMediaTransformError( $params, "Error rotating $rotation degrees" ); |
| 365 |
} |
| 366 |
|
| 367 |
if ( $this->isAnimatedImage( $image ) ) { |
| 368 |
wfDebug( __METHOD__ . ": Writing animated thumbnail" ); |
| 369 |
|
| 370 |
$result = $im->writeImages( $params['dstPath'], true ); |
| 371 |
} else { |
| 372 |
$result = $im->writeImage( $params['dstPath'] ); |
| 373 |
} |
| 374 |
if ( !$result ) { |
| 375 |
return $this->getMediaTransformError( $params, |
| 376 |
"Unable to write thumbnail to {$params['dstPath']}" ); |
| 377 |
} |
| 378 |
} catch ( ImagickException $e ) { |
| 379 |
return $this->getMediaTransformError( $params, $e->getMessage() ); |
| 380 |
} |
| 381 |
|
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
protected function transformCustom( $image, $params ) { |
| 394 |
|
| 395 |
$customConvertCommand = MediaWikiServices::getInstance()->getMainConfig() |
| 396 |
->get( MainConfigNames::CustomConvertCommand ); |
| 397 |
|
| 398 |
|
| 399 |
|
| 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'] ), |
| 405 |
] ); |
| 406 |
wfDebug( __METHOD__ . ": Running custom convert command $cmd" ); |
| 407 |
$retval = 0; |
| 408 |
$err = wfShellExecWithStderr( $cmd, $retval ); |
| 409 |
|
| 410 |
if ( $retval !== 0 ) { |
| 411 |
$this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 412 |
|
| 413 |
return $this->getMediaTransformError( $params, $err ); |
| 414 |
} |
| 415 |
|
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
protected function transformGd( $image, $params ) { |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
$typemap = [ |
| 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' ], |
| 439 |
]; |
| 440 |
|
| 441 |
if ( !isset( $typemap[$params['mimeType']] ) ) { |
| 442 |
$err = 'Image type not supported'; |
| 443 |
wfDebug( $err ); |
| 444 |
$errMsg = wfMessage( 'thumbnail_image-type' )->text(); |
| 445 |
|
| 446 |
return $this->getMediaTransformError( $params, $errMsg ); |
| 447 |
} |
| 448 |
[ $loader, $colorStyle, $useQuality, $saveType ] = $typemap[$params['mimeType']]; |
| 449 |
|
| 450 |
if ( !function_exists( $loader ) ) { |
| 451 |
$err = "Incomplete GD library configuration: missing function $loader"; |
| 452 |
wfDebug( $err ); |
| 453 |
$errMsg = wfMessage( 'thumbnail_gd-library', $loader )->text(); |
| 454 |
|
| 455 |
return $this->getMediaTransformError( $params, $errMsg ); |
| 456 |
} |
| 457 |
|
| 458 |
if ( !file_exists( $params['srcPath'] ) ) { |
| 459 |
$err = "File seems to be missing: {$params['srcPath']}"; |
| 460 |
wfDebug( $err ); |
| 461 |
$errMsg = wfMessage( 'thumbnail_image-missing', $params['srcPath'] )->text(); |
| 462 |
|
| 463 |
return $this->getMediaTransformError( $params, $errMsg ); |
| 464 |
} |
| 465 |
|
| 466 |
if ( filesize( $params['srcPath'] ) === 0 ) { |
| 467 |
$err = "Image file size seems to be zero."; |
| 468 |
wfDebug( $err ); |
| 469 |
$errMsg = wfMessage( 'thumbnail_image-size-zero', $params['srcPath'] )->text(); |
| 470 |
|
| 471 |
return $this->getMediaTransformError( $params, $errMsg ); |
| 472 |
} |
| 473 |
|
| 474 |
$src_image = $loader( $params['srcPath'] ); |
| 475 |
|
| 476 |
$rotation = function_exists( 'imagerotate' ) && !isset( $params['disableRotation'] ) ? |
| 477 |
$this->getRotation( $image ) : |
| 478 |
0; |
| 479 |
[ $width, $height ] = $this->extractPreRotationDimensions( $params, $rotation ); |
| 480 |
$dst_image = imagecreatetruecolor( $width, $height ); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
$background = imagecolorallocate( $dst_image, 0, 0, 0 ); |
| 485 |
imagecolortransparent( $dst_image, $background ); |
| 486 |
imagealphablending( $dst_image, false ); |
| 487 |
|
| 488 |
if ( $colorStyle === 'palette' ) { |
| 489 |
|
| 490 |
|
| 491 |
imagecopyresized( $dst_image, $src_image, |
| 492 |
0, 0, 0, 0, |
| 493 |
$width, $height, |
| 494 |
imagesx( $src_image ), imagesy( $src_image ) ); |
| 495 |
} else { |
| 496 |
imagecopyresampled( $dst_image, $src_image, |
| 497 |
0, 0, 0, 0, |
| 498 |
$width, $height, |
| 499 |
imagesx( $src_image ), imagesy( $src_image ) ); |
| 500 |
} |
| 501 |
|
| 502 |
if ( $rotation % 360 !== 0 && $rotation % 90 === 0 ) { |
| 503 |
$dst_image = imagerotate( $dst_image, $rotation, 0 ); |
| 504 |
} |
| 505 |
|
| 506 |
imagesavealpha( $dst_image, true ); |
| 507 |
|
| 508 |
$funcParams = [ $dst_image, $params['dstPath'] ]; |
| 509 |
if ( $useQuality && isset( $params['quality'] ) ) { |
| 510 |
$funcParams[] = $params['quality']; |
| 511 |
} |
| 512 |
|
| 513 |
$saveType( ...$funcParams ); |
| 514 |
|
| 515 |
return false; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public static function imageJpegWrapper( $dst_image, $thumbPath, $quality = null ) { |
| 528 |
$jpegQuality = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::JpegQuality ); |
| 529 |
|
| 530 |
imageinterlace( $dst_image ); |
| 531 |
imagejpeg( $dst_image, $thumbPath, $quality ?? $jpegQuality ); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public function canRotate() { |
| 541 |
$scaler = $this->getScalerType( null, false ); |
| 542 |
switch ( $scaler ) { |
| 543 |
case 'im': |
| 544 |
|
| 545 |
return true; |
| 546 |
case 'imext': |
| 547 |
|
| 548 |
return true; |
| 549 |
case 'gd': |
| 550 |
|
| 551 |
|
| 552 |
return function_exists( 'imagerotate' ); |
| 553 |
default: |
| 554 |
|
| 555 |
return false; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public function autoRotateEnabled() { |
| 565 |
$enableAutoRotation = MediaWikiServices::getInstance()->getMainConfig() |
| 566 |
->get( MainConfigNames::EnableAutoRotation ); |
| 567 |
|
| 568 |
if ( $enableAutoRotation === null ) { |
| 569 |
|
| 570 |
return $this->canRotate(); |
| 571 |
} |
| 572 |
|
| 573 |
return $enableAutoRotation; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public function rotate( $file, $params ) { |
| 585 |
$imageMagickConvertCommand = MediaWikiServices::getInstance() |
| 586 |
->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand ); |
| 587 |
|
| 588 |
$rotation = ( $params['rotation'] + $this->getRotation( $file ) ) % 360; |
| 589 |
$scene = false; |
| 590 |
|
| 591 |
$scaler = $this->getScalerType( null, false ); |
| 592 |
switch ( $scaler ) { |
| 593 |
case 'im': |
| 594 |
$cmd = Shell::escape( $imageMagickConvertCommand ) . " " . |
| 595 |
Shell::escape( $this->escapeMagickInput( $params['srcPath'], $scene ) ) . |
| 596 |
" -rotate " . Shell::escape( "-$rotation" ) . " " . |
| 597 |
Shell::escape( $this->escapeMagickOutput( $params['dstPath'] ) ); |
| 598 |
wfDebug( __METHOD__ . ": running ImageMagick: $cmd" ); |
| 599 |
$retval = 0; |
| 600 |
$err = wfShellExecWithStderr( $cmd, $retval ); |
| 601 |
if ( $retval !== 0 ) { |
| 602 |
$this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 603 |
|
| 604 |
return new MediaTransformError( 'thumbnail_error', 0, 0, $err ); |
| 605 |
} |
| 606 |
|
| 607 |
return false; |
| 608 |
case 'imext': |
| 609 |
$im = new Imagick(); |
| 610 |
$im->readImage( $params['srcPath'] ); |
| 611 |
if ( !$im->rotateImage( new ImagickPixel( 'white' ), 360 - $rotation ) ) { |
| 612 |
return new MediaTransformError( 'thumbnail_error', 0, 0, |
| 613 |
"Error rotating $rotation degrees" ); |
| 614 |
} |
| 615 |
$result = $im->writeImage( $params['dstPath'] ); |
| 616 |
if ( !$result ) { |
| 617 |
return new MediaTransformError( 'thumbnail_error', 0, 0, |
| 618 |
"Unable to write image to {$params['dstPath']}" ); |
| 619 |
} |
| 620 |
|
| 621 |
return false; |
| 622 |
default: |
| 623 |
return new MediaTransformError( 'thumbnail_error', 0, 0, |
| 624 |
"$scaler rotation not implemented" ); |
| 625 |
} |
| 626 |
} |
| 627 |
} |