MediaWiki  1.30.0
ImageHandler.php
Go to the documentation of this file.
1 <?php
29 abstract class ImageHandler extends MediaHandler {
34  public function canRender( $file ) {
35  return ( $file->getWidth() && $file->getHeight() );
36  }
37 
38  public function getParamMap() {
39  return [ 'img_width' => 'width' ];
40  }
41 
42  public function validateParam( $name, $value ) {
43  if ( in_array( $name, [ 'width', 'height' ] ) ) {
44  if ( $value <= 0 ) {
45  return false;
46  } else {
47  return true;
48  }
49  } else {
50  return false;
51  }
52  }
53 
54  public function makeParamString( $params ) {
55  if ( isset( $params['physicalWidth'] ) ) {
56  $width = $params['physicalWidth'];
57  } elseif ( isset( $params['width'] ) ) {
58  $width = $params['width'];
59  } else {
60  throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
61  }
62 
63  # Removed for ProofreadPage
64  # $width = intval( $width );
65  return "{$width}px";
66  }
67 
68  public function parseParamString( $str ) {
69  $m = false;
70  if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
71  return [ 'width' => $m[1] ];
72  } else {
73  return false;
74  }
75  }
76 
77  function getScriptParams( $params ) {
78  return [ 'width' => $params['width'] ];
79  }
80 
86  function normaliseParams( $image, &$params ) {
87  $mimeType = $image->getMimeType();
88 
89  if ( !isset( $params['width'] ) ) {
90  return false;
91  }
92 
93  if ( !isset( $params['page'] ) ) {
94  $params['page'] = 1;
95  } else {
96  $params['page'] = intval( $params['page'] );
97  if ( $params['page'] > $image->pageCount() ) {
98  $params['page'] = $image->pageCount();
99  }
100 
101  if ( $params['page'] < 1 ) {
102  $params['page'] = 1;
103  }
104  }
105 
106  $srcWidth = $image->getWidth( $params['page'] );
107  $srcHeight = $image->getHeight( $params['page'] );
108 
109  if ( isset( $params['height'] ) && $params['height'] != -1 ) {
110  # Height & width were both set
111  if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
112  # Height is the relative smaller dimension, so scale width accordingly
113  $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
114 
115  if ( $params['width'] == 0 ) {
116  # Very small image, so we need to rely on client side scaling :(
117  $params['width'] = 1;
118  }
119 
120  $params['physicalWidth'] = $params['width'];
121  } else {
122  # Height was crap, unset it so that it will be calculated later
123  unset( $params['height'] );
124  }
125  }
126 
127  if ( !isset( $params['physicalWidth'] ) ) {
128  # Passed all validations, so set the physicalWidth
129  $params['physicalWidth'] = $params['width'];
130  }
131 
132  # Because thumbs are only referred to by width, the height always needs
133  # to be scaled by the width to keep the thumbnail sizes consistent,
134  # even if it was set inside the if block above
135  $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
136  $params['physicalWidth'] );
137 
138  # Set the height if it was not validated in the if block higher up
139  if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
140  $params['height'] = $params['physicalHeight'];
141  }
142 
143  if ( !$this->validateThumbParams( $params['physicalWidth'],
144  $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType )
145  ) {
146  return false;
147  }
148 
149  return true;
150  }
151 
162  function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
163  $width = intval( $width );
164 
165  # Sanity check $width
166  if ( $width <= 0 ) {
167  wfDebug( __METHOD__ . ": Invalid destination width: $width\n" );
168 
169  return false;
170  }
171  if ( $srcWidth <= 0 ) {
172  wfDebug( __METHOD__ . ": Invalid source width: $srcWidth\n" );
173 
174  return false;
175  }
176 
177  $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
178  if ( $height == 0 ) {
179  # Force height to be at least 1 pixel
180  $height = 1;
181  }
182 
183  return true;
184  }
185 
192  function getScriptedTransform( $image, $script, $params ) {
193  if ( !$this->normaliseParams( $image, $params ) ) {
194  return false;
195  }
196  $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
197 
198  if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
199  return new ThumbnailImage( $image, $url, false, $params );
200  }
201  }
202 
203  function getImageSize( $image, $path ) {
204  MediaWiki\suppressWarnings();
205  $gis = getimagesize( $path );
206  MediaWiki\restoreWarnings();
207 
208  return $gis;
209  }
210 
220  function getImageArea( $image ) {
221  return $image->getWidth() * $image->getHeight();
222  }
223 
228  function getShortDesc( $file ) {
229  global $wgLang;
230  $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
231  $widthheight = wfMessage( 'widthheight' )
232  ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
233 
234  return "$widthheight ($nbytes)";
235  }
236 
241  function getLongDesc( $file ) {
242  global $wgLang;
243  $pages = $file->pageCount();
244  $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
245  if ( $pages === false || $pages <= 1 ) {
246  $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
247  $file->getHeight() )->params( $size,
248  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
249  } else {
250  $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
251  $file->getHeight() )->params( $size,
252  '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
253  }
254 
255  return $msg;
256  }
257 
262  function getDimensionsString( $file ) {
263  $pages = $file->pageCount();
264  if ( $pages > 1 ) {
265  return wfMessage( 'widthheightpage' )
266  ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
267  } else {
268  return wfMessage( 'widthheight' )
269  ->numParams( $file->getWidth(), $file->getHeight() )->text();
270  }
271  }
272 
273  public function sanitizeParamsForBucketing( $params ) {
274  $params = parent::sanitizeParamsForBucketing( $params );
275 
276  // We unset the height parameters in order to let normaliseParams recalculate them
277  // Otherwise there might be a height discrepancy
278  if ( isset( $params['height'] ) ) {
279  unset( $params['height'] );
280  }
281 
282  if ( isset( $params['physicalHeight'] ) ) {
283  unset( $params['physicalHeight'] );
284  }
285 
286  return $params;
287  }
288 }
ImageHandler\validateParam
validateParam( $name, $value)
Validate a thumbnail parameter at parse time.
Definition: ImageHandler.php:42
ThumbnailImage
Media transform output for images.
Definition: MediaTransformOutput.php:277
ImageHandler\parseParamString
parseParamString( $str)
Parse a param string made with makeParamString back into an array.
Definition: ImageHandler.php:68
ImageHandler\getScriptParams
getScriptParams( $params)
Definition: ImageHandler.php:77
ImageHandler\canRender
canRender( $file)
Definition: ImageHandler.php:34
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
ImageHandler\getShortDesc
getShortDesc( $file)
Definition: ImageHandler.php:228
$params
$params
Definition: styleTest.css.php:40
ImageHandler\getParamMap
getParamMap()
Get an associative array mapping magic word IDs to parameter names.
Definition: ImageHandler.php:38
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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:534
ImageHandler
Media handler abstract base class for images.
Definition: ImageHandler.php:29
ImageHandler\sanitizeParamsForBucketing
sanitizeParamsForBucketing( $params)
Returns a normalised params array for which parameters have been cleaned up for bucketing purposes.
Definition: ImageHandler.php:273
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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:1047
ImageHandler\validateThumbParams
validateThumbParams(&$width, &$height, $srcWidth, $srcHeight, $mimeType)
Validate thumbnail parameters and fill in the correct height.
Definition: ImageHandler.php:162
ImageHandler\getDimensionsString
getDimensionsString( $file)
Definition: ImageHandler.php:262
$image
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:781
ImageHandler\normaliseParams
normaliseParams( $image, &$params)
Definition: ImageHandler.php:86
$value
$value
Definition: styleTest.css.php:45
ImageHandler\getLongDesc
getLongDesc( $file)
Definition: ImageHandler.php:241
File\scaleHeight
static scaleHeight( $srcWidth, $srcHeight, $dstWidth)
Calculate the height of a thumbnail using the source and destination width.
Definition: File.php:1989
ImageHandler\makeParamString
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.
Definition: ImageHandler.php:54
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:203
$path
$path
Definition: NoLocalSettings.php:26
MediaTransformInvalidParametersException
MediaWiki exception thrown by some methods when the transform parameter array is invalid.
Definition: MediaTransformInvalidParametersException.php:26
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
ImageHandler\getScriptedTransform
getScriptedTransform( $image, $script, $params)
Definition: ImageHandler.php:192
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:630
ImageHandler\getImageArea
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
Definition: ImageHandler.php:220
MediaHandler
Base media handler class.
Definition: MediaHandler.php:30