4 * Returns the image directory of an image
5 * The result is an absolute path.
7 * This function is called from thumb.php before Setup.php is included
9 * @param $fname String: file name of the image file.
12 function wfImageDir( $fname ) {
13 global $wgUploadDirectory, $wgHashedUploadDirectory;
15 if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
17 $hash = md5( $fname );
18 $dest = $wgUploadDirectory . '/' . $hash{0} . '/' . substr( $hash, 0, 2 );
24 * Returns the image directory of an image's thubnail
25 * The result is an absolute path.
27 * This function is called from thumb.php before Setup.php is included
29 * @param $fname String: file name of the original image file
30 * @param $shared Boolean: (optional) use the shared upload directory (default: 'false').
33 function wfImageThumbDir( $fname, $shared = false ) {
34 $base = wfImageArchiveDir( $fname, 'thumb', $shared );
35 if ( Image
::isHashed( $shared ) ) {
36 $dir = "$base/$fname";
45 * Old thumbnail directory, kept for conversion
47 function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
48 return wfImageArchiveDir( $thumbName, $subdir, $shared );
52 * Returns the image directory of an image's old version
53 * The result is an absolute path.
55 * This function is called from thumb.php before Setup.php is included
57 * @param $fname String: file name of the thumbnail file, including file size prefix.
58 * @param $subdir String: subdirectory of the image upload directory that should be used for storing the old version. Default is 'archive'.
59 * @param $shared Boolean use the shared upload directory (only relevant for other functions which call this one). Default is 'false'.
62 function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) {
63 global $wgUploadDirectory, $wgHashedUploadDirectory;
64 global $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
65 $dir = $shared ?
$wgSharedUploadDirectory : $wgUploadDirectory;
66 $hashdir = $shared ?
$wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
67 if (!$hashdir) { return $dir.'/'.$subdir; }
68 $hash = md5( $fname );
70 return $dir.'/'.$subdir.'/'.$hash[0].'/'.substr( $hash, 0, 2 );
75 * Return the hash path component of an image path (URL or filesystem),
76 * e.g. "/3/3c/", or just "/" if hashing is not used.
78 * @param $dbkey The filesystem / database name of the file
79 * @param $fromSharedDirectory Use the shared file repository? It may
80 * use different hash settings from the local one.
81 * TODO:@param $fromInstantCommons Use the InstantCommons repository? - this is calculated by the IC Api
83 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false, $fromInstantCommons = false ) {
84 if($fromInstantCommons)
85 {//we will display a local image that indicates that this file is being downloaded
88 elseif( Image
::isHashed( $fromSharedDirectory ) ) {
90 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
97 * Returns the image URL of an image's old version
99 * @param $name String: file name of the image file
100 * @param $subdir String: (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive'
103 function wfImageArchiveUrl( $name, $subdir='archive' ) {
104 global $wgUploadPath, $wgHashedUploadDirectory;
106 if ($wgHashedUploadDirectory) {
107 $hash = md5( substr( $name, 15) );
108 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
109 substr( $hash, 0, 2 ) . '/'.$name;
111 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
113 return wfUrlencode($url);
117 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
118 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
120 * @param $length String: CSS/SVG length.
121 * @return Integer: length in pixels
123 function wfScaleSVGUnit( $length ) {
124 static $unitLength = array(
131 '' => 1.0, // "User units" pixels by default
132 '%' => 2.0, // Fake it!
135 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
136 $length = floatval( $matches[1] );
138 return round( $length * $unitLength[$unit] );
141 return round( floatval( $length ) );
146 * Compatible with PHP getimagesize()
147 * @todo support gzipped SVGZ
148 * @todo check XML more carefully
149 * @todo sensible defaults
151 * @param $filename String: full name of the file (passed to php fopen()).
154 function wfGetSVGsize( $filename ) {
158 // Read a chunk of the file
159 $f = fopen( $filename, "rt" );
160 if( !$f ) return false;
161 $chunk = fread( $f, 4096 );
164 // Uber-crappy hack! Run through a real XML parser.
166 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
170 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
171 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
173 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
174 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
177 return array( $width, $height, 'SVG',
178 "width=\"$width\" height=\"$height\"" );
182 * Determine if an image exists on the 'bad image list'.
184 * The format of MediaWiki:Bad_image_list is as follows:
185 * * Only list items (lines starting with "*") are considered
186 * * The first link on a line must be a link to a bad image
187 * * Any subsequent links on the same line are considered to be exceptions,
188 * i.e. articles where the image may occur inline.
190 * @param string $name the image name to check
191 * @param Title $contextTitle The page on which the image occurs, if known
194 function wfIsBadImage( $name, $contextTitle = false ) {
195 static $badImages = false;
196 wfProfileIn( __METHOD__
);
198 # Run the extension hook
200 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
201 wfProfileOut( __METHOD__
);
207 $badImages = array();
208 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
209 foreach( $lines as $line ) {
211 if ( substr( $line, 0, 1 ) !== '*' ) {
217 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
221 $exceptions = array();
223 foreach ( $m[1] as $i => $titleText ) {
224 $title = Title
::newFromText( $titleText );
225 if ( !is_null( $title ) ) {
227 $imageDBkey = $title->getDBkey();
229 $exceptions[$title->getPrefixedDBkey()] = true;
234 if ( $imageDBkey !== false ) {
235 $badImages[$imageDBkey] = $exceptions;
240 $contextKey = $contextTitle ?
$contextTitle->getPrefixedDBkey() : false;
241 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
242 wfProfileOut( __METHOD__
);
247 * Calculate the largest thumbnail width for a given original file size
248 * such that the thumbnail's height is at most $maxHeight.
249 * @param $boxWidth Integer Width of the thumbnail box.
250 * @param $boxHeight Integer Height of the thumbnail box.
251 * @param $maxHeight Integer Maximum height expected for the thumbnail.
254 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
255 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
256 $roundedUp = ceil( $idealWidth );
257 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
258 return floor( $idealWidth );