51 if ( !parent::normaliseParams( $image, $params ) ) {
55 # Obtain the source, pre-rotation dimensions
56 $srcWidth = $image->getWidth( $params[
'page'] );
57 $srcHeight = $image->getHeight( $params[
'page'] );
59 # Don't make an image bigger than the source
60 if ( $params[
'physicalWidth'] >= $srcWidth ) {
61 $params[
'physicalWidth'] = $srcWidth;
62 $params[
'physicalHeight'] = $srcHeight;
64 # Skip scaling limit checks if no scaling is required
65 # due to requested size being bigger than source.
66 if ( !$image->mustRender() ) {
87 if ( $rotation === 90 || $rotation === 270 ) {
89 $width = $params[
'physicalHeight'];
90 $height = $params[
'physicalWidth'];
92 $width = $params[
'physicalWidth'];
93 $height = $params[
'physicalHeight'];
96 return [ $width, $height ];
113 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
121 'physicalWidth' => $params[
'physicalWidth'],
122 'physicalHeight' => $params[
'physicalHeight'],
123 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
125 'clientWidth' => $params[
'width'],
126 'clientHeight' => $params[
'height'],
128 'comment' => isset( $params[
'descriptionUrl'] )
129 ?
"File source: {$params['descriptionUrl']}"
132 'srcWidth' => $image->getWidth(),
133 'srcHeight' => $image->getHeight(),
134 'mimeType' => $image->getMimeType(),
135 'dstPath' => $dstPath,
137 'interlace' => $params[
'interlace'] ??
false,
138 'isFilePageThumb' => $params[
'isFilePageThumb'] ??
false,
141 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
142 $scalerParams[
'quality'] = 30;
146 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
147 $scalerParams[
'page'] = (int)$params[
'page'];
150 # Determine scaler type
153 if ( is_array( $scaler ) ) {
154 $scalerName = get_class( $scaler[0] );
156 $scalerName = $scaler;
159 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
160 "thumbnail at $dstPath using scaler $scalerName" );
162 if ( !$image->mustRender() &&
163 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
164 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
165 && !isset( $scalerParams[
'quality'] )
167 # normaliseParams (or the user) wants us to return the unscaled image
168 wfDebug( __METHOD__ .
": returning unscaled image" );
173 if ( $scaler ===
'client' ) {
174 # Client-side image scaling, use the source URL
175 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
179 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
180 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
184 if ( $flags & self::TRANSFORM_LATER ) {
185 wfDebug( __METHOD__ .
": Transforming later per flags." );
187 'width' => $scalerParams[
'clientWidth'],
188 'height' => $scalerParams[
'clientHeight']
190 if ( isset( $params[
'quality'] ) ) {
191 $newParams[
'quality'] = $params[
'quality'];
193 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
194 $newParams[
'page'] = $params[
'page'];
199 # Try to make a target path for the thumbnail
201 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
202 "directory, falling back to client scaling" );
207 # Transform functions and binaries need a FS source file
211 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
212 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
213 $scalerParams[
'disableRotation'] =
true;
216 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
217 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
218 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
220 if ( $scalerParams[
'srcPath'] ===
false ) {
222 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
226 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
234 Hooks::runner()->onBitmapHandlerTransform( $this, $image, $scalerParams, $mto );
235 if ( $mto !==
null ) {
236 wfDebug( __METHOD__ .
": Hook to BitmapHandlerTransform created an mto" );
237 $scaler =
'hookaborted';
243 if ( is_array( $scaler ) && is_callable( $scaler ) ) {
245 $err = call_user_func( $scaler, $image, $scalerParams );
249 # Handled by the hook above
250 $err = $mto->isError() ? $mto :
false;
263 $err = $this->
transformGd( $image, $scalerParams );
272 # transform returned MediaTransforError
279 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
290 'width' => $scalerParams[
'clientWidth'],
291 'height' => $scalerParams[
'clientHeight']
293 if ( isset( $params[
'quality'] ) ) {
294 $newParams[
'quality'] = $params[
'quality'];
296 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
297 $newParams[
'page'] = $params[
'page'];
299 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
310 return $file->getThumbnailSource( $params );
334 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
349 'width' => $scalerParams[
'clientWidth'],
350 'height' => $scalerParams[
'clientHeight']
353 $url = $image->getUrl();
354 if ( isset( $scalerParams[
'isFilePageThumb'] ) && $scalerParams[
'isFilePageThumb'] ) {
356 $url = $image->getFilePageThumbUrl( $url );
416 $params[
'clientHeight'], $errMsg );
441 $s = str_replace(
'\\',
'\\\\', $s );
443 $s = str_replace(
'%',
'%%', $s );
445 if ( strlen( $s ) > 0 && ( $s[0] ===
'-' || $s[0] ===
'@' ) ) {
470 # Die on initial metacharacters (caller should prepend path)
471 $firstChar = substr(
$path, 0, 1 );
472 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
473 throw new MWException( __METHOD__ .
': cannot escape this path name' );
477 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
505 # Die on format specifiers (other than drive letters). The regex is
506 # meant to match all the formats you get from "convert -list format"
507 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
512 throw new MWException( __METHOD__ .
': unexpected colon character in path name' );
516 # If there are square brackets, add a do-nothing scene specification
517 # to force a literal interpretation
518 if ( $scene ===
false ) {
519 if ( strpos(
$path,
'[' ) !==
false ) {
536 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
537 $method = __METHOD__;
538 return $cache->getWithSetCallback(
539 $cache->makeGlobalKey(
'imagemagick-version' ),
541 static function () use ( $method ) {
542 $imageMagickConvertCommand = MediaWikiServices::getInstance()
543 ->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand );
545 $cmd = Shell::escape( $imageMagickConvertCommand ) .
' -version';
546 wfDebug( $method .
": Running convert -version" );
550 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
553 wfDebug( $method .
": ImageMagick version check failed" );
599 static::class .
' rotation not implemented' );
626 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
628 # For historical reasons, hook starts with BitmapHandler
629 $checkImageAreaHookResult =
null;
630 Hooks::runner()->onBitmapHandlerCheckImageArea(
631 $file, $params, $checkImageAreaHookResult );
633 if ( $checkImageAreaHookResult !==
null ) {
635 return (
bool)$checkImageAreaHookResult;
638 if ( $maxImageArea ===
false ) {
644 $srcWidth =
$file->getWidth( $params[
'page'] );
646 $srcHeight =
$file->getHeight( $params[
'page'] );
648 if ( $srcWidth * $srcHeight > $maxImageArea
649 && !(
$file->getMimeType() ===
'image/jpeg'
650 && $this->getScalerType(
null,
false ) ===
'im' )
652 # Only ImageMagick can efficiently downsize jpg images without loading
653 # 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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.