| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Shell\Shell; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
abstract class TransformationalImageHandler extends ImageHandler { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function normaliseParams( $image, &$params ) { |
| 52 |
if ( !parent::normaliseParams( $image, $params ) ) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
$srcWidth = $image->getWidth( $params['page'] ); |
| 58 |
$srcHeight = $image->getHeight( $params['page'] ); |
| 59 |
|
| 60 |
$params['physicalWidth'] = $this->getSteppedThumbWidth( |
| 61 |
$image, $params['physicalWidth'], $srcWidth, $srcHeight |
| 62 |
); |
| 63 |
$params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $params['physicalWidth'] ); |
| 64 |
|
| 65 |
|
| 66 |
if ( $params['physicalWidth'] >= $srcWidth ) { |
| 67 |
$params['physicalWidth'] = $srcWidth; |
| 68 |
$params['physicalHeight'] = $srcHeight; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if ( !$image->mustRender() ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function extractPreRotationDimensions( $params, $rotation ) { |
| 93 |
if ( $rotation === 90 || $rotation === 270 ) { |
| 94 |
|
| 95 |
$width = $params['physicalHeight']; |
| 96 |
$height = $params['physicalWidth']; |
| 97 |
} else { |
| 98 |
$width = $params['physicalWidth']; |
| 99 |
$height = $params['physicalHeight']; |
| 100 |
} |
| 101 |
|
| 102 |
return [ $width, $height ]; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 120 |
if ( !$this->normaliseParams( $image, $params ) ) { |
| 121 |
return new TransformParameterError( $params ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
$scalerParams = [ |
| 126 |
|
| 127 |
'physicalWidth' => $params['physicalWidth'], |
| 128 |
'physicalHeight' => $params['physicalHeight'], |
| 129 |
'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}", |
| 130 |
|
| 131 |
'clientWidth' => $params['width'], |
| 132 |
'clientHeight' => $params['height'], |
| 133 |
|
| 134 |
'comment' => isset( $params['descriptionUrl'] ) |
| 135 |
? "File source: {$params['descriptionUrl']}" |
| 136 |
: '', |
| 137 |
|
| 138 |
'srcWidth' => $image->getWidth(), |
| 139 |
'srcHeight' => $image->getHeight(), |
| 140 |
'mimeType' => $image->getMimeType(), |
| 141 |
'dstPath' => $dstPath, |
| 142 |
'dstUrl' => $dstUrl, |
| 143 |
'interlace' => $params['interlace'] ?? false, |
| 144 |
'isFilePageThumb' => $params['isFilePageThumb'] ?? false, |
| 145 |
]; |
| 146 |
|
| 147 |
if ( isset( $params['quality'] ) && $params['quality'] === 'low' ) { |
| 148 |
$scalerParams['quality'] = 30; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if ( $image->isMultipage() && isset( $params['page'] ) ) { |
| 153 |
$scalerParams['page'] = (int)$params['page']; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
$scaler = $this->getScalerType( $dstPath ); |
| 158 |
|
| 159 |
if ( is_array( $scaler ) ) { |
| 160 |
$scalerName = get_class( $scaler[0] ); |
| 161 |
} else { |
| 162 |
$scalerName = $scaler; |
| 163 |
} |
| 164 |
|
| 165 |
wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} " . |
| 166 |
"thumbnail at $dstPath using scaler $scalerName" ); |
| 167 |
|
| 168 |
if ( !$image->mustRender() && |
| 169 |
$scalerParams['physicalWidth'] == $scalerParams['srcWidth'] |
| 170 |
&& $scalerParams['physicalHeight'] == $scalerParams['srcHeight'] |
| 171 |
&& !isset( $scalerParams['quality'] ) |
| 172 |
) { |
| 173 |
|
| 174 |
wfDebug( __METHOD__ . ": returning unscaled image" ); |
| 175 |
|
| 176 |
return $this->getClientScalingThumbnailImage( $image, $scalerParams ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( $scaler === 'client' ) { |
| 180 |
|
| 181 |
|
| 182 |
return $this->getClientScalingThumbnailImage( $image, $scalerParams ); |
| 183 |
} |
| 184 |
|
| 185 |
if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) { |
| 186 |
$maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea ); |
| 187 |
return new TransformTooBigImageAreaError( $params, $maxImageArea ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( $flags & self::TRANSFORM_LATER ) { |
| 191 |
wfDebug( __METHOD__ . ": Transforming later per flags." ); |
| 192 |
$newParams = [ |
| 193 |
'width' => $scalerParams['clientWidth'], |
| 194 |
'height' => $scalerParams['clientHeight'] |
| 195 |
]; |
| 196 |
if ( isset( $params['quality'] ) ) { |
| 197 |
$newParams['quality'] = $params['quality']; |
| 198 |
} |
| 199 |
if ( isset( $params['page'] ) && $params['page'] ) { |
| 200 |
$newParams['page'] = $params['page']; |
| 201 |
} |
| 202 |
return new ThumbnailImage( $image, $dstUrl, false, $newParams ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { |
| 207 |
wfDebug( __METHOD__ . ": Unable to create thumbnail destination " . |
| 208 |
"directory, falling back to client scaling" ); |
| 209 |
|
| 210 |
return $this->getClientScalingThumbnailImage( $image, $scalerParams ); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
$thumbnailSource = $this->getThumbnailSource( $image, $params ); |
| 215 |
|
| 216 |
|
| 217 |
if ( $scalerParams['srcWidth'] != $thumbnailSource['width'] |
| 218 |
|| $scalerParams['srcHeight'] != $thumbnailSource['height'] ) { |
| 219 |
$scalerParams['disableRotation'] = true; |
| 220 |
} |
| 221 |
|
| 222 |
$scalerParams['srcPath'] = $thumbnailSource['path']; |
| 223 |
$scalerParams['srcWidth'] = $thumbnailSource['width']; |
| 224 |
$scalerParams['srcHeight'] = $thumbnailSource['height']; |
| 225 |
|
| 226 |
if ( $scalerParams['srcPath'] === false ) { |
| 227 |
wfDebugLog( 'thumbnail', |
| 228 |
sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"', |
| 229 |
wfHostname(), $image->getName() ) ); |
| 230 |
|
| 231 |
return new MediaTransformError( 'thumbnail_error', |
| 232 |
$scalerParams['clientWidth'], $scalerParams['clientHeight'], |
| 233 |
wfMessage( 'filemissing' ) |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
$mto = null; |
| 240 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 241 |
->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto ); |
| 242 |
if ( $mto !== null ) { |
| 243 |
wfDebug( __METHOD__ . ": Hook to BitmapHandlerTransform created an mto" ); |
| 244 |
$scaler = 'hookaborted'; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if ( is_array( $scaler ) && is_callable( $scaler ) ) { |
| 251 |
|
| 252 |
$err = call_user_func( $scaler, $image, $scalerParams ); |
| 253 |
} else { |
| 254 |
switch ( $scaler ) { |
| 255 |
case 'hookaborted': |
| 256 |
|
| 257 |
$err = $mto->isError() ? $mto : false; |
| 258 |
break; |
| 259 |
case 'im': |
| 260 |
$err = $this->transformImageMagick( $image, $scalerParams ); |
| 261 |
break; |
| 262 |
case 'custom': |
| 263 |
$err = $this->transformCustom( $image, $scalerParams ); |
| 264 |
break; |
| 265 |
case 'imext': |
| 266 |
$err = $this->transformImageMagickExt( $image, $scalerParams ); |
| 267 |
break; |
| 268 |
case 'gd': |
| 269 |
default: |
| 270 |
$err = $this->transformGd( $image, $scalerParams ); |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
$removed = $this->removeBadFile( $dstPath, (bool)$err ); |
| 278 |
if ( $err ) { |
| 279 |
|
| 280 |
return $err; |
| 281 |
} |
| 282 |
|
| 283 |
if ( $removed ) { |
| 284 |
|
| 285 |
return new MediaTransformError( 'thumbnail_error', |
| 286 |
$scalerParams['clientWidth'], $scalerParams['clientHeight'], |
| 287 |
wfMessage( 'unknown-error' ) |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
if ( $mto ) { |
| 292 |
|
| 293 |
return $mto; |
| 294 |
} |
| 295 |
|
| 296 |
$newParams = [ |
| 297 |
'width' => $scalerParams['clientWidth'], |
| 298 |
'height' => $scalerParams['clientHeight'] |
| 299 |
]; |
| 300 |
if ( isset( $params['quality'] ) ) { |
| 301 |
$newParams['quality'] = $params['quality']; |
| 302 |
} |
| 303 |
if ( isset( $params['page'] ) && $params['page'] ) { |
| 304 |
$newParams['page'] = $params['page']; |
| 305 |
} |
| 306 |
return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
protected function getThumbnailSource( $file, $params ) { |
| 317 |
return $file->getThumbnailSource( $params ); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
abstract protected function getScalerType( $dstPath, $checkDstPath = true ); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
protected function getClientScalingThumbnailImage( $image, $scalerParams ) { |
| 355 |
$params = [ |
| 356 |
'width' => $scalerParams['clientWidth'], |
| 357 |
'height' => $scalerParams['clientHeight'] |
| 358 |
]; |
| 359 |
|
| 360 |
$url = $image->getUrl(); |
| 361 |
if ( isset( $scalerParams['isFilePageThumb'] ) && $scalerParams['isFilePageThumb'] ) { |
| 362 |
|
| 363 |
$url = $image->getFilePageThumbUrl( $url ); |
| 364 |
} |
| 365 |
|
| 366 |
return new ThumbnailImage( $image, $url, null, $params ); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
protected function transformImageMagick( $image, $params ) { |
| 381 |
return $this->getMediaTransformError( $params, "Unimplemented" ); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
protected function transformImageMagickExt( $image, $params ) { |
| 396 |
return $this->getMediaTransformError( $params, "Unimplemented" ); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
protected function transformCustom( $image, $params ) { |
| 411 |
return $this->getMediaTransformError( $params, "Unimplemented" ); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public function getMediaTransformError( $params, $errMsg ) { |
| 422 |
return new MediaTransformError( 'thumbnail_error', $params['clientWidth'], |
| 423 |
$params['clientHeight'], $errMsg ); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
protected function transformGd( $image, $params ) { |
| 437 |
return $this->getMediaTransformError( $params, "Unimplemented" ); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
protected function escapeMagickProperty( $s ) { |
| 447 |
|
| 448 |
$s = str_replace( '\\', '\\\\', $s ); |
| 449 |
|
| 450 |
$s = str_replace( '%', '%%', $s ); |
| 451 |
|
| 452 |
if ( strlen( $s ) > 0 && ( $s[0] === '-' || $s[0] === '@' ) ) { |
| 453 |
$s = '\\' . $s; |
| 454 |
} |
| 455 |
|
| 456 |
return $s; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
protected function escapeMagickInput( $path, $scene = false ) { |
| 476 |
|
| 477 |
$firstChar = substr( $path, 0, 1 ); |
| 478 |
if ( $firstChar === '~' || $firstChar === '@' ) { |
| 479 |
throw new InvalidArgumentException( __METHOD__ . ': cannot escape this path name' ); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
$path = preg_replace( '/[*?\[\]{}]/', '\\\\\0', $path ); |
| 484 |
|
| 485 |
return $this->escapeMagickPath( $path, $scene ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
protected function escapeMagickOutput( $path, $scene = false ) { |
| 496 |
$path = str_replace( '%', '%%', $path ); |
| 497 |
|
| 498 |
return $this->escapeMagickPath( $path, $scene ); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
protected function escapeMagickPath( $path, $scene = false ) { |
| 510 |
|
| 511 |
|
| 512 |
if ( preg_match( '/^([a-zA-Z0-9-]+):/', $path, $m ) ) { |
| 513 |
if ( wfIsWindows() && is_dir( $m[0] ) ) { |
| 514 |
|
| 515 |
|
| 516 |
} else { |
| 517 |
throw new InvalidArgumentException( __METHOD__ . ': unexpected colon character in path name' ); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if ( $scene === false ) { |
| 524 |
if ( strpos( $path, '[' ) !== false ) { |
| 525 |
$path .= '[0--1]'; |
| 526 |
} |
| 527 |
} else { |
| 528 |
$path .= "[$scene]"; |
| 529 |
} |
| 530 |
|
| 531 |
return $path; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
protected function getMagickVersion() { |
| 541 |
$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
| 542 |
$method = __METHOD__; |
| 543 |
return $cache->getWithSetCallback( |
| 544 |
$cache->makeGlobalKey( 'imagemagick-version' ), |
| 545 |
$cache::TTL_HOUR, |
| 546 |
static function () use ( $method ) { |
| 547 |
$imageMagickConvertCommand = MediaWikiServices::getInstance() |
| 548 |
->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand ); |
| 549 |
|
| 550 |
$cmd = Shell::escape( $imageMagickConvertCommand ) . ' -version'; |
| 551 |
wfDebug( $method . ": Running convert -version" ); |
| 552 |
$retval = ''; |
| 553 |
$return = wfShellExecWithStderr( $cmd, $retval ); |
| 554 |
$x = preg_match( |
| 555 |
'/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return, $matches |
| 556 |
); |
| 557 |
if ( $x != 1 ) { |
| 558 |
wfDebug( $method . ": ImageMagick version check failed" ); |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
return $matches[1]; |
| 563 |
} |
| 564 |
); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public function canRotate() { |
| 575 |
return false; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public function autoRotateEnabled() { |
| 587 |
return false; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public function rotate( $file, $params ) { |
| 603 |
return new MediaTransformError( 'thumbnail_error', 0, 0, |
| 604 |
static::class . ' rotation not implemented' ); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
public function mustRender( $file ) { |
| 616 |
return $this->canRotate() && $this->getRotation( $file ) != 0; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
public function isImageAreaOkForThumbnaling( $file, &$params ) { |
| 631 |
$maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea ); |
| 632 |
|
| 633 |
|
| 634 |
$checkImageAreaHookResult = null; |
| 635 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onBitmapHandlerCheckImageArea( |
| 636 |
$file, $params, $checkImageAreaHookResult ); |
| 637 |
|
| 638 |
if ( $checkImageAreaHookResult !== null ) { |
| 639 |
|
| 640 |
return (bool)$checkImageAreaHookResult; |
| 641 |
} |
| 642 |
|
| 643 |
if ( $maxImageArea === false ) { |
| 644 |
|
| 645 |
return true; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
$srcWidth = $file->getWidth( $params['page'] ); |
| 650 |
|
| 651 |
$srcHeight = $file->getHeight( $params['page'] ); |
| 652 |
|
| 653 |
if ( $srcWidth * $srcHeight > $maxImageArea |
| 654 |
&& !( $file->getMimeType() === 'image/jpeg' |
| 655 |
&& $this->getScalerType( null, false ) === 'im' ) |
| 656 |
) { |
| 657 |
|
| 658 |
|
| 659 |
return false; |
| 660 |
} |
| 661 |
return true; |
| 662 |
} |
| 663 |
} |