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 
236  public function getImageSize( $image, $path ) {
237  Wikimedia\suppressWarnings();
238  $gis = getimagesize( $path );
239  Wikimedia\restoreWarnings();
240 
241  return $gis;
242  }
243 
254  public function getImageArea( $image ) {
255  return $image->getWidth() * $image->getHeight();
256  }
257 
264  public function getShortDesc( $file ) {
265  global $wgLang;
266  $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
267  $widthheight = wfMessage( 'widthheight' )
268  ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
269 
270  return "$widthheight ($nbytes)";
271  }
272 
279  public function getLongDesc( $file ) {
280  global $wgLang;
281  $pages = $file->pageCount();
282  $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
283  if ( $pages === false || $pages <= 1 ) {
284  $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
285  $file->getHeight() )->params( $size,
286  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
287  } else {
288  $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
289  $file->getHeight() )->params( $size,
290  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
291  }
292 
293  return $msg;
294  }
295 
302  public function getDimensionsString( $file ) {
303  $pages = $file->pageCount();
304  if ( $pages > 1 ) {
305  return wfMessage( 'widthheightpage' )
306  ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
307  } else {
308  return wfMessage( 'widthheight' )
309  ->numParams( $file->getWidth(), $file->getHeight() )->text();
310  }
311  }
312 
317  public function sanitizeParamsForBucketing( $params ) {
318  $params = parent::sanitizeParamsForBucketing( $params );
319 
320  // We unset the height parameters in order to let normaliseParams recalculate them
321  // Otherwise there might be a height discrepancy
322  if ( isset( $params['height'] ) ) {
323  unset( $params['height'] );
324  }
325 
326  if ( isset( $params['physicalHeight'] ) ) {
327  unset( $params['physicalHeight'] );
328  }
329 
330  return $params;
331  }
332 }
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)
Stable to override
Definition: ImageHandler.php:102
ImageHandler\canRender
canRender( $file)
True if the handled types can be transformed.Stable to overridebool Stable to override
Definition: ImageHandler.php:38
ImageHandler\getShortDesc
getShortDesc( $file)
Short description.Shown on Special:Search results.Stable to overridestring Stable to override
Definition: ImageHandler.php:264
$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:1219
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:438
$wgLang
$wgLang
Definition: Setup.php:776
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 St...
Definition: ImageHandler.php:317
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:909
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.Stable to overridestring Dimensions Stable to ove...
Definition: ImageHandler.php:302
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 ().Stable to overridestring...
Definition: ImageHandler.php:279
File\scaleHeight
static scaleHeight( $srcWidth, $srcHeight, $dstWidth)
Calculate the height of a thumbnail using the source and destination width.
Definition: File.php:2138
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:236
$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:675
ImageHandler\getImageArea
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
Definition: ImageHandler.php:254
MediaHandler
Base media handler class.
Definition: MediaHandler.php:37