MediaWiki REL1_35
ImageHandler.php
Go to the documentation of this file.
1<?php
31abstract 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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgLang
Definition Setup.php:781
Media handler abstract base class for images.
canRender( $file)
True if the handled types can be transformed.Stable to overridebool Stable to override
getImageSize( $image, $path)
Get an image size array like that returned by getimagesize(), or false if it can't be determined....
sanitizeParamsForBucketing( $params)
Returns a normalised params array for which parameters have been cleaned up for bucketing purposes St...
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
validateThumbParams(&$width, &$height, $srcWidth, $srcHeight, $mimeType)
Validate thumbnail parameters and fill in the correct height.
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
parseParamString( $str)
Parse a param string made with makeParamString back into an array.array|bool Array of parameters or f...
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.string Stable to overrid...
getLongDesc( $file)
Long description.Shown under image on image description page surounded by ().Stable to overridestring...
validateParam( $name, $value)
Validate a thumbnail parameter at parse time.Return true to accept the parameter, and false to reject...
getScriptedTransform( $image, $script, $params)
Get a MediaTransformOutput object representing an alternate of the transformed output which will call...
getScriptParams( $params)
Stable to override
getDimensionsString( $file)
Shown in file history box on image description page.Stable to overridestring Dimensions Stable to ove...
getShortDesc( $file)
Short description.Shown on Special:Search results.Stable to overridestring Stable to override
Base media handler class.
static fitBoxWidth( $boxWidth, $boxHeight, $maxHeight)
Calculate the largest thumbnail width for a given original file size such that the thumbnail's height...
MediaWiki exception thrown by some methods when the transform parameter array is invalid.
Media transform output for images.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42