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  if ( in_array( $name, [ 'width', 'height' ] ) ) {
57  if ( $value <= 0 ) {
58  return false;
59  } else {
60  return true;
61  }
62  } else {
63  return false;
64  }
65  }
66 
71  public function makeParamString( $params ) {
72  if ( isset( $params['physicalWidth'] ) ) {
73  $width = $params['physicalWidth'];
74  } elseif ( isset( $params['width'] ) ) {
75  $width = $params['width'];
76  } else {
77  throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
78  }
79 
80  # Removed for ProofreadPage
81  # $width = intval( $width );
82  return "{$width}px";
83  }
84 
89  public function parseParamString( $str ) {
90  $m = false;
91  if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
92  return [ 'width' => $m[1] ];
93  } else {
94  return false;
95  }
96  }
97 
102  protected function getScriptParams( $params ) {
103  return [ 'width' => $params['width'] ];
104  }
105 
113  public function normaliseParams( $image, &$params ) {
114  $mimeType = $image->getMimeType();
115 
116  if ( !isset( $params['width'] ) ) {
117  return false;
118  }
119 
120  if ( !isset( $params['page'] ) ) {
121  $params['page'] = 1;
122  } else {
123  $params['page'] = intval( $params['page'] );
124  if ( $params['page'] > $image->pageCount() ) {
125  $params['page'] = $image->pageCount();
126  }
127 
128  if ( $params['page'] < 1 ) {
129  $params['page'] = 1;
130  }
131  }
132 
133  $srcWidth = $image->getWidth( $params['page'] );
134  $srcHeight = $image->getHeight( $params['page'] );
135 
136  if ( isset( $params['height'] ) && $params['height'] != -1 ) {
137  # Height & width were both set
138  if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
139  # Height is the relative smaller dimension, so scale width accordingly
140  $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
141 
142  if ( $params['width'] == 0 ) {
143  # Very small image, so we need to rely on client side scaling :(
144  $params['width'] = 1;
145  }
146 
147  $params['physicalWidth'] = $params['width'];
148  } else {
149  # Height was crap, unset it so that it will be calculated later
150  unset( $params['height'] );
151  }
152  }
153 
154  if ( !isset( $params['physicalWidth'] ) ) {
155  # Passed all validations, so set the physicalWidth
156  $params['physicalWidth'] = $params['width'];
157  }
158 
159  # Because thumbs are only referred to by width, the height always needs
160  # to be scaled by the width to keep the thumbnail sizes consistent,
161  # even if it was set inside the if block above
162  $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
163  $params['physicalWidth'] );
164 
165  # Set the height if it was not validated in the if block higher up
166  if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
167  $params['height'] = $params['physicalHeight'];
168  }
169 
170  if ( !$this->validateThumbParams( $params['physicalWidth'],
171  $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType )
172  ) {
173  return false;
174  }
175 
176  return true;
177  }
178 
189  private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
190  $width = intval( $width );
191 
192  # Sanity check $width
193  if ( $width <= 0 ) {
194  wfDebug( __METHOD__ . ": Invalid destination width: $width" );
195 
196  return false;
197  }
198  if ( $srcWidth <= 0 ) {
199  wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
200 
201  return false;
202  }
203 
204  $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
205  if ( $height == 0 ) {
206  # Force height to be at least 1 pixel
207  $height = 1;
208  }
209 
210  return true;
211  }
212 
221  public function getScriptedTransform( $image, $script, $params ) {
222  if ( !$this->normaliseParams( $image, $params ) ) {
223  return false;
224  }
225  $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
226 
227  if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
228  return new ThumbnailImage( $image, $url, false, $params );
229  }
230  }
231 
232  public function getImageSize( $image, $path ) {
233  Wikimedia\suppressWarnings();
234  $gis = getimagesize( $path );
235  Wikimedia\restoreWarnings();
236 
237  return $gis;
238  }
239 
240  public function getSizeAndMetadata( $state, $path ) {
241  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
242  $gis = @getimagesize( $path );
243  if ( $gis ) {
244  $info = [
245  'width' => $gis[0],
246  'height' => $gis[1],
247  ];
248  if ( isset( $gis['bits'] ) ) {
249  $info['bits'] = $gis['bits'];
250  }
251  } else {
252  $info = [];
253  }
254  return $info;
255  }
256 
267  public function getImageArea( $image ) {
268  return $image->getWidth() * $image->getHeight();
269  }
270 
277  public function getShortDesc( $file ) {
278  global $wgLang;
279  $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
280  $widthheight = wfMessage( 'widthheight' )
281  ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
282 
283  return "$widthheight ($nbytes)";
284  }
285 
292  public function getLongDesc( $file ) {
293  global $wgLang;
294  $pages = $file->pageCount();
295  $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
296  if ( $pages === false || $pages <= 1 ) {
297  $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
298  $file->getHeight() )->params( $size,
299  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
300  } else {
301  $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
302  $file->getHeight() )->params( $size,
303  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
304  }
305 
306  return $msg;
307  }
308 
315  public function getDimensionsString( $file ) {
316  $pages = $file->pageCount();
317  if ( $pages > 1 ) {
318  return wfMessage( 'widthheightpage' )
319  ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
320  } else {
321  return wfMessage( 'widthheight' )
322  ->numParams( $file->getWidth(), $file->getHeight() )->text();
323  }
324  }
325 
330  public function sanitizeParamsForBucketing( $params ) {
331  $params = parent::sanitizeParamsForBucketing( $params );
332 
333  // We unset the height parameters in order to let normaliseParams recalculate them
334  // Otherwise there might be a height discrepancy
335  unset( $params['height'] );
336  unset( $params['physicalHeight'] );
337 
338  return $params;
339  }
340 }
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:89
ImageHandler\getScriptParams
getScriptParams( $params)
Definition: ImageHandler.php:102
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:277
ImageHandler\getSizeAndMetadata
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
Definition: ImageHandler.php:240
$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:1182
$wgLang
$wgLang
Definition: Setup.php:796
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:330
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:189
ImageHandler\getDimensionsString
getDimensionsString( $file)
Shown in file history box on image description page.Stability: stableto overridestring Dimensions
Definition: ImageHandler.php:315
ImageHandler\normaliseParams
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
Definition: ImageHandler.php:113
ImageHandler\getLongDesc
getLongDesc( $file)
Long description.Shown under image on image description page surounded by ().Stability: stableto over...
Definition: ImageHandler.php:292
File\scaleHeight
static scaleHeight( $srcWidth, $srcHeight, $dstWidth)
Calculate the height of a thumbnail using the source and destination width.
Definition: File.php:2177
ImageHandler\makeParamString
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.Array of parameters that...
Definition: ImageHandler.php:71
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:232
$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:221
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:863
ImageHandler\getImageArea
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
Definition: ImageHandler.php:267
MediaHandler
Base media handler class.
Definition: MediaHandler.php:37