MediaWiki  REL1_22
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             $params['page'] = intval( $params['page'] );
00097             if ( $params['page'] > $image->pageCount() ) {
00098                 $params['page'] = $image->pageCount();
00099             }
00100 
00101             if ( $params['page'] < 1 ) {
00102                 $params['page'] = 1;
00103             }
00104         }
00105 
00106         $srcWidth = $image->getWidth( $params['page'] );
00107         $srcHeight = $image->getHeight( $params['page'] );
00108 
00109         if ( isset( $params['height'] ) && $params['height'] != -1 ) {
00110             # Height & width were both set
00111             if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
00112                 # Height is the relative smaller dimension, so scale width accordingly
00113                 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
00114 
00115                 if ( $params['width'] == 0 ) {
00116                     # Very small image, so we need to rely on client side scaling :(
00117                     $params['width'] = 1;
00118                 }
00119 
00120                 $params['physicalWidth'] = $params['width'];
00121             } else {
00122                 # Height was crap, unset it so that it will be calculated later
00123                 unset( $params['height'] );
00124             }
00125         }
00126 
00127         if ( !isset( $params['physicalWidth'] ) ) {
00128             # Passed all validations, so set the physicalWidth
00129             $params['physicalWidth'] = $params['width'];
00130         }
00131 
00132         # Because thumbs are only referred to by width, the height always needs
00133         # to be scaled by the width to keep the thumbnail sizes consistent,
00134         # even if it was set inside the if block above
00135         $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
00136             $params['physicalWidth'] );
00137 
00138         # Set the height if it was not validated in the if block higher up
00139         if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
00140             $params['height'] = $params['physicalHeight'];
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 = wfAppendQuery( $script, $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     }
00213     function getImageArea( $image ) {
00214         return $image->getWidth() * $image->getHeight();
00215     }
00216 
00217 
00222     function getShortDesc( $file ) {
00223         global $wgLang;
00224         $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00225         $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
00226 
00227         return "$widthheight ($nbytes)";
00228     }
00229 
00234     function getLongDesc( $file ) {
00235         global $wgLang;
00236         $pages = $file->pageCount();
00237         $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00238         if ( $pages === false || $pages <= 1 ) {
00239             $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
00240                 $file->getHeight() )->params( $size,
00241                 $file->getMimeType() )->parse();
00242         } else {
00243             $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
00244                 $file->getHeight() )->params( $size,
00245                 $file->getMimeType() )->numParams( $pages )->parse();
00246         }
00247         return $msg;
00248     }
00249 
00254     function getDimensionsString( $file ) {
00255         $pages = $file->pageCount();
00256         if ( $pages > 1 ) {
00257             return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
00258         } else {
00259             return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();
00260         }
00261     }
00262 }