MediaWiki master
ImageHandler.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
13
21abstract class ImageHandler extends MediaHandler {
28 public function canRender( $file ) {
29 return ( $file->getWidth() && $file->getHeight() );
30 }
31
37 public function getParamMap() {
38 return [ 'img_width' => 'width' ];
39 }
40
45 public function validateParam( $name, $value ) {
46 return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
47 }
48
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
72 public function parseParamString( $str ) {
73 $m = false;
74 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
75 return [ 'width' => $m[1] ];
76 }
77 return false;
78 }
79
85 protected function getScriptParams( $params ) {
86 return [ 'width' => $params['width'] ];
87 }
88
97 public function normaliseParams( $image, &$params ) {
98 if ( !isset( $params['width'] ) ) {
99 return false;
100 }
101
102 if ( !isset( $params['page'] ) ) {
103 $params['page'] = 1;
104 } else {
105 $params['page'] = (int)$params['page'];
106 if ( $params['page'] > $image->pageCount() ) {
107 $params['page'] = $image->pageCount();
108 }
109
110 if ( $params['page'] < 1 ) {
111 $params['page'] = 1;
112 }
113 }
114
115 $srcWidth = $image->getWidth( $params['page'] );
116 $srcHeight = $image->getHeight( $params['page'] );
117
118 if ( isset( $params['height'] ) && $params['height'] !== -1 ) {
119 # Height & width were both set
120 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
121 # Height is the relative smaller dimension, so scale width accordingly
122 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
123
124 if ( $params['width'] == 0 ) {
125 # Very small image, so we need to rely on client side scaling :(
126 $params['width'] = 1;
127 }
128
129 $params['physicalWidth'] = $params['width'];
130 } else {
131 # Height was crap, unset it so that it will be calculated later
132 unset( $params['height'] );
133 }
134 }
135
136 if ( !isset( $params['physicalWidth'] ) ) {
137 # Passed all validations, so set the physicalWidth
138 $params['physicalWidth'] = $params['width'];
139 }
140
141 # Because thumbs are only referred to by width, the height always needs
142 # to be scaled by the width to keep the thumbnail sizes consistent,
143 # even if it was set inside the if block above
144 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
145 $params['physicalWidth'] );
146
147 # Set the height if it was not validated in the if block higher up
148 if ( !isset( $params['height'] ) || $params['height'] === -1 ) {
149 $params['height'] = $params['physicalHeight'];
150 }
151
152 if ( !$this->validateThumbParams( $params['physicalWidth'],
153 $params['physicalHeight'], $srcWidth, $srcHeight )
154 ) {
155 return false;
156 }
157
158 return true;
159 }
160
170 private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight ) {
171 $width = (int)$width;
172
173 if ( $width <= 0 ) {
174 wfDebug( __METHOD__ . ": Invalid destination width: $width" );
175
176 return false;
177 }
178 if ( $srcWidth <= 0 ) {
179 wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
180
181 return false;
182 }
183
184 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
185 if ( $height == 0 ) {
186 # Force height to be at least 1 pixel
187 $height = 1;
188 }
189
190 return true;
191 }
192
201 public function getScriptedTransform( $image, $script, $params ) {
202 if ( !$this->normaliseParams( $image, $params ) ) {
203 return false;
204 }
205 $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
206
207 if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
208 return new ThumbnailImage( $image, $url, false, $params );
209 }
210 }
211
213 public function getImageSize( $image, $path ) {
214 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
215 return @getimagesize( $path );
216 }
217
219 public function getSizeAndMetadata( $state, $path ) {
220 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
221 $gis = @getimagesize( $path );
222 if ( $gis ) {
223 $info = [
224 'width' => $gis[0],
225 'height' => $gis[1],
226 ];
227 if ( isset( $gis['bits'] ) ) {
228 $info['bits'] = $gis['bits'];
229 }
230 } else {
231 $info = [];
232 }
233 return $info;
234 }
235
246 public function getImageArea( $image ) {
247 return $image->getWidth() * $image->getHeight();
248 }
249
256 public function getShortDesc( $file ) {
257 global $wgLang;
258 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ), ENT_QUOTES );
259 $widthheight = wfMessage( 'widthheight' )
260 ->numParams( $file->getWidth(), $file->getHeight() )
261 ->escaped();
262
263 return "$widthheight ($nbytes)";
264 }
265
272 public function getLongDesc( $file ) {
273 $pages = $file->pageCount();
274 if ( $pages === false || $pages <= 1 ) {
275 $msg = wfMessage( 'file-info-size' )
276 ->numParams( $file->getWidth(), $file->getHeight() )
277 ->sizeParams( $file->getSize() )
278 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )
279 ->parse();
280 } else {
281 $msg = wfMessage( 'file-info-size-pages' )
282 ->numParams( $file->getWidth(), $file->getHeight() )
283 ->sizeParams( $file->getSize() )
284 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )
285 ->parse();
286 }
287
288 return $msg;
289 }
290
297 public function getDimensionsString( $file ) {
298 $pages = $file->pageCount();
299 if ( $pages > 1 ) {
300 return wfMessage( 'widthheightpage' )
301 ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
302 }
303 return wfMessage( 'widthheight' )
304 ->numParams( $file->getWidth(), $file->getHeight() )->text();
305 }
306
311 public function sanitizeParamsForBucketing( $params ) {
312 $params = parent::sanitizeParamsForBucketing( $params );
313
314 // We unset the height parameters in order to let normaliseParams recalculate them
315 // Otherwise there might be a height discrepancy
316 unset( $params['height'] );
317 unset( $params['physicalHeight'] );
318
319 return $params;
320 }
321}
322
324class_alias( ImageHandler::class, 'ImageHandler' );
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.
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:551
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Media handler abstract base class for images.
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.string
canRender( $file)
True if the handled types can be transformed.to overridebool
sanitizeParamsForBucketing( $params)
Returns a normalised params array for which parameters have been cleaned up for bucketing purposes....
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
getShortDesc( $file)
Short description.Shown on Special:Search results.Until MediaWiki 1.45, the return value was poorly d...
parseParamString( $str)
Parse a param string made with makeParamString back into an array.array|false Array of parameters or ...
getImageSize( $image, $path)
Get an image size array like that returned by getimagesize(), or false if it can't be determined....
getLongDesc( $file)
Long description.Shown under image on image description page surrounded by ().Until MediaWiki 1....
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...
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
getDimensionsString( $file)
Shown in file history box on image description page.to overridestring Dimensions (plain text)
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
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.