4 * PHP script to stream out an image thumbnail.
8 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
9 require_once( './includes/WebStart.php' );
11 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
13 require_once( "$IP/includes/StreamFile.php" );
18 //--------------------------------------------------------------------------
20 function wfThumbMain() {
21 wfProfileIn( __METHOD__
);
22 // Get input parameters
23 if ( get_magic_quotes_gpc() ) {
24 $params = array_map( 'stripslashes', $_REQUEST );
29 $fileName = isset( $params['f'] ) ?
$params['f'] : '';
30 unset( $params['f'] );
32 // Backwards compatibility parameters
33 if ( isset( $params['w'] ) ) {
34 $params['width'] = $params['w'];
35 unset( $params['w'] );
37 if ( isset( $params['p'] ) ) {
38 $params['page'] = $params['p'];
40 unset( $params['r'] );
42 // Some basic input validation
43 $fileName = strtr( $fileName, '\\/', '__' );
45 $img = wfLocalFile( $fileName );
47 wfThumbError( 404, wfMsg( 'badtitletext' ) );
50 if ( !$img->exists() ) {
51 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
54 $sourcePath = $img->getPath();
55 if ( $sourcePath === false ) {
56 wfThumbError( 500, 'The source file is not locally accessible.' );
60 // Check IMS against the source file
61 // This means that clients can keep a cached copy even after it has been deleted on the server
62 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
64 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
67 $imsUnix = strtotime( $imsString );
69 $stat = @stat
( $sourcePath );
70 if ( $stat['mtime'] <= $imsUnix ) {
71 header( 'HTTP/1.1 304 Not Modified' );
76 // Stream the file if it exists already
78 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
79 $thumbPath = $img->getThumbPath( $thumbName );
81 if ( is_file( $thumbPath ) ) {
82 wfStreamFile( $thumbPath );
86 } catch ( MWException
$e ) {
87 wfThumbError( 500, $e->getHTML() );
92 $thumb = $img->transform( $params, File
::RENDER_NOW
);
93 } catch( Exception
$ex ) {
94 // Tried to select a page on a non-paged file?
100 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
101 } elseif ( $thumb->isError() ) {
102 $errorMsg = $thumb->getHtmlMsg();
103 } elseif ( !$thumb->getPath() ) {
104 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
105 } elseif ( $thumb->getPath() == $img->getPath() ) {
106 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
107 'is the requested width bigger than the source?' );
109 wfStreamFile( $thumb->getPath() );
111 if ( $errorMsg !== false ) {
112 wfThumbError( 500, $errorMsg );
115 wfProfileOut( __METHOD__
);
118 function wfThumbError( $status, $msg ) {
119 header( 'Cache-Control: no-cache' );
120 header( 'Content-Type: text/html; charset=utf-8' );
121 if ( $status == 404 ) {
122 header( 'HTTP/1.1 404 Not found' );
124 header( 'HTTP/1.1 500 Internal server error' );
127 <html><head><title>Error generating thumbnail</title></head>
129 <h1>Error generating thumbnail</h1>