4 * Returns the image directory of an image
5 * If the directory does not exist, it is created.
6 * The result is an absolute path.
8 * This function is called from thumb.php before Setup.php is included
10 * @param $fname String: file name of the image file.
13 function wfImageDir( $fname ) {
14 global $wgUploadDirectory, $wgHashedUploadDirectory;
16 if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
18 $hash = md5( $fname );
20 $dest = $wgUploadDirectory . '/' . $hash{0};
21 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
22 $dest .= '/' . substr( $hash, 0, 2 );
23 if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
30 * Returns the image directory of an image's thubnail
31 * If the directory does not exist, it is created.
32 * The result is an absolute path.
34 * This function is called from thumb.php before Setup.php is included
36 * @param $fname String: file name of the original image file
37 * @param $shared Boolean: (optional) use the shared upload directory (default: 'false').
40 function wfImageThumbDir( $fname, $shared = false ) {
41 $base = wfImageArchiveDir( $fname, 'thumb', $shared );
42 if ( Image
::isHashed( $shared ) ) {
43 $dir = "$base/$fname";
45 if ( !is_dir( $base ) ) {
47 @mkdir
( $base, 0777 );
51 if ( ! is_dir( $dir ) ) {
52 if ( is_file( $dir ) ) {
53 // Old thumbnail in the way of directory creation, kill it
68 * Old thumbnail directory, kept for conversion
70 function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
71 return wfImageArchiveDir( $thumbName, $subdir, $shared );
75 * Returns the image directory of an image's old version
76 * If the directory does not exist, it is created.
77 * The result is an absolute path.
79 * This function is called from thumb.php before Setup.php is included
81 * @param $fname String: file name of the thumbnail file, including file size prefix.
82 * @param $subdir String: subdirectory of the image upload directory that should be used for storing the old version. Default is 'archive'.
83 * @param $shared Boolean use the shared upload directory (only relevant for other functions which call this one). Default is 'false'.
86 function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) {
87 global $wgUploadDirectory, $wgHashedUploadDirectory;
88 global $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
89 $dir = $shared ?
$wgSharedUploadDirectory : $wgUploadDirectory;
90 $hashdir = $shared ?
$wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
91 if (!$hashdir) { return $dir.'/'.$subdir; }
92 $hash = md5( $fname );
95 # Suppress warning messages here; if the file itself can't
96 # be written we'll worry about it then.
99 $archive = $dir.'/'.$subdir;
100 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
101 $archive .= '/' . $hash{0};
102 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
103 $archive .= '/' . substr( $hash, 0, 2 );
104 if ( ! is_dir( $archive ) ) { mkdir( $archive, 0777 ); }
113 * Return the hash path component of an image path (URL or filesystem),
114 * e.g. "/3/3c/", or just "/" if hashing is not used.
116 * @param $dbkey The filesystem / database name of the file
117 * @param $fromSharedDirectory Use the shared file repository? It may
118 * use different hash settings from the local one.
120 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
121 if( Image
::isHashed( $fromSharedDirectory ) ) {
123 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
130 * Returns the image URL of an image's old version
132 * @param $name String: file name of the image file
133 * @param $subdir String: (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive'
136 function wfImageArchiveUrl( $name, $subdir='archive' ) {
137 global $wgUploadPath, $wgHashedUploadDirectory;
139 if ($wgHashedUploadDirectory) {
140 $hash = md5( substr( $name, 15) );
141 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
142 substr( $hash, 0, 2 ) . '/'.$name;
144 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
146 return wfUrlencode($url);
150 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
151 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
153 * @param $length String: CSS/SVG length.
154 * @return Integer: length in pixels
156 function wfScaleSVGUnit( $length ) {
157 static $unitLength = array(
164 '' => 1.0, // "User units" pixels by default
165 '%' => 2.0, // Fake it!
167 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
168 $length = floatval( $matches[1] );
170 return round( $length * $unitLength[$unit] );
173 return round( floatval( $length ) );
178 * Compatible with PHP getimagesize()
179 * @todo support gzipped SVGZ
180 * @todo check XML more carefully
181 * @todo sensible defaults
183 * @param $filename String: full name of the file (passed to php fopen()).
186 function wfGetSVGsize( $filename ) {
190 // Read a chunk of the file
191 $f = fopen( $filename, "rt" );
192 if( !$f ) return false;
193 $chunk = fread( $f, 4096 );
196 // Uber-crappy hack! Run through a real XML parser.
197 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
201 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
202 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
204 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
205 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
208 return array( $width, $height, 'SVG',
209 "width=\"$width\" height=\"$height\"" );
213 * Determine if an image exists on the 'bad image list'.
215 * @param $name String: the image name to check
218 function wfIsBadImage( $name ) {
219 static $titleList = false;
220 wfProfileIn( __METHOD__
);
222 if( wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
225 $titleList = array();
226 $lines = explode( "\n", wfMsgForContent( 'bad_image_list' ) );
227 foreach( $lines as $line ) {
228 if( preg_match( '/^\*\s*\[\[:?(.*?)\]\]/i', $line, $matches ) ) {
229 $title = Title
::newFromText( $matches[1] );
230 if( is_object( $title ) && $title->getNamespace() == NS_IMAGE
)
231 $titleList[ $title->getDBkey() ] = true;
235 wfProfileOut( __METHOD__
);
236 return array_key_exists( $name, $titleList );
238 wfProfileOut( __METHOD__
);
244 * Calculate the largest thumbnail width for a given original file size
245 * such that the thumbnail's height is at most $maxHeight.
246 * @param $boxWidth Integer Width of the thumbnail box.
247 * @param $boxHeight Integer Height of the thumbnail box.
248 * @param $maxHeight Integer Maximum height expected for the thumbnail.
251 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
252 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
253 $roundedUp = ceil( $idealWidth );
254 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
255 return floor( $idealWidth );