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.
82 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
83 if( Image
::isHashed( $fromSharedDirectory ) ) {
85 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
92 * Returns the image URL of an image's old version
94 * @param $name String: file name of the image file
95 * @param $subdir String: (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive'
98 function wfImageArchiveUrl( $name, $subdir='archive' ) {
99 global $wgUploadPath, $wgHashedUploadDirectory;
101 if ($wgHashedUploadDirectory) {
102 $hash = md5( substr( $name, 15) );
103 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
104 substr( $hash, 0, 2 ) . '/'.$name;
106 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
108 return wfUrlencode($url);
112 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
113 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
115 * @param $length String: CSS/SVG length.
116 * @return Integer: length in pixels
118 function wfScaleSVGUnit( $length ) {
119 static $unitLength = array(
126 '' => 1.0, // "User units" pixels by default
127 '%' => 2.0, // Fake it!
129 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
130 $length = floatval( $matches[1] );
132 return round( $length * $unitLength[$unit] );
135 return round( floatval( $length ) );
140 * Compatible with PHP getimagesize()
141 * @todo support gzipped SVGZ
142 * @todo check XML more carefully
143 * @todo sensible defaults
145 * @param $filename String: full name of the file (passed to php fopen()).
148 function wfGetSVGsize( $filename ) {
152 // Read a chunk of the file
153 $f = fopen( $filename, "rt" );
154 if( !$f ) return false;
155 $chunk = fread( $f, 4096 );
158 // Uber-crappy hack! Run through a real XML parser.
159 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
163 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
164 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
166 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
167 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
170 return array( $width, $height, 'SVG',
171 "width=\"$width\" height=\"$height\"" );
175 * Determine if an image exists on the 'bad image list'.
177 * @param $name String: the image name to check
180 function wfIsBadImage( $name ) {
181 static $titleList = false;
182 wfProfileIn( __METHOD__
);
184 if( wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
187 $titleList = array();
188 $lines = explode( "\n", wfMsgForContent( 'bad_image_list' ) );
189 foreach( $lines as $line ) {
190 if( preg_match( '/^\*\s*\[\[:?(.*?)\]\]/i', $line, $matches ) ) {
191 $title = Title
::newFromText( $matches[1] );
192 if( is_object( $title ) && $title->getNamespace() == NS_IMAGE
)
193 $titleList[ $title->getDBkey() ] = true;
197 wfProfileOut( __METHOD__
);
198 return array_key_exists( $name, $titleList );
200 wfProfileOut( __METHOD__
);
206 * Calculate the largest thumbnail width for a given original file size
207 * such that the thumbnail's height is at most $maxHeight.
208 * @param $boxWidth Integer Width of the thumbnail box.
209 * @param $boxHeight Integer Height of the thumbnail box.
210 * @param $maxHeight Integer Maximum height expected for the thumbnail.
213 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
214 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
215 $roundedUp = ceil( $idealWidth );
216 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
217 return floor( $idealWidth );