Merge "Typo fix"
[mediawiki.git] / includes / media / ImageHandler.php
blob8e46f2f2fa3685ce6ec96e885ac550efc365222c
1 <?php
2 /**
3 * Media-handling base classes and generic functionality.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Media
24 /**
25 * Media handler abstract base class for images
27 * @ingroup Media
29 abstract class ImageHandler extends MediaHandler {
31 /**
32 * @param $file File
33 * @return bool
35 function canRender( $file ) {
36 return ( $file->getWidth() && $file->getHeight() );
39 function getParamMap() {
40 return array( 'img_width' => 'width' );
43 function validateParam( $name, $value ) {
44 if ( in_array( $name, array( 'width', 'height' ) ) ) {
45 if ( $value <= 0 ) {
46 return false;
47 } else {
48 return true;
50 } else {
51 return false;
55 function makeParamString( $params ) {
56 if ( isset( $params['physicalWidth'] ) ) {
57 $width = $params['physicalWidth'];
58 } elseif ( isset( $params['width'] ) ) {
59 $width = $params['width'];
60 } else {
61 throw new MWException( 'No width specified to ' . __METHOD__ );
63 # Removed for ProofreadPage
64 #$width = intval( $width );
65 return "{$width}px";
68 function parseParamString( $str ) {
69 $m = false;
70 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
71 return array( 'width' => $m[1] );
72 } else {
73 return false;
77 function getScriptParams( $params ) {
78 return array( 'width' => $params['width'] );
81 /**
82 * @param $image File
83 * @param $params
84 * @return bool
86 function normaliseParams( $image, &$params ) {
87 $mimeType = $image->getMimeType();
89 if ( !isset( $params['width'] ) ) {
90 return false;
93 if ( !isset( $params['page'] ) ) {
94 $params['page'] = 1;
95 } else {
96 if ( $params['page'] > $image->pageCount() ) {
97 $params['page'] = $image->pageCount();
100 if ( $params['page'] < 1 ) {
101 $params['page'] = 1;
105 $srcWidth = $image->getWidth( $params['page'] );
106 $srcHeight = $image->getHeight( $params['page'] );
108 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
109 # Height & width were both set
110 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
111 # Height is the relative smaller dimension, so scale width accordingly
112 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
114 if ( $params['width'] == 0 ) {
115 # Very small image, so we need to rely on client side scaling :(
116 $params['width'] = 1;
119 $params['physicalWidth'] = $params['width'];
120 } else {
121 # Height was crap, unset it so that it will be calculated later
122 unset( $params['height'] );
126 if ( !isset( $params['physicalWidth'] ) ) {
127 # Passed all validations, so set the physicalWidth
128 $params['physicalWidth'] = $params['width'];
131 # Because thumbs are only referred to by width, the height always needs
132 # to be scaled by the width to keep the thumbnail sizes consistent,
133 # even if it was set inside the if block above
134 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
135 $params['physicalWidth'] );
137 # Set the height if it was not validated in the if block higher up
138 if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
139 $params['height'] = $params['physicalHeight'];
142 if ( !$this->validateThumbParams( $params['physicalWidth'],
143 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) {
144 return false;
146 return true;
150 * Validate thumbnail parameters and fill in the correct height
152 * @param $width Integer: specified width (input/output)
153 * @param $height Integer: height (output only)
154 * @param $srcWidth Integer: width of the source image
155 * @param $srcHeight Integer: height of the source image
156 * @param $mimeType
157 * @return bool False to indicate that an error should be returned to the user.
159 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
160 $width = intval( $width );
162 # Sanity check $width
163 if ( $width <= 0 ) {
164 wfDebug( __METHOD__ . ": Invalid destination width: $width\n" );
165 return false;
167 if ( $srcWidth <= 0 ) {
168 wfDebug( __METHOD__ . ": Invalid source width: $srcWidth\n" );
169 return false;
172 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
173 if ( $height == 0 ) {
174 # Force height to be at least 1 pixel
175 $height = 1;
177 return true;
181 * @param $image File
182 * @param $script
183 * @param $params
184 * @return bool|ThumbnailImage
186 function getScriptedTransform( $image, $script, $params ) {
187 if ( !$this->normaliseParams( $image, $params ) ) {
188 return false;
190 $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
192 if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
193 return new ThumbnailImage( $image, $url, false, $params );
197 function getImageSize( $image, $path ) {
198 wfSuppressWarnings();
199 $gis = getimagesize( $path );
200 wfRestoreWarnings();
201 return $gis;
205 * @param $file File
206 * @return string
208 function getShortDesc( $file ) {
209 global $wgLang;
210 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
211 $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
213 return "$widthheight ($nbytes)";
217 * @param $file File
218 * @return string
220 function getLongDesc( $file ) {
221 global $wgLang;
222 $pages = $file->pageCount();
223 $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
224 if ( $pages === false || $pages <= 1 ) {
225 $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
226 $file->getHeight() )->params( $size,
227 $file->getMimeType() )->parse();
228 } else {
229 $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
230 $file->getHeight() )->params( $size,
231 $file->getMimeType() )->numParams( $pages )->parse();
233 return $msg;
237 * @param $file File
238 * @return string
240 function getDimensionsString( $file ) {
241 $pages = $file->pageCount();
242 if ( $pages > 1 ) {
243 return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
244 } else {
245 return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();