MediaWiki  master
ImageHandler.php
Go to the documentation of this file.
1 <?php
31 abstract class ImageHandler extends MediaHandler {
38  public function canRender( $file ) {
39  return ( $file->getWidth() && $file->getHeight() );
40  }
41 
47  public function getParamMap() {
48  return [ 'img_width' => 'width' ];
49  }
50 
55  public function validateParam( $name, $value ) {
56  return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
57  }
58 
63  public function makeParamString( $params ) {
64  if ( isset( $params['physicalWidth'] ) ) {
65  $width = $params['physicalWidth'];
66  } elseif ( isset( $params['width'] ) ) {
67  $width = $params['width'];
68  } else {
69  throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
70  }
71 
72  # Removed for ProofreadPage
73  # $width = intval( $width );
74  return "{$width}px";
75  }
76 
81  public function parseParamString( $str ) {
82  $m = false;
83  if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
84  return [ 'width' => $m[1] ];
85  } else {
86  return false;
87  }
88  }
89 
94  protected function getScriptParams( $params ) {
95  return [ 'width' => $params['width'] ];
96  }
97 
105  public function normaliseParams( $image, &$params ) {
106  $mimeType = $image->getMimeType();
107 
108  if ( !isset( $params['width'] ) ) {
109  return false;
110  }
111 
112  if ( !isset( $params['page'] ) ) {
113  $params['page'] = 1;
114  } else {
115  $params['page'] = intval( $params['page'] );
116  if ( $params['page'] > $image->pageCount() ) {
117  $params['page'] = $image->pageCount();
118  }
119 
120  if ( $params['page'] < 1 ) {
121  $params['page'] = 1;
122  }
123  }
124 
125  $srcWidth = $image->getWidth( $params['page'] );
126  $srcHeight = $image->getHeight( $params['page'] );
127 
128  if ( isset( $params['height'] ) && $params['height'] != -1 ) {
129  # Height & width were both set
130  if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
131  # Height is the relative smaller dimension, so scale width accordingly
132  $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
133 
134  if ( $params['width'] == 0 ) {
135  # Very small image, so we need to rely on client side scaling :(
136  $params['width'] = 1;
137  }
138 
139  $params['physicalWidth'] = $params['width'];
140  } else {
141  # Height was crap, unset it so that it will be calculated later
142  unset( $params['height'] );
143  }
144  }
145 
146  if ( !isset( $params['physicalWidth'] ) ) {
147  # Passed all validations, so set the physicalWidth
148  $params['physicalWidth'] = $params['width'];
149  }
150 
151  # Because thumbs are only referred to by width, the height always needs
152  # to be scaled by the width to keep the thumbnail sizes consistent,
153  # even if it was set inside the if block above
154  $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
155  $params['physicalWidth'] );
156 
157  # Set the height if it was not validated in the if block higher up
158  if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
159  $params['height'] = $params['physicalHeight'];
160  }
161 
162  if ( !$this->validateThumbParams( $params['physicalWidth'],
163  $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType )
164  ) {
165  return false;
166  }
167 
168  return true;
169  }
170 
181  private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
182  $width = intval( $width );
183 
184  if ( $width <= 0 ) {
185  wfDebug( __METHOD__ . ": Invalid destination width: $width" );
186 
187  return false;
188  }
189  if ( $srcWidth <= 0 ) {
190  wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
191 
192  return false;
193  }
194 
195  $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
196  if ( $height == 0 ) {
197  # Force height to be at least 1 pixel
198  $height = 1;
199  }
200 
201  return true;
202  }
203 
212  public function getScriptedTransform( $image, $script, $params ) {
213  if ( !$this->normaliseParams( $image, $params ) ) {
214  return false;
215  }
216  $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
217 
218  if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
219  return new ThumbnailImage( $image, $url, false, $params );
220  }
221  }
222 
223  public function getImageSize( $image, $path ) {
224  Wikimedia\suppressWarnings();
225  $gis = getimagesize( $path );
226  Wikimedia\restoreWarnings();
227 
228  return $gis;
229  }
230 
231  public function getSizeAndMetadata( $state, $path ) {
232  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
233  $gis = @getimagesize( $path );
234  if ( $gis ) {
235  $info = [
236  'width' => $gis[0],
237  'height' => $gis[1],
238  ];
239  if ( isset( $gis['bits'] ) ) {
240  $info['bits'] = $gis['bits'];
241  }
242  } else {
243  $info = [];
244  }
245  return $info;
246  }
247 
258  public function getImageArea( $image ) {
259  return $image->getWidth() * $image->getHeight();
260  }
261 
268  public function getShortDesc( $file ) {
269  global $wgLang;
270  $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
271  $widthheight = wfMessage( 'widthheight' )
272  ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
273 
274  return "$widthheight ($nbytes)";
275  }
276 
283  public function getLongDesc( $file ) {
284  $pages = $file->pageCount();
285  if ( $pages === false || $pages <= 1 ) {
286  $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
287  $file->getHeight() )->sizeParams( $file->getSize() )->params(
288  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
289  } else {
290  $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
291  $file->getHeight() )->sizeParams( $file->getSize() )->params(
292  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
293  }
294 
295  return $msg;
296  }
297 
304  public function getDimensionsString( $file ) {
305  $pages = $file->pageCount();
306  if ( $pages > 1 ) {
307  return wfMessage( 'widthheightpage' )
308  ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
309  } else {
310  return wfMessage( 'widthheight' )
311  ->numParams( $file->getWidth(), $file->getHeight() )->text();
312  }
313  }
314 
319  public function sanitizeParamsForBucketing( $params ) {
320  $params = parent::sanitizeParamsForBucketing( $params );
321 
322  // We unset the height parameters in order to let normaliseParams recalculate them
323  // Otherwise there might be a height discrepancy
324  unset( $params['height'] );
325  unset( $params['physicalHeight'] );
326 
327  return $params;
328  }
329 }
ImageHandler\validateParam
validateParam( $name, $value)
Validate a thumbnail parameter at parse time.Return true to accept the parameter, and false to reject...
Definition: ImageHandler.php:55
ThumbnailImage
Media transform output for images.
Definition: ThumbnailImage.php:29
ImageHandler\parseParamString
parseParamString( $str)
Parse a param string made with makeParamString back into an array.The parameter string without file n...
Definition: ImageHandler.php:81
ImageHandler\getScriptParams
getScriptParams( $params)
Definition: ImageHandler.php:94
ImageHandler\canRender
canRender( $file)
True if the handled types can be transformed.Stability: stableto overridebool
Definition: ImageHandler.php:38
ImageHandler\getShortDesc
getShortDesc( $file)
Short description.Shown on Special:Search results.Stability: stableto overridestring
Definition: ImageHandler.php:268
ImageHandler\getSizeAndMetadata
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
Definition: ImageHandler.php:231
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ImageHandler\getParamMap
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
Definition: ImageHandler.php:47
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
$wgLang
$wgLang
Definition: Setup.php:851
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:422
ImageHandler
Media handler abstract base class for images.
Definition: ImageHandler.php:31
ImageHandler\sanitizeParamsForBucketing
sanitizeParamsForBucketing( $params)
Returns a normalised params array for which parameters have been cleaned up for bucketing purposes....
Definition: ImageHandler.php:319
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
ImageHandler\validateThumbParams
validateThumbParams(&$width, &$height, $srcWidth, $srcHeight, $mimeType)
Validate thumbnail parameters and fill in the correct height.
Definition: ImageHandler.php:181
ImageHandler\getDimensionsString
getDimensionsString( $file)
Shown in file history box on image description page.Stability: stableto overridestring Dimensions
Definition: ImageHandler.php:304
ImageHandler\normaliseParams
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
Definition: ImageHandler.php:105
ImageHandler\getLongDesc
getLongDesc( $file)
Long description.Shown under image on image description page surounded by ().Stability: stableto over...
Definition: ImageHandler.php:283
File\scaleHeight
static scaleHeight( $srcWidth, $srcHeight, $dstWidth)
Calculate the height of a thumbnail using the source and destination width.
Definition: File.php:2158
ImageHandler\makeParamString
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.Array of parameters that...
Definition: ImageHandler.php:63
ImageHandler\getImageSize
getImageSize( $image, $path)
Get an image size array like that returned by getimagesize(), or false if it can't be determined.
Definition: ImageHandler.php:223
$path
$path
Definition: NoLocalSettings.php:25
MediaTransformInvalidParametersException
MediaWiki exception thrown by some methods when the transform parameter array is invalid.
Definition: MediaTransformInvalidParametersException.php:27
ImageHandler\getScriptedTransform
getScriptedTransform( $image, $script, $params)
Get a MediaTransformOutput object representing an alternate of the transformed output which will call...
Definition: ImageHandler.php:212
MediaHandler\fitBoxWidth
static fitBoxWidth( $boxWidth, $boxHeight, $maxHeight)
Calculate the largest thumbnail width for a given original file size such that the thumbnail's height...
Definition: MediaHandler.php:861
ImageHandler\getImageArea
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
Definition: ImageHandler.php:258
MediaHandler
Base media handler class.
Definition: MediaHandler.php:37