39 if ( !parent::normaliseParams( $image, $params ) ) {
43 # Obtain the source, pre-rotation dimensions
44 $srcWidth = $image->getWidth( $params[
'page'] );
45 $srcHeight = $image->getHeight( $params[
'page'] );
47 # Don't make an image bigger than the source
48 if ( $params[
'physicalWidth'] >= $srcWidth ) {
49 $params[
'physicalWidth'] = $srcWidth;
50 $params[
'physicalHeight'] = $srcHeight;
52 # Skip scaling limit checks if no scaling is required
53 # due to requested size being bigger than source.
54 if ( !$image->mustRender() ) {
75 if ( $rotation === 90 || $rotation === 270 ) {
77 $width = $params[
'physicalHeight'];
78 $height = $params[
'physicalWidth'];
80 $width = $params[
'physicalWidth'];
81 $height = $params[
'physicalHeight'];
84 return [ $width, $height ];
101 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
109 'physicalWidth' => $params[
'physicalWidth'],
110 'physicalHeight' => $params[
'physicalHeight'],
111 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
113 'clientWidth' => $params[
'width'],
114 'clientHeight' => $params[
'height'],
116 'comment' => isset( $params[
'descriptionUrl'] )
117 ?
"File source: {$params['descriptionUrl']}"
120 'srcWidth' => $image->getWidth(),
121 'srcHeight' => $image->getHeight(),
122 'mimeType' => $image->getMimeType(),
123 'dstPath' => $dstPath,
125 'interlace' => $params[
'interlace'] ??
false,
126 'isFilePageThumb' => $params[
'isFilePageThumb'] ??
false,
129 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
130 $scalerParams[
'quality'] = 30;
134 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
135 $scalerParams[
'page'] = (int)$params[
'page'];
138 # Determine scaler type
141 if ( is_array( $scaler ) ) {
142 $scalerName = get_class( $scaler[0] );
144 $scalerName = $scaler;
147 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
148 "thumbnail of {$image->getPath()} at $dstPath using scaler $scalerName" );
150 if ( !$image->mustRender() &&
151 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
152 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
153 && !isset( $scalerParams[
'quality'] )
155 # normaliseParams (or the user) wants us to return the unscaled image
156 wfDebug( __METHOD__ .
": returning unscaled image" );
161 if ( $scaler ===
'client' ) {
162 # Client-side image scaling, use the source URL
163 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
167 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
168 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
172 if ( $flags & self::TRANSFORM_LATER ) {
173 wfDebug( __METHOD__ .
": Transforming later per flags." );
175 'width' => $scalerParams[
'clientWidth'],
176 'height' => $scalerParams[
'clientHeight']
178 if ( isset( $params[
'quality'] ) ) {
179 $newParams[
'quality'] = $params[
'quality'];
181 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
182 $newParams[
'page'] = $params[
'page'];
187 # Try to make a target path for the thumbnail
189 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
190 "directory, falling back to client scaling" );
195 # Transform functions and binaries need a FS source file
199 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
200 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
201 $scalerParams[
'disableRotation'] =
true;
204 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
205 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
206 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
208 if ( $scalerParams[
'srcPath'] ===
false ) {
210 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
214 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
222 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
223 ->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto );
224 if ( $mto !==
null ) {
225 wfDebug( __METHOD__ .
": Hook to BitmapHandlerTransform created an mto" );
226 $scaler =
'hookaborted';
232 if ( is_array( $scaler ) && is_callable( $scaler ) ) {
234 $err = $scaler( $image, $scalerParams );
238 # Handled by the hook above
239 $err = $mto->isError() ? $mto :
false;
252 $err = $this->
transformGd( $image, $scalerParams );
261 # transform returned MediaTransforError
268 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
279 'width' => $scalerParams[
'clientWidth'],
280 'height' => $scalerParams[
'clientHeight']
282 if ( isset( $params[
'quality'] ) ) {
283 $newParams[
'quality'] = $params[
'quality'];
285 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
286 $newParams[
'page'] = $params[
'page'];
288 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
299 return $file->getThumbnailSource( $params );
323 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
338 'width' => $scalerParams[
'clientWidth'],
339 'height' => $scalerParams[
'clientHeight']
342 $url = $image->getUrl();
343 if ( isset( $scalerParams[
'isFilePageThumb'] ) && $scalerParams[
'isFilePageThumb'] ) {
345 $url = $image->getFilePageThumbUrl(
$url );
405 $params[
'clientHeight'], $errMsg );
430 $s = str_replace(
'\\',
'\\\\', $s );
432 $s = str_replace(
'%',
'%%', $s );
434 if ( strlen( $s ) > 0 && ( $s[0] ===
'-' || $s[0] ===
'@' ) ) {
458 # Die on initial metacharacters (caller should prepend path)
459 $firstChar = substr(
$path, 0, 1 );
460 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
461 throw new InvalidArgumentException( __METHOD__ .
': cannot escape this path name' );
465 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
492 # Die on format specifiers (other than drive letters). The regex is
493 # meant to match all the formats you get from "convert -list format"
494 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
499 throw new InvalidArgumentException( __METHOD__ .
': unexpected colon character in path name' );
503 # If there are square brackets, add a do-nothing scene specification
504 # to force a literal interpretation
505 if ( $scene ===
false ) {
506 if ( str_contains(
$path,
'[' ) ) {
523 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
524 $method = __METHOD__;
525 return $cache->getWithSetCallback(
526 $cache->makeGlobalKey(
'imagemagick-version' ),
528 static function () use ( $method ) {
529 $imageMagickConvertCommand = MediaWikiServices::getInstance()
530 ->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand );
532 $cmd = Shell::escape( $imageMagickConvertCommand ) .
' -version';
533 wfDebug( $method .
": Running convert -version" );
537 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
540 wfDebug( $method .
": ImageMagick version check failed" );
584 public function rotate( $file, $params ) {
586 static::class .
' rotation not implemented' );
613 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
615 # For historical reasons, hook starts with BitmapHandler
616 $checkImageAreaHookResult =
null;
617 (
new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onBitmapHandlerCheckImageArea(
618 $file, $params, $checkImageAreaHookResult );
620 if ( $checkImageAreaHookResult !==
null ) {
622 return (
bool)$checkImageAreaHookResult;
625 if ( $maxImageArea ===
false ) {
630 $srcWidth = $file->getWidth( $params[
'page'] );
631 $srcHeight = $file->getHeight( $params[
'page'] );
633 if ( $srcWidth * $srcHeight > $maxImageArea
634 && !( $file->getMimeType() ===
'image/jpeg'
635 && $this->getScalerType(
null,
false ) ===
'im' )
637 # Only ImageMagick can efficiently downsize jpg images without loading
638 # 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.