17use InvalidArgumentException;
42 if ( !parent::normaliseParams( $image, $params ) ) {
46 # Obtain the source, pre-rotation dimensions
47 $srcWidth = $image->getWidth( $params[
'page'] );
48 $srcHeight = $image->getHeight( $params[
'page'] );
51 $image, $params[
'physicalWidth'], $srcWidth, $srcHeight
53 $params[
'physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $params[
'physicalWidth'] );
55 # Don't make an image bigger than the source
56 if ( $params[
'physicalWidth'] >= $srcWidth ) {
57 $params[
'physicalWidth'] = $srcWidth;
58 $params[
'physicalHeight'] = $srcHeight;
60 # Skip scaling limit checks if no scaling is required
61 # due to requested size being bigger than source.
62 if ( !$image->mustRender() ) {
83 if ( $rotation === 90 || $rotation === 270 ) {
85 $width = $params[
'physicalHeight'];
86 $height = $params[
'physicalWidth'];
88 $width = $params[
'physicalWidth'];
89 $height = $params[
'physicalHeight'];
92 return [ $width, $height ];
109 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
117 'physicalWidth' => $params[
'physicalWidth'],
118 'physicalHeight' => $params[
'physicalHeight'],
119 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
121 'clientWidth' => $params[
'width'],
122 'clientHeight' => $params[
'height'],
124 'comment' => isset( $params[
'descriptionUrl'] )
125 ?
"File source: {$params['descriptionUrl']}"
128 'srcWidth' => $image->getWidth(),
129 'srcHeight' => $image->getHeight(),
130 'mimeType' => $image->getMimeType(),
131 'dstPath' => $dstPath,
133 'interlace' => $params[
'interlace'] ??
false,
136 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
137 $scalerParams[
'quality'] = 30;
141 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
142 $scalerParams[
'page'] = (int)$params[
'page'];
145 # Determine scaler type
148 if ( is_array( $scaler ) ) {
149 $scalerName = get_class( $scaler[0] );
151 $scalerName = $scaler;
154 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
155 "thumbnail of {$image->getPath()} at $dstPath using scaler $scalerName" );
157 if ( !$image->mustRender() &&
158 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
159 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
160 && !isset( $scalerParams[
'quality'] )
162 # normaliseParams (or the user) wants us to return the unscaled image
163 wfDebug( __METHOD__ .
": returning unscaled image" );
168 if ( $scaler ===
'client' ) {
169 # Client-side image scaling, use the source URL
170 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
174 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
179 if ( $flags & self::TRANSFORM_LATER ) {
180 wfDebug( __METHOD__ .
": Transforming later per flags." );
184 # Try to make a target path for the thumbnail
186 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
187 "directory, falling back to client scaling" );
192 # Transform functions and binaries need a FS source file
196 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
197 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
198 $scalerParams[
'disableRotation'] =
true;
201 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
202 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
203 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
205 if ( $scalerParams[
'srcPath'] ===
false ) {
207 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
211 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
220 ->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto );
221 if ( $mto !==
null ) {
222 wfDebug( __METHOD__ .
": Hook to BitmapHandlerTransform created an mto" );
223 $scaler =
'hookaborted';
229 if ( is_array( $scaler ) && is_callable( $scaler ) ) {
231 $err = $scaler( $image, $scalerParams );
235 # Handled by the hook above
236 $err = $mto->isError() ? $mto :
false;
249 $err = $this->
transformGd( $image, $scalerParams );
265 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
286 return $file->getThumbnailSource( $params );
310 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
324 $url = $image->modifyClientThumbUrl( $image->getUrl(), $params );
382 $params[
'clientHeight'], $errMsg );
407 $s = str_replace(
'\\',
'\\\\', $s );
409 $s = str_replace(
'%',
'%%', $s );
411 if ( strlen( $s ) > 0 && ( $s[0] ===
'-' || $s[0] ===
'@' ) ) {
435 # Die on initial metacharacters (caller should prepend path)
436 $firstChar = substr(
$path, 0, 1 );
437 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
438 throw new InvalidArgumentException( __METHOD__ .
': cannot escape this path name' );
442 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
469 # Die on format specifiers (other than drive letters). The regex is
470 # meant to match all the formats you get from "convert -list format"
471 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
476 throw new InvalidArgumentException( __METHOD__ .
': unexpected colon character in path name' );
480 # If there are square brackets, add a do-nothing scene specification
481 # to force a literal interpretation
482 if ( $scene ===
false ) {
483 if ( str_contains(
$path,
'[' ) ) {
501 $method = __METHOD__;
502 return $cache->getWithSetCallback(
503 $cache->makeGlobalKey(
'imagemagick-version' ),
505 static function () use ( $method ) {
509 wfDebug( $method .
": Running convert -version" );
510 $return = Shell::command( $imageMagickConvertCommand,
'-version' )
515 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return, $matches
518 wfDebug( $method .
": ImageMagick version check failed" );
562 public function rotate( $file, $params ) {
564 static::class .
' rotation not implemented' );
593 # For historical reasons, hook starts with BitmapHandler
594 $checkImageAreaHookResult =
null;
596 $file, $params, $checkImageAreaHookResult );
598 if ( $checkImageAreaHookResult !==
null ) {
600 return (
bool)$checkImageAreaHookResult;
603 if ( $maxImageArea ===
false ) {
608 $srcWidth = $file->getWidth( $params[
'page'] );
609 $srcHeight = $file->getHeight( $params[
'page'] );
611 if ( $srcWidth * $srcHeight > $maxImageArea
612 && !( $file->getMimeType() ===
'image/jpeg'
613 && $this->getScalerType(
null,
false ) ===
'im' )
615 # Only ImageMagick can efficiently downsize jpg images without loading
616 # the entire file in memory
624class_alias( TransformationalImageHandler::class,
'TransformationalImageHandler' );
wfIsWindows()
Check if the operating system is Windows.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfHostname()
Get host name of the current machine, for use in error reporting.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A class containing constants representing the names of configuration variables.
const ImageMagickConvertCommand
Name constant for the ImageMagickConvertCommand setting, for use with Config::get()
const MaxImageArea
Name constant for the MaxImageArea setting, for use with Config::get()