53 if ( !parent::normaliseParams( $image, $params ) ) {
57 # Obtain the source, pre-rotation dimensions
58 $srcWidth = $image->getWidth( $params[
'page'] );
59 $srcHeight = $image->getHeight( $params[
'page'] );
61 # Don't make an image bigger than the source
62 if ( $params[
'physicalWidth'] >= $srcWidth ) {
63 $params[
'physicalWidth'] = $srcWidth;
64 $params[
'physicalHeight'] = $srcHeight;
66 # Skip scaling limit checks if no scaling is required
67 # due to requested size being bigger than source.
68 if ( !$image->mustRender() ) {
89 if ( $rotation === 90 || $rotation === 270 ) {
91 $width = $params[
'physicalHeight'];
92 $height = $params[
'physicalWidth'];
94 $width = $params[
'physicalWidth'];
95 $height = $params[
'physicalHeight'];
98 return [ $width, $height ];
115 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
123 'physicalWidth' => $params[
'physicalWidth'],
124 'physicalHeight' => $params[
'physicalHeight'],
125 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
127 'clientWidth' => $params[
'width'],
128 'clientHeight' => $params[
'height'],
130 'comment' => isset( $params[
'descriptionUrl'] )
131 ?
"File source: {$params['descriptionUrl']}"
134 'srcWidth' => $image->getWidth(),
135 'srcHeight' => $image->getHeight(),
136 'mimeType' => $image->getMimeType(),
137 'dstPath' => $dstPath,
139 'interlace' => $params[
'interlace'] ??
false,
140 'isFilePageThumb' => $params[
'isFilePageThumb'] ??
false,
143 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
144 $scalerParams[
'quality'] = 30;
148 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
149 $scalerParams[
'page'] = (int)$params[
'page'];
152 # Determine scaler type
155 if ( is_array( $scaler ) ) {
156 $scalerName = get_class( $scaler[0] );
158 $scalerName = $scaler;
161 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
162 "thumbnail of {$image->getPath()} at $dstPath using scaler $scalerName" );
164 if ( !$image->mustRender() &&
165 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
166 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
167 && !isset( $scalerParams[
'quality'] )
169 # normaliseParams (or the user) wants us to return the unscaled image
170 wfDebug( __METHOD__ .
": returning unscaled image" );
175 if ( $scaler ===
'client' ) {
176 # Client-side image scaling, use the source URL
177 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
181 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
182 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
186 if ( $flags & self::TRANSFORM_LATER ) {
187 wfDebug( __METHOD__ .
": Transforming later per flags." );
189 'width' => $scalerParams[
'clientWidth'],
190 'height' => $scalerParams[
'clientHeight']
192 if ( isset( $params[
'quality'] ) ) {
193 $newParams[
'quality'] = $params[
'quality'];
195 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
196 $newParams[
'page'] = $params[
'page'];
201 # Try to make a target path for the thumbnail
203 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
204 "directory, falling back to client scaling" );
209 # Transform functions and binaries need a FS source file
213 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
214 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
215 $scalerParams[
'disableRotation'] =
true;
218 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
219 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
220 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
222 if ( $scalerParams[
'srcPath'] ===
false ) {
224 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
228 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
236 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
237 ->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto );
238 if ( $mto !==
null ) {
239 wfDebug( __METHOD__ .
": Hook to BitmapHandlerTransform created an mto" );
240 $scaler =
'hookaborted';
246 if ( is_array( $scaler ) && is_callable( $scaler ) ) {
248 $err = $scaler( $image, $scalerParams );
252 # Handled by the hook above
253 $err = $mto->isError() ? $mto :
false;
266 $err = $this->
transformGd( $image, $scalerParams );
275 # transform returned MediaTransforError
282 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
293 'width' => $scalerParams[
'clientWidth'],
294 'height' => $scalerParams[
'clientHeight']
296 if ( isset( $params[
'quality'] ) ) {
297 $newParams[
'quality'] = $params[
'quality'];
299 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
300 $newParams[
'page'] = $params[
'page'];
302 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
313 return $file->getThumbnailSource( $params );
337 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
352 'width' => $scalerParams[
'clientWidth'],
353 'height' => $scalerParams[
'clientHeight']
356 $url = $image->getUrl();
357 if ( isset( $scalerParams[
'isFilePageThumb'] ) && $scalerParams[
'isFilePageThumb'] ) {
359 $url = $image->getFilePageThumbUrl(
$url );
419 $params[
'clientHeight'], $errMsg );
444 $s = str_replace(
'\\',
'\\\\', $s );
446 $s = str_replace(
'%',
'%%', $s );
448 if ( strlen( $s ) > 0 && ( $s[0] ===
'-' || $s[0] ===
'@' ) ) {
472 # Die on initial metacharacters (caller should prepend path)
473 $firstChar = substr(
$path, 0, 1 );
474 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
475 throw new InvalidArgumentException( __METHOD__ .
': cannot escape this path name' );
479 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
506 # Die on format specifiers (other than drive letters). The regex is
507 # meant to match all the formats you get from "convert -list format"
508 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
513 throw new InvalidArgumentException( __METHOD__ .
': unexpected colon character in path name' );
517 # If there are square brackets, add a do-nothing scene specification
518 # to force a literal interpretation
519 if ( $scene ===
false ) {
520 if ( strpos(
$path,
'[' ) !==
false ) {
537 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
538 $method = __METHOD__;
539 return $cache->getWithSetCallback(
540 $cache->makeGlobalKey(
'imagemagick-version' ),
542 static function () use ( $method ) {
543 $imageMagickConvertCommand = MediaWikiServices::getInstance()
544 ->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand );
546 $cmd = Shell::escape( $imageMagickConvertCommand ) .
' -version';
547 wfDebug( $method .
": Running convert -version" );
551 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
554 wfDebug( $method .
": ImageMagick version check failed" );
598 public function rotate( $file, $params ) {
600 static::class .
' rotation not implemented' );
627 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
629 # For historical reasons, hook starts with BitmapHandler
630 $checkImageAreaHookResult =
null;
631 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onBitmapHandlerCheckImageArea(
632 $file, $params, $checkImageAreaHookResult );
634 if ( $checkImageAreaHookResult !==
null ) {
636 return (
bool)$checkImageAreaHookResult;
639 if ( $maxImageArea ===
false ) {
645 $srcWidth = $file->getWidth( $params[
'page'] );
647 $srcHeight = $file->getHeight( $params[
'page'] );
649 if ( $srcWidth * $srcHeight > $maxImageArea
650 && !( $file->getMimeType() ===
'image/jpeg'
651 && $this->getScalerType(
null,
false ) ===
'im' )
653 # Only ImageMagick can efficiently downsize jpg images without loading
654 # the entire file in memory
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.
Media handler abstract base class for images.
A class containing constants representing the names of configuration variables.
Media transform output for images.