MediaWiki REL1_37
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 return in_array( $name, [ 'width', 'height' ] ) && $value > 0;
57 }
58
63 public function makeParamString( $params ) {
64 if ( isset( $params['physicalWidth'] ) ) {
65 $width = $params['physicalWidth'];
66 } elseif ( isset( $params['width'] ) ) {
67 $width = $params['width'];
68 } else {
69 throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
70 }
71
72 # Removed for ProofreadPage
73 # $width = intval( $width );
74 return "{$width}px";
75 }
76
81 public function parseParamString( $str ) {
82 $m = false;
83 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
84 return [ 'width' => $m[1] ];
85 } else {
86 return false;
87 }
88 }
89
94 protected function getScriptParams( $params ) {
95 return [ 'width' => $params['width'] ];
96 }
97
105 public function normaliseParams( $image, &$params ) {
106 $mimeType = $image->getMimeType();
107
108 if ( !isset( $params['width'] ) ) {
109 return false;
110 }
111
112 if ( !isset( $params['page'] ) ) {
113 $params['page'] = 1;
114 } else {
115 $params['page'] = intval( $params['page'] );
116 if ( $params['page'] > $image->pageCount() ) {
117 $params['page'] = $image->pageCount();
118 }
119
120 if ( $params['page'] < 1 ) {
121 $params['page'] = 1;
122 }
123 }
124
125 $srcWidth = $image->getWidth( $params['page'] );
126 $srcHeight = $image->getHeight( $params['page'] );
127
128 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
129 # Height & width were both set
130 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
131 # Height is the relative smaller dimension, so scale width accordingly
132 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
133
134 if ( $params['width'] == 0 ) {
135 # Very small image, so we need to rely on client side scaling :(
136 $params['width'] = 1;
137 }
138
139 $params['physicalWidth'] = $params['width'];
140 } else {
141 # Height was crap, unset it so that it will be calculated later
142 unset( $params['height'] );
143 }
144 }
145
146 if ( !isset( $params['physicalWidth'] ) ) {
147 # Passed all validations, so set the physicalWidth
148 $params['physicalWidth'] = $params['width'];
149 }
150
151 # Because thumbs are only referred to by width, the height always needs
152 # to be scaled by the width to keep the thumbnail sizes consistent,
153 # even if it was set inside the if block above
154 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
155 $params['physicalWidth'] );
156
157 # Set the height if it was not validated in the if block higher up
158 if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
159 $params['height'] = $params['physicalHeight'];
160 }
161
162 if ( !$this->validateThumbParams( $params['physicalWidth'],
163 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType )
164 ) {
165 return false;
166 }
167
168 return true;
169 }
170
181 private function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
182 $width = intval( $width );
183
184 # Sanity check $width
185 if ( $width <= 0 ) {
186 wfDebug( __METHOD__ . ": Invalid destination width: $width" );
187
188 return false;
189 }
190 if ( $srcWidth <= 0 ) {
191 wfDebug( __METHOD__ . ": Invalid source width: $srcWidth" );
192
193 return false;
194 }
195
196 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
197 if ( $height == 0 ) {
198 # Force height to be at least 1 pixel
199 $height = 1;
200 }
201
202 return true;
203 }
204
213 public function getScriptedTransform( $image, $script, $params ) {
214 if ( !$this->normaliseParams( $image, $params ) ) {
215 return false;
216 }
217 $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
218
219 if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
220 return new ThumbnailImage( $image, $url, false, $params );
221 }
222 }
223
224 public function getImageSize( $image, $path ) {
225 Wikimedia\suppressWarnings();
226 $gis = getimagesize( $path );
227 Wikimedia\restoreWarnings();
228
229 return $gis;
230 }
231
232 public function getSizeAndMetadata( $state, $path ) {
233 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
234 $gis = @getimagesize( $path );
235 if ( $gis ) {
236 $info = [
237 'width' => $gis[0],
238 'height' => $gis[1],
239 ];
240 if ( isset( $gis['bits'] ) ) {
241 $info['bits'] = $gis['bits'];
242 }
243 } else {
244 $info = [];
245 }
246 return $info;
247 }
248
259 public function getImageArea( $image ) {
260 return $image->getWidth() * $image->getHeight();
261 }
262
269 public function getShortDesc( $file ) {
270 global $wgLang;
271 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
272 $widthheight = wfMessage( 'widthheight' )
273 ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
274
275 return "$widthheight ($nbytes)";
276 }
277
284 public function getLongDesc( $file ) {
285 global $wgLang;
286 $pages = $file->pageCount();
287 $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
288 if ( $pages === false || $pages <= 1 ) {
289 $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
290 $file->getHeight() )->params( $size,
291 '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
292 } else {
293 $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
294 $file->getHeight() )->params( $size,
295 '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
296 }
297
298 return $msg;
299 }
300
307 public function getDimensionsString( $file ) {
308 $pages = $file->pageCount();
309 if ( $pages > 1 ) {
310 return wfMessage( 'widthheightpage' )
311 ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
312 } else {
313 return wfMessage( 'widthheight' )
314 ->numParams( $file->getWidth(), $file->getHeight() )->text();
315 }
316 }
317
322 public function sanitizeParamsForBucketing( $params ) {
323 $params = parent::sanitizeParamsForBucketing( $params );
324
325 // We unset the height parameters in order to let normaliseParams recalculate them
326 // Otherwise there might be a height discrepancy
327 unset( $params['height'] );
328 unset( $params['physicalHeight'] );
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:831
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.
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...
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
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
getLongDesc( $file)
Long description.Shown under image on image description page surounded by ().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)
getDimensionsString( $file)
Shown in file history box on image description page.to overridestring Dimensions
getShortDesc( $file)
Short description.Shown on Special:Search results.to overridestring
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