4 * PHP script to stream out an image thumbnail.
5 * If the file exists, we make do with abridged MediaWiki initialisation.
7 define( 'MW_NO_SETUP', 1 );
8 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
9 require_once( './includes/WebStart.php' );
10 wfProfileIn( 'thumb.php' );
11 wfProfileIn( 'thumb.php-start' );
12 require_once( "$IP/includes/GlobalFunctions.php" );
13 require_once( "$IP/includes/ImageFunctions.php" );
15 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
17 require_once( "$IP/includes/StreamFile.php" );
18 require_once( "$IP/includes/AutoLoader.php" );
20 // Get input parameters
21 if ( get_magic_quotes_gpc() ) {
22 $params = array_map( 'stripslashes', $_REQUEST );
27 $fileName = isset( $params['f'] ) ?
$params['f'] : '';
28 unset( $params['f'] );
30 // Backwards compatibility parameters
31 if ( isset( $params['w'] ) ) {
32 $params['width'] = $params['w'];
33 unset( $params['w'] );
35 if ( isset( $params['p'] ) ) {
36 $params['page'] = $params['p'];
38 unset( $params['r'] );
40 // Some basic input validation
41 $fileName = strtr( $fileName, '\\/', '__' );
43 // Work out paths, carefully avoiding constructing an Image object because that won't work yet
44 $handler = thumbGetHandler( $fileName );
46 $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
47 $thumbName = $handler->makeParamString( $params ) . "-$fileName";
48 $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
50 if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
51 wfStreamFile( $thumbPath );
52 // Can't log profiling data with no Setup.php
57 // OK, no valid thumbnail, time to get out the heavy machinery
58 wfProfileOut( 'thumb.php-start' );
59 require_once( './includes/Setup.php' );
60 wfProfileIn( 'thumb.php-render' );
62 $img = Image
::newFromName( $fileName );
65 $thumb = $img->transform( $params, Image
::RENDER_NOW
);
69 } catch( Exception
$ex ) {
70 // Tried to select a page on a non-paged file?
74 if ( $thumb && $thumb->getPath() && file_exists( $thumb->getPath() ) ) {
75 wfStreamFile( $thumb->getPath() );
77 header( 'Cache-Control: no-cache' );
78 header( 'Content-Type: text/html; charset=utf-8' );
79 header( 'HTTP/1.1 500 Internal server error' );
81 $msg = wfMsgHtml( 'thumbnail_error', 'Image::transform() returned false' );
82 } elseif ( $thumb->isError() ) {
83 $msg = $thumb->getHtmlMsg();
84 } elseif ( !$thumb->getPath() ) {
85 $msg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
87 $msg = wfMsgHtml( 'thumbnail_error', 'Output file missing' );
90 <html><head><title>Error generating thumbnail</title></head>
92 <h1>Error generating thumbnail</h1>
101 $badtitle = wfMsg( 'badtitle' );
102 $badtitletext = wfMsg( 'badtitletext' );
103 header( 'Cache-Control: no-cache' );
104 header( 'Content-Type: text/html; charset=utf-8' );
105 header( 'HTTP/1.1 500 Internal server error' );
107 <title>$badtitle</title>
115 wfProfileOut( 'thumb.php-render' );
116 wfProfileOut( 'thumb.php' );
117 wfLogProfilingData();
119 //--------------------------------------------------------------------------
121 function thumbGetHandler( $fileName ) {
123 $magic = MimeMagic
::singleton();
124 $extPos = strrpos( $fileName, '.' );
125 if ( $extPos === false ) {
128 $mime = $magic->guessTypesForExtension( substr( $fileName, $extPos +
1 ) );
129 return MediaHandler
::getHandler( $mime );