MediaWiki master
ImageHandler.php
Go to the documentation of this file.
1<?php
11use Wikimedia\AtEase\AtEase;
12
20abstract class ImageHandler extends MediaHandler {
27 public function canRender( $file ) {
28 return ( $file->getWidth() && $file->getHeight() );
29 }
30
36 public function getParamMap() {
37 return [ 'img_width' => 'width' ];
38 }
39
44 public function validateParam( $name, $value ) {
45 return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
46 }
47
53 public function makeParamString( $params ) {
54 if ( isset( $params['physicalWidth'] ) ) {
55 $width = $params['physicalWidth'];
56 } elseif ( isset( $params['width'] ) ) {
57 $width = $params['width'];
58 } else {
59 throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
60 }
61
62 # Removed for ProofreadPage
63 # $width = intval( $width );
64 return "{$width}px";
65 }
66
71 public function parseParamString( $str ) {
72 $m = false;
73 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
74 return [ 'width' => $m[1] ];
75 }
76 return false;
77 }
78
84 protected function getScriptParams( $params ) {
85 return [ 'width' => $params['width'] ];
86 }
87
96 public function normaliseParams( $image, &$params ) {
97 if ( !isset( $params['width'] ) ) {
98 return false;
99 }
100
101 if ( !isset( $params['page'] ) ) {
102 $params['page'] = 1;
103 } else {
104 $params['page'] = (int)$params['page'];
105 if ( $params['page'] > $image->pageCount() ) {
106 $params['page'] = $image->pageCount();
107 }
108
109 if ( $params['page'] < 1 ) {
110 $params['page'] = 1;
111 }
112 }
113
114 $srcWidth = $image->getWidth( $params['page'] );
115 $srcHeight = $image->getHeight( $params['page'] );
116
117 if ( isset( $params['height'] ) && $params['height'] !== -1 ) {
118 # Height & width were both set
119 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
120 # Height is the relative smaller dimension, so scale width accordingly
121 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
122
123 if ( $params['width'] == 0 ) {
124 # Very small image, so we need to rely on client side scaling :(
125 $params['width'] = 1;
126 }
127
128 $params['physicalWidth'] = $params['width'];
129 } else {
130 # Height was crap, unset it so that it will be calculated later
131 unset( $params['height'] );
132 }
133 }
134
135 if ( !isset( $params['physicalWidth'] ) ) {
136 # Passed all validations, so set the physicalWidth
137 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive, checked above
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 AtEase::suppressWarnings();
215 $gis = getimagesize( $path );
216 AtEase::restoreWarnings();
217
218 return $gis;
219 }
220
222 public function getSizeAndMetadata( $state, $path ) {
223 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
224 $gis = @getimagesize( $path );
225 if ( $gis ) {
226 $info = [
227 'width' => $gis[0],
228 'height' => $gis[1],
229 ];
230 if ( isset( $gis['bits'] ) ) {
231 $info['bits'] = $gis['bits'];
232 }
233 } else {
234 $info = [];
235 }
236 return $info;
237 }
238
249 public function getImageArea( $image ) {
250 return $image->getWidth() * $image->getHeight();
251 }
252
259 public function getShortDesc( $file ) {
260 global $wgLang;
261 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ), ENT_QUOTES );
262 $widthheight = wfMessage( 'widthheight' )
263 ->numParams( $file->getWidth(), $file->getHeight() )
264 ->escaped();
265
266 return "$widthheight ($nbytes)";
267 }
268
275 public function getLongDesc( $file ) {
276 $pages = $file->pageCount();
277 if ( $pages === false || $pages <= 1 ) {
278 $msg = wfMessage( 'file-info-size' )
279 ->numParams( $file->getWidth(), $file->getHeight() )
280 ->sizeParams( $file->getSize() )
281 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )
282 ->parse();
283 } else {
284 $msg = wfMessage( 'file-info-size-pages' )
285 ->numParams( $file->getWidth(), $file->getHeight() )
286 ->sizeParams( $file->getSize() )
287 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )
288 ->parse();
289 }
290
291 return $msg;
292 }
293
300 public function getDimensionsString( $file ) {
301 $pages = $file->pageCount();
302 if ( $pages > 1 ) {
303 return wfMessage( 'widthheightpage' )
304 ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
305 }
306 return wfMessage( 'widthheight' )
307 ->numParams( $file->getWidth(), $file->getHeight() )->text();
308 }
309
314 public function sanitizeParamsForBucketing( $params ) {
315 $params = parent::sanitizeParamsForBucketing( $params );
316
317 // We unset the height parameters in order to let normaliseParams recalculate them
318 // Otherwise there might be a height discrepancy
319 unset( $params['height'] );
320 unset( $params['physicalHeight'] );
321
322 return $params;
323 }
324}
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
Media handler abstract base class for images.
canRender( $file)
True if the handled types can be transformed.to overridebool
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....
getImageArea( $image)
Function that returns the number of pixels to be thumbnailed.
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
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|false Array of parameters or ...
makeParamString( $params)
Merge a parameter array into a string appropriate for inclusion in filenames.string
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...
getScriptParams( $params)
getDimensionsString( $file)
Shown in file history box on image description page.to overridestring Dimensions (plain text)
getShortDesc( $file)
Short description.Shown on Special:Search results.Until MediaWiki 1.45, the return value was poorly d...
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Media transform output for images.