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 ) {
88 # We'll resize before rotation, so swap the dimensions again
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 ) {
118 # Create a parameter array to pass to the scaler
120 # The size to which the image will be resized
121 'physicalWidth' => $params[
'physicalWidth'],
122 'physicalHeight' => $params[
'physicalHeight'],
123 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
124 # The size of the image on the page
125 'clientWidth' => $params[
'width'],
126 'clientHeight' => $params[
'height'],
127 # Comment as will be added to the Exif of the thumbnail
128 'comment' => isset( $params[
'descriptionUrl'] )
129 ?
"File source: {$params['descriptionUrl']}"
131 # Properties of the original image
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'] = intval( $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'],
231 # Try a hook. Called "Bitmap" for historical reasons.
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 );
268 # Remove the file if a zero-byte thumbnail was created, or if there was an error
272 # transform returned MediaTransforError
274 } elseif ( $removed ) {
275 # Thumbnail was zero-byte and had to be removed
277 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
285 'width' => $scalerParams[
'clientWidth'],
286 'height' => $scalerParams[
'clientHeight']
288 if ( isset( $params[
'quality'] ) ) {
289 $newParams[
'quality'] = $params[
'quality'];
291 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
292 $newParams[
'page'] = $params[
'page'];
294 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
306 return $file->getThumbnailSource( $params );
330 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
345 'width' => $scalerParams[
'clientWidth'],
346 'height' => $scalerParams[
'clientHeight']
349 $url = $image->getUrl();
350 if ( isset( $scalerParams[
'isFilePageThumb'] ) && $scalerParams[
'isFilePageThumb'] ) {
352 $url = $image->getFilePageThumbUrl( $url );
412 $params[
'clientHeight'], $errMsg );
437 $s = str_replace(
'\\',
'\\\\',
$s );
439 $s = str_replace(
'%',
'%%',
$s );
441 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 MWException( __METHOD__ .
': cannot escape this path name' );
473 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
501 # Die on format specifiers (other than drive letters). The regex is
502 # meant to match all the formats you get from "convert -list format"
503 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
508 throw new MWException( __METHOD__ .
': unexpected colon character in path name' );
512 # If there are square brackets, add a do-nothing scene specification
513 # to force a literal interpretation
514 if ( $scene ===
false ) {
515 if ( strpos(
$path,
'[' ) !==
false ) {
532 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
533 $method = __METHOD__;
534 return $cache->getWithSetCallback(
535 $cache->makeGlobalKey(
'imagemagick-version' ),
537 static function () use ( $method ) {
538 $imageMagickConvertCommand = MediaWikiServices::getInstance()
539 ->getMainConfig()->get( MainConfigNames::ImageMagickConvertCommand );
541 $cmd = Shell::escape( $imageMagickConvertCommand ) .
' -version';
542 wfDebug( $method .
": Running convert -version" );
546 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
549 wfDebug( $method .
": ImageMagick version check failed" );
595 static::class .
' rotation not implemented' );
622 $maxImageArea = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxImageArea );
624 # For historical reasons, hook starts with BitmapHandler
625 $checkImageAreaHookResult =
null;
626 Hooks::runner()->onBitmapHandlerCheckImageArea(
627 $file, $params, $checkImageAreaHookResult );
629 if ( $checkImageAreaHookResult !==
null ) {
631 return (
bool)$checkImageAreaHookResult;
634 if ( $maxImageArea ===
false ) {
640 $srcWidth =
$file->getWidth( $params[
'page'] );
642 $srcHeight =
$file->getHeight( $params[
'page'] );
644 if ( $srcWidth * $srcHeight > $maxImageArea
645 && !(
$file->getMimeType() ==
'image/jpeg'
646 && $this->getScalerType(
null,
false ) ==
'im' )
648 # Only ImageMagick can efficiently downsize jpg images without loading
649 # the entire file in memory
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.
wfIsWindows()
Check if the operating system is Windows.
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.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.