MediaWiki  REL1_24
ImageHandler.php
Go to the documentation of this file.
00001 <?php
00029 abstract class ImageHandler extends MediaHandler {
00034     function canRender( $file ) {
00035         return ( $file->getWidth() && $file->getHeight() );
00036     }
00037 
00038     function getParamMap() {
00039         return array( 'img_width' => 'width' );
00040     }
00041 
00042     function validateParam( $name, $value ) {
00043         if ( in_array( $name, array( 'width', 'height' ) ) ) {
00044             if ( $value <= 0 ) {
00045                 return false;
00046             } else {
00047                 return true;
00048             }
00049         } else {
00050             return false;
00051         }
00052     }
00053 
00054     function makeParamString( $params ) {
00055         if ( isset( $params['physicalWidth'] ) ) {
00056             $width = $params['physicalWidth'];
00057         } elseif ( isset( $params['width'] ) ) {
00058             $width = $params['width'];
00059         } else {
00060             throw new MWException( 'No width specified to ' . __METHOD__ );
00061         }
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         ) {
00146             return false;
00147         }
00148 
00149         return true;
00150     }
00151 
00162     function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
00163         $width = intval( $width );
00164 
00165         # Sanity check $width
00166         if ( $width <= 0 ) {
00167             wfDebug( __METHOD__ . ": Invalid destination width: $width\n" );
00168 
00169             return false;
00170         }
00171         if ( $srcWidth <= 0 ) {
00172             wfDebug( __METHOD__ . ": Invalid source width: $srcWidth\n" );
00173 
00174             return false;
00175         }
00176 
00177         $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
00178         if ( $height == 0 ) {
00179             # Force height to be at least 1 pixel
00180             $height = 1;
00181         }
00182 
00183         return true;
00184     }
00185 
00192     function getScriptedTransform( $image, $script, $params ) {
00193         if ( !$this->normaliseParams( $image, $params ) ) {
00194             return false;
00195         }
00196         $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
00197 
00198         if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
00199             return new ThumbnailImage( $image, $url, false, $params );
00200         }
00201     }
00202 
00203     function getImageSize( $image, $path ) {
00204         wfSuppressWarnings();
00205         $gis = getimagesize( $path );
00206         wfRestoreWarnings();
00207 
00208         return $gis;
00209     }
00210 
00220     function getImageArea( $image ) {
00221         return $image->getWidth() * $image->getHeight();
00222     }
00223 
00228     function getShortDesc( $file ) {
00229         global $wgLang;
00230         $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00231         $widthheight = wfMessage( 'widthheight' )
00232             ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
00233 
00234         return "$widthheight ($nbytes)";
00235     }
00236 
00241     function getLongDesc( $file ) {
00242         global $wgLang;
00243         $pages = $file->pageCount();
00244         $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00245         if ( $pages === false || $pages <= 1 ) {
00246             $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
00247                 $file->getHeight() )->params( $size,
00248                     $file->getMimeType() )->parse();
00249         } else {
00250             $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
00251                 $file->getHeight() )->params( $size,
00252                     $file->getMimeType() )->numParams( $pages )->parse();
00253         }
00254 
00255         return $msg;
00256     }
00257 
00262     function getDimensionsString( $file ) {
00263         $pages = $file->pageCount();
00264         if ( $pages > 1 ) {
00265             return wfMessage( 'widthheightpage' )
00266                 ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
00267         } else {
00268             return wfMessage( 'widthheight' )
00269                 ->numParams( $file->getWidth(), $file->getHeight() )->text();
00270         }
00271     }
00272 
00273     public function sanitizeParamsForBucketing( $params ) {
00274         $params = parent::sanitizeParamsForBucketing( $params );
00275 
00276         // We unset the height parameters in order to let normaliseParams recalculate them
00277         // Otherwise there might be a height discrepancy
00278         if ( isset( $params['height'] ) ) {
00279             unset( $params['height'] );
00280         }
00281 
00282         if ( isset( $params['physicalHeight'] ) ) {
00283             unset( $params['physicalHeight'] );
00284         }
00285 
00286         return $params;
00287     }
00288 }