narf
[lhc/web/wiklou.git] / includes / ImageFunctions.php
1 <?php
2
3 /**
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.
7 *
8 * This function is called from thumb.php before Setup.php is included
9 *
10 * @param $fname String: file name of the image file.
11 * @public
12 */
13 function wfImageDir( $fname ) {
14 global $wgUploadDirectory, $wgHashedUploadDirectory;
15
16 if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
17
18 $hash = md5( $fname );
19 $oldumask = umask(0);
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 ); }
24
25 umask( $oldumask );
26 return $dest;
27 }
28
29 /**
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.
33 *
34 * This function is called from thumb.php before Setup.php is included
35 *
36 * @param $fname String: file name of the original image file
37 * @param $shared Boolean: (optional) use the shared upload directory (default: 'false').
38 * @public
39 */
40 function wfImageThumbDir( $fname, $shared = false ) {
41 $base = wfImageArchiveDir( $fname, 'thumb', $shared );
42 if ( Image::isHashed( $shared ) ) {
43 $dir = "$base/$fname";
44
45 if ( !is_dir( $base ) ) {
46 $oldumask = umask(0);
47 @mkdir( $base, 0777 );
48 umask( $oldumask );
49 }
50
51 if ( ! is_dir( $dir ) ) {
52 if ( is_file( $dir ) ) {
53 // Old thumbnail in the way of directory creation, kill it
54 unlink( $dir );
55 }
56 $oldumask = umask(0);
57 @mkdir( $dir, 0777 );
58 umask( $oldumask );
59 }
60 } else {
61 $dir = $base;
62 }
63
64 return $dir;
65 }
66
67 /**
68 * Old thumbnail directory, kept for conversion
69 */
70 function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
71 return wfImageArchiveDir( $thumbName, $subdir, $shared );
72 }
73
74 /**
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.
78 *
79 * This function is called from thumb.php before Setup.php is included
80 *
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'.
84 * @public
85 */
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 );
93 $oldumask = umask(0);
94
95 # Suppress warning messages here; if the file itself can't
96 # be written we'll worry about it then.
97 wfSuppressWarnings();
98
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 ); }
105
106 wfRestoreWarnings();
107 umask( $oldumask );
108 return $archive;
109 }
110
111
112 /*
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.
115 *
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.
119 */
120 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
121 if( Image::isHashed( $fromSharedDirectory ) ) {
122 $hash = md5($dbkey);
123 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
124 } else {
125 return '/';
126 }
127 }
128
129 /**
130 * Returns the image URL of an image's old version
131 *
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'
134 * @public
135 */
136 function wfImageArchiveUrl( $name, $subdir='archive' ) {
137 global $wgUploadPath, $wgHashedUploadDirectory;
138
139 if ($wgHashedUploadDirectory) {
140 $hash = md5( substr( $name, 15) );
141 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
142 substr( $hash, 0, 2 ) . '/'.$name;
143 } else {
144 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
145 }
146 return wfUrlencode($url);
147 }
148
149 /**
150 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
151 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
152 *
153 * @param $length String: CSS/SVG length.
154 * @return Integer: length in pixels
155 */
156 function wfScaleSVGUnit( $length ) {
157 static $unitLength = array(
158 'px' => 1.0,
159 'pt' => 1.25,
160 'pc' => 15.0,
161 'mm' => 3.543307,
162 'cm' => 35.43307,
163 'in' => 90.0,
164 '' => 1.0, // "User units" pixels by default
165 '%' => 2.0, // Fake it!
166 );
167 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
168 $length = floatval( $matches[1] );
169 $unit = $matches[2];
170 return round( $length * $unitLength[$unit] );
171 } else {
172 // Assume pixels
173 return round( floatval( $length ) );
174 }
175 }
176
177 /**
178 * Compatible with PHP getimagesize()
179 * @todo support gzipped SVGZ
180 * @todo check XML more carefully
181 * @todo sensible defaults
182 *
183 * @param $filename String: full name of the file (passed to php fopen()).
184 * @return array
185 */
186 function wfGetSVGsize( $filename ) {
187 $width = 256;
188 $height = 256;
189
190 // Read a chunk of the file
191 $f = fopen( $filename, "rt" );
192 if( !$f ) return false;
193 $chunk = fread( $f, 4096 );
194 fclose( $f );
195
196 // Uber-crappy hack! Run through a real XML parser.
197 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
198 return false;
199 }
200 $tag = $matches[1];
201 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
202 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
203 }
204 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
205 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
206 }
207
208 return array( $width, $height, 'SVG',
209 "width=\"$width\" height=\"$height\"" );
210 }
211
212 /**
213 * Determine if an image exists on the 'bad image list'.
214 *
215 * @param $name String: the image name to check
216 * @return bool
217 */
218 function wfIsBadImage( $name ) {
219 static $titleList = false;
220 wfProfileIn( __METHOD__ );
221 $bad = false;
222 if( wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
223 if( !$titleList ) {
224 # Build the list now
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;
232 }
233 }
234 }
235 wfProfileOut( __METHOD__ );
236 return array_key_exists( $name, $titleList );
237 } else {
238 wfProfileOut( __METHOD__ );
239 return $bad;
240 }
241 }
242
243 /**
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.
249 * @return Integer.
250 */
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 );
256 else
257 return $roundedUp;
258 }
259
260
261 ?>