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
25 * Media handler abstract base class for images
29 abstract class ImageHandler
extends MediaHandler
{
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' ) ) ) {
55 function makeParamString( $params ) {
56 if ( isset( $params['physicalWidth'] ) ) {
57 $width = $params['physicalWidth'];
58 } elseif ( isset( $params['width'] ) ) {
59 $width = $params['width'];
61 throw new MWException( 'No width specified to ' . __METHOD__
);
63 # Removed for ProofreadPage
64 #$width = intval( $width );
68 function parseParamString( $str ) {
70 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
71 return array( 'width' => $m[1] );
77 function getScriptParams( $params ) {
78 return array( 'width' => $params['width'] );
86 function normaliseParams( $image, &$params ) {
87 $mimeType = $image->getMimeType();
89 if ( !isset( $params['width'] ) ) {
93 if ( !isset( $params['page'] ) ) {
96 if ( $params['page'] > $image->pageCount() ) {
97 $params['page'] = $image->pageCount();
100 if ( $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'];
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 ) ) {
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
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
164 wfDebug( __METHOD__
. ": Invalid destination width: $width\n" );
167 if ( $srcWidth <= 0 ) {
168 wfDebug( __METHOD__
. ": Invalid source width: $srcWidth\n" );
172 $height = File
::scaleHeight( $srcWidth, $srcHeight, $width );
173 if ( $height == 0 ) {
174 # Force height to be at least 1 pixel
184 * @return bool|ThumbnailImage
186 function getScriptedTransform( $image, $script, $params ) {
187 if ( !$this->normaliseParams( $image, $params ) ) {
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 );
208 function getShortDesc( $file ) {
210 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
211 $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
213 return "$widthheight ($nbytes)";
220 function getLongDesc( $file ) {
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();
229 $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
230 $file->getHeight() )->params( $size,
231 $file->getMimeType() )->numParams( $pages )->parse();
240 function getDimensionsString( $file ) {
241 $pages = $file->pageCount();
243 return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
245 return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();