49 if ( !parent::normaliseParams( $image, $params ) ) {
53 # Obtain the source, pre-rotation dimensions
54 $srcWidth = $image->getWidth( $params[
'page'] );
55 $srcHeight = $image->getHeight( $params[
'page'] );
57 # Don't make an image bigger than the source
58 if ( $params[
'physicalWidth'] >= $srcWidth ) {
59 $params[
'physicalWidth'] = $srcWidth;
60 $params[
'physicalHeight'] = $srcHeight;
62 # Skip scaling limit checks if no scaling is required
63 # due to requested size being bigger than source.
64 if ( !$image->mustRender() ) {
85 if ( $rotation == 90 || $rotation == 270 ) {
86 # We'll resize before rotation, so swap the dimensions again
87 $width = $params[
'physicalHeight'];
88 $height = $params[
'physicalWidth'];
90 $width = $params[
'physicalWidth'];
91 $height = $params[
'physicalHeight'];
94 return [ $width, $height ];
111 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
116 # Create a parameter array to pass to the scaler
118 # The size to which the image will be resized
119 'physicalWidth' => $params[
'physicalWidth'],
120 'physicalHeight' => $params[
'physicalHeight'],
121 'physicalDimensions' =>
"{$params['physicalWidth']}x{$params['physicalHeight']}",
122 # The size of the image on the page
123 'clientWidth' => $params[
'width'],
124 'clientHeight' => $params[
'height'],
125 # Comment as will be added to the Exif of the thumbnail
126 'comment' => isset( $params[
'descriptionUrl'] )
127 ?
"File source: {$params['descriptionUrl']}"
129 # Properties of the original image
130 'srcWidth' => $image->getWidth(),
131 'srcHeight' => $image->getHeight(),
132 'mimeType' => $image->getMimeType(),
133 'dstPath' => $dstPath,
135 'interlace' => $params[
'interlace'] ??
false,
138 if ( isset( $params[
'quality'] ) && $params[
'quality'] ===
'low' ) {
139 $scalerParams[
'quality'] = 30;
143 if ( $image->isMultipage() && isset( $params[
'page'] ) ) {
144 $scalerParams[
'page'] = intval( $params[
'page'] );
147 # Determine scaler type
150 if ( is_array( $scaler ) ) {
151 $scalerName = get_class( $scaler[0] );
153 $scalerName = $scaler;
156 wfDebug( __METHOD__ .
": creating {$scalerParams['physicalDimensions']} " .
157 "thumbnail at $dstPath using scaler $scalerName" );
159 if ( !$image->mustRender() &&
160 $scalerParams[
'physicalWidth'] == $scalerParams[
'srcWidth']
161 && $scalerParams[
'physicalHeight'] == $scalerParams[
'srcHeight']
162 && !isset( $scalerParams[
'quality'] )
164 # normaliseParams (or the user) wants us to return the unscaled image
165 wfDebug( __METHOD__ .
": returning unscaled image" );
170 if ( $scaler ==
'client' ) {
171 # Client-side image scaling, use the source URL
172 # Using the destination URL in a TRANSFORM_LATER request would be incorrect
176 if ( $image->isTransformedLocally() && !$this->isImageAreaOkForThumbnaling( $image, $params ) ) {
181 if ( $flags & self::TRANSFORM_LATER ) {
182 wfDebug( __METHOD__ .
": Transforming later per flags." );
184 'width' => $scalerParams[
'clientWidth'],
185 'height' => $scalerParams[
'clientHeight']
187 if ( isset( $params[
'quality'] ) ) {
188 $newParams[
'quality'] = $params[
'quality'];
190 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
191 $newParams[
'page'] = $params[
'page'];
196 # Try to make a target path for the thumbnail
198 wfDebug( __METHOD__ .
": Unable to create thumbnail destination " .
199 "directory, falling back to client scaling" );
204 # Transform functions and binaries need a FS source file
208 if ( $scalerParams[
'srcWidth'] != $thumbnailSource[
'width']
209 || $scalerParams[
'srcHeight'] != $thumbnailSource[
'height'] ) {
210 $scalerParams[
'disableRotation'] =
true;
213 $scalerParams[
'srcPath'] = $thumbnailSource[
'path'];
214 $scalerParams[
'srcWidth'] = $thumbnailSource[
'width'];
215 $scalerParams[
'srcHeight'] = $thumbnailSource[
'height'];
217 if ( $scalerParams[
'srcPath'] ===
false ) {
219 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
223 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
228 # Try a hook. Called "Bitmap" for historical reasons.
231 Hooks::runner()->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 = call_user_func( $scaler, $image, $scalerParams );
246 # Handled by the hook above
247 $err = $mto->isError() ? $mto :
false;
260 $err = $this->
transformGd( $image, $scalerParams );
265 # Remove the file if a zero-byte thumbnail was created, or if there was an error
268 # transform returned MediaTransforError
270 } elseif ( $removed ) {
271 # Thumbnail was zero-byte and had to be removed
273 $scalerParams[
'clientWidth'], $scalerParams[
'clientHeight'],
280 'width' => $scalerParams[
'clientWidth'],
281 'height' => $scalerParams[
'clientHeight']
283 if ( isset( $params[
'quality'] ) ) {
284 $newParams[
'quality'] = $params[
'quality'];
286 if ( isset( $params[
'page'] ) && $params[
'page'] ) {
287 $newParams[
'page'] = $params[
'page'];
289 return new ThumbnailImage( $image, $dstUrl, $dstPath, $newParams );
301 return $file->getThumbnailSource( $params );
325 abstract protected function getScalerType( $dstPath, $checkDstPath =
true );
340 'width' => $scalerParams[
'clientWidth'],
341 'height' => $scalerParams[
'clientHeight']
344 return new ThumbnailImage( $image, $image->getUrl(),
null, $params );
401 $params[
'clientHeight'], $errMsg );
426 $s = str_replace(
'\\',
'\\\\',
$s );
428 $s = str_replace(
'%',
'%%',
$s );
430 if ( strlen(
$s ) > 0 && (
$s[0] ===
'-' ||
$s[0] ===
'@' ) ) {
455 # Die on initial metacharacters (caller should prepend path)
456 $firstChar = substr(
$path, 0, 1 );
457 if ( $firstChar ===
'~' || $firstChar ===
'@' ) {
458 throw new MWException( __METHOD__ .
': cannot escape this path name' );
462 $path = preg_replace(
'/[*?\[\]{}]/',
'\\\\\0',
$path );
490 # Die on format specifiers (other than drive letters). The regex is
491 # meant to match all the formats you get from "convert -list format"
492 if ( preg_match(
'/^([a-zA-Z0-9-]+):/',
$path, $m ) ) {
497 throw new MWException( __METHOD__ .
': unexpected colon character in path name' );
501 # If there are square brackets, add a do-nothing scene specification
502 # to force a literal interpretation
503 if ( $scene ===
false ) {
504 if ( strpos(
$path,
'[' ) !==
false ) {
521 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
522 $method = __METHOD__;
523 return $cache->getWithSetCallback(
524 $cache->makeGlobalKey(
'imagemagick-version' ),
526 function () use ( $method ) {
530 wfDebug( $method .
": Running convert -version" );
534 '/Version: ImageMagick ([0-9]*\.[0-9]*\.[0-9]*)/', $return,
$matches
537 wfDebug( $method .
": ImageMagick version check failed" );
583 static::class .
' rotation not implemented' );
612 # For historical reasons, hook starts with BitmapHandler
613 $checkImageAreaHookResult =
null;
614 Hooks::runner()->onBitmapHandlerCheckImageArea(
615 $file, $params, $checkImageAreaHookResult );
617 if ( $checkImageAreaHookResult !==
null ) {
619 return (
bool)$checkImageAreaHookResult;
622 $srcWidth =
$file->getWidth( $params[
'page'] );
623 $srcHeight =
$file->getHeight( $params[
'page'] );
626 && !(
$file->getMimeType() ==
'image/jpeg'
627 && $this->getScalerType(
false,
false ) ==
'im' )
629 # Only ImageMagick can efficiently downsize jpg images without loading
630 # the entire file in memory
$wgMaxImageArea
The maximum number of pixels a source image can have if it is to be scaled down by a scaler that requ...
$wgImageMagickConvertCommand
The convert command shipped with ImageMagick.
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.
Media transform output for images.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.