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,
134 'isFilePageThumb' => $params[
'isFilePageThumb'] ??
false,
137 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
138 $scalerParams[
'quality'] = 30;
142 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
143 $scalerParams[
'page'] = (int)$params[
'page'];
146 # Determine scaler type
149 if ( is_array( $scaler ) ) {
150 $scalerName = get_class( $scaler[0] );
152 $scalerName = $scaler;
155 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
156 "thumbnail of {$image->getPath()} at $dstPath using scaler $scalerName" );
158 if ( !$image->mustRender() &&
159 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
160 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
161 && !isset( $scalerParams[
'quality'] )
163 # normaliseParams (or the user) wants us to return the unscaled image
164 wfDebug( __METHOD__ .
": returning unscaled image" );
169 if ( $scaler ===
'client' ) {
170 # Client-side image scaling, use the source URL
171 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
175 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
180 if ( $flags & self::TRANSFORM_LATER ) {
181 wfDebug( __METHOD__ .
": Transforming later per flags." );
183 'width' => $scalerParams[
'clientWidth'],
184 'height' => $scalerParams[
'clientHeight']
186 if ( isset( $params[
'quality'] ) ) {
187 $newParams[
'quality'] = $params[
'quality'];
189 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
190 $newParams[
'page'] = $params[
'page'];
195 # Try to make a target path for the thumbnail
197 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
198 "directory, falling back to client scaling" );
203 # Transform functions and binaries need a FS source file
207 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
208 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
209 $scalerParams[
'disableRotation'] =
true;
212 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
213 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
214 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
216 if ( $scalerParams[
'srcPath'] ===
false ) {
218 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
222 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
231 ->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto );
232 if ( $mto !==
null ) {
233 wfDebug( __METHOD__ .
": Hook to BitmapHandlerTransform created an mto" );
234 $scaler =
'hookaborted';
240 if ( is_array( $scaler ) && is_callable( $scaler ) ) {
242 $err = $scaler( $image, $scalerParams );
246 # Handled by the hook above
247 $err = $mto->isError() ? $mto :
false;
260 $err = $this->
transformGd( $image, $scalerParams );
269 # transform returned MediaTransforError
276 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
287 'width' => $scalerParams[
'clientWidth'],
288 'height' => $scalerParams[
'clientHeight']
290 if ( isset( $params[
'quality'] ) ) {
291 $newParams[
'quality'] = $params[
'quality'];
293 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
294 $newParams[
'page'] = $params[
'page'];
296 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
307 return $file->getThumbnailSource( $params );
331 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
346 'width' => $scalerParams[
'clientWidth'],
347 'height' => $scalerParams[
'clientHeight']
350 $url = $image->getUrl();
351 if ( isset( $scalerParams[
'isFilePageThumb'] ) && $scalerParams[
'isFilePageThumb'] ) {
353 $url = $image->getFilePageThumbUrl(
$url );
413 $params[
'clientHeight'], $errMsg );
438 $s = str_replace(
'\\',
'\\\\', $s );
440 $s = str_replace(
'%',
'%%', $s );
442 if ( strlen( $s ) > 0 && ( $s[0] ===
'-' || $s[0] ===
'@' ) ) {
466 # Die on initial metacharacters (caller should prepend path)
467 $firstChar = substr(
$path, 0, 1 );
468 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
469 throw new InvalidArgumentException( __METHOD__ .
': cannot escape this path name' );
473 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
500 # Die on format specifiers (other than drive letters). The regex is
501 # meant to match all the formats you get from "convert -list format"
502 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
507 throw new InvalidArgumentException( __METHOD__ .
': unexpected colon character in path name' );
511 # If there are square brackets, add a do-nothing scene specification
512 # to force a literal interpretation
513 if ( $scene ===
false ) {
514 if ( str_contains(
$path,
'[' ) ) {
532 $method = __METHOD__;
533 return $cache->getWithSetCallback(
534 $cache->makeGlobalKey(
'imagemagick-version' ),
536 static function () use ( $method ) {
540 $cmd = Shell::escape( $imageMagickConvertCommand ) .
' -version';
541 wfDebug( $method .
": Running convert -version" );
545 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
548 wfDebug( $method .
": ImageMagick version check failed" );
592 public function rotate( $file, $params ) {
594 static::class .
' rotation not implemented' );
623 # For historical reasons, hook starts with BitmapHandler
624 $checkImageAreaHookResult =
null;
626 $file, $params, $checkImageAreaHookResult );
628 if ( $checkImageAreaHookResult !==
null ) {
630 return (
bool)$checkImageAreaHookResult;
633 if ( $maxImageArea ===
false ) {
638 $srcWidth = $file->getWidth( $params[
'page'] );
639 $srcHeight = $file->getHeight( $params[
'page'] );
641 if ( $srcWidth * $srcHeight > $maxImageArea
642 && !( $file->getMimeType() ===
'image/jpeg'
643 && $this->getScalerType(
null,
false ) ===
'im' )
645 # Only ImageMagick can efficiently downsize jpg images without loading
646 # the entire file in memory
654class_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.
wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
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()