MediaWiki  REL1_20
ImageHandler.php
Go to the documentation of this file.
00001 <?php
00029 abstract class ImageHandler extends MediaHandler {
00030 
00035         function canRender( $file ) {
00036                 return ( $file->getWidth() && $file->getHeight() );
00037         }
00038 
00039         function getParamMap() {
00040                 return array( 'img_width' => 'width' );
00041         }
00042 
00043         function validateParam( $name, $value ) {
00044                 if ( in_array( $name, array( 'width', 'height' ) ) ) {
00045                         if ( $value <= 0 ) {
00046                                 return false;
00047                         } else {
00048                                 return true;
00049                         }
00050                 } else {
00051                         return false;
00052                 }
00053         }
00054 
00055         function makeParamString( $params ) {
00056                 if ( isset( $params['physicalWidth'] ) ) {
00057                         $width = $params['physicalWidth'];
00058                 } elseif ( isset( $params['width'] ) ) {
00059                         $width = $params['width'];
00060                 } else {
00061                         throw new MWException( 'No width specified to '.__METHOD__ );
00062                 }
00063                 # Removed for ProofreadPage
00064                 #$width = intval( $width );
00065                 return "{$width}px";
00066         }
00067 
00068         function parseParamString( $str ) {
00069                 $m = false;
00070                 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
00071                         return array( 'width' => $m[1] );
00072                 } else {
00073                         return false;
00074                 }
00075         }
00076 
00077         function getScriptParams( $params ) {
00078                 return array( 'width' => $params['width'] );
00079         }
00080 
00086         function normaliseParams( $image, &$params ) {
00087                 $mimeType = $image->getMimeType();
00088 
00089                 if ( !isset( $params['width'] ) ) {
00090                         return false;
00091                 }
00092 
00093                 if ( !isset( $params['page'] ) ) {
00094                         $params['page'] = 1;
00095                 } else  {
00096                         if ( $params['page'] > $image->pageCount() ) {
00097                                 $params['page'] = $image->pageCount();
00098                         }
00099 
00100                         if ( $params['page'] < 1 ) {
00101                                 $params['page'] = 1;
00102                         }
00103                 }
00104 
00105                 $srcWidth = $image->getWidth( $params['page'] );
00106                 $srcHeight = $image->getHeight( $params['page'] );
00107 
00108                 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
00109                         # Height & width were both set
00110                         if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
00111                                 # Height is the relative smaller dimension, so scale width accordingly
00112                                 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
00113 
00114                                 if ( $params['width'] == 0 ) {
00115                                         # Very small image, so we need to rely on client side scaling :(
00116                                         $params['width'] = 1;
00117                                 }
00118 
00119                                 $params['physicalWidth'] = $params['width'];
00120                         } else {
00121                                 # Height was crap, unset it so that it will be calculated later
00122                                 unset( $params['height'] );
00123                         }
00124                 }
00125 
00126                 if ( !isset( $params['physicalWidth'] ) ) {
00127                         # Passed all validations, so set the physicalWidth
00128                         $params['physicalWidth'] = $params['width'];
00129                 }
00130 
00131                 # Because thumbs are only referred to by width, the height always needs
00132                 # to be scaled by the width to keep the thumbnail sizes consistent,
00133                 # even if it was set inside the if block above
00134                 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
00135                         $params['physicalWidth'] );
00136 
00137                 # Set the height if it was not validated in the if block higher up
00138                 if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
00139                         $params['height'] = $params['physicalHeight'];
00140                 }
00141 
00142 
00143                 if ( !$this->validateThumbParams( $params['physicalWidth'],
00144                                 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) {
00145                         return false;
00146                 }
00147                 return true;
00148         }
00149 
00160         function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
00161                 $width = intval( $width );
00162 
00163                 # Sanity check $width
00164                 if( $width <= 0) {
00165                         wfDebug( __METHOD__.": Invalid destination width: $width\n" );
00166                         return false;
00167                 }
00168                 if ( $srcWidth <= 0 ) {
00169                         wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
00170                         return false;
00171                 }
00172 
00173                 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
00174                 if ( $height == 0 ) {
00175                         # Force height to be at least 1 pixel
00176                         $height = 1;
00177                 }
00178                 return true;
00179         }
00180 
00187         function getScriptedTransform( $image, $script, $params ) {
00188                 if ( !$this->normaliseParams( $image, $params ) ) {
00189                         return false;
00190                 }
00191                 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
00192 
00193                 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
00194                         return new ThumbnailImage( $image, $url, false, $params );
00195                 }
00196         }
00197 
00198         function getImageSize( $image, $path ) {
00199                 wfSuppressWarnings();
00200                 $gis = getimagesize( $path );
00201                 wfRestoreWarnings();
00202                 return $gis;
00203         }
00204 
00209         function getShortDesc( $file ) {
00210                 global $wgLang;
00211                 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00212                 $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
00213 
00214                 return "$widthheight ($nbytes)";
00215         }
00216 
00221         function getLongDesc( $file ) {
00222                 global $wgLang;
00223                 $pages = $file->pageCount();
00224                 $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00225                 if ( $pages === false || $pages <= 1 ) {
00226                         $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
00227                                 $file->getHeight() )->params( $size,
00228                                 $file->getMimeType() )->parse();
00229                 } else {
00230                         $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
00231                                 $file->getHeight() )->params( $size,
00232                                 $file->getMimeType() )->numParams( $pages )->parse();
00233                 }
00234                 return $msg;
00235         }
00236 
00241         function getDimensionsString( $file ) {
00242                 $pages = $file->pageCount();
00243                 if ( $pages > 1 ) {
00244                         return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
00245                 } else {
00246                         return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();
00247                 }
00248         }
00249 }