revert r18517; should have been on branch
[lhc/web/wiklou.git] / includes / ImageFunctions.php
1 <?php
2
3 /**
4 * Returns the image directory of an image
5 * The result is an absolute path.
6 *
7 * This function is called from thumb.php before Setup.php is included
8 *
9 * @param $fname String: file name of the image file.
10 * @public
11 */
12 function wfImageDir( $fname ) {
13 global $wgUploadDirectory, $wgHashedUploadDirectory;
14
15 if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
16
17 $hash = md5( $fname );
18 $dest = $wgUploadDirectory . '/' . $hash{0} . '/' . substr( $hash, 0, 2 );
19
20 return $dest;
21 }
22
23 /**
24 * Returns the image directory of an image's thubnail
25 * The result is an absolute path.
26 *
27 * This function is called from thumb.php before Setup.php is included
28 *
29 * @param $fname String: file name of the original image file
30 * @param $shared Boolean: (optional) use the shared upload directory (default: 'false').
31 * @public
32 */
33 function wfImageThumbDir( $fname, $shared = false ) {
34 $base = wfImageArchiveDir( $fname, 'thumb', $shared );
35 if ( Image::isHashed( $shared ) ) {
36 $dir = "$base/$fname";
37 } else {
38 $dir = $base;
39 }
40
41 return $dir;
42 }
43
44 /**
45 * Old thumbnail directory, kept for conversion
46 */
47 function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
48 return wfImageArchiveDir( $thumbName, $subdir, $shared );
49 }
50
51 /**
52 * Returns the image directory of an image's old version
53 * The result is an absolute path.
54 *
55 * This function is called from thumb.php before Setup.php is included
56 *
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'.
60 * @public
61 */
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 );
69
70 return $dir.'/'.$subdir.'/'.$hash[0].'/'.substr( $hash, 0, 2 );
71 }
72
73
74 /*
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.
77 *
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 */
82 function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
83 if( Image::isHashed( $fromSharedDirectory ) ) {
84 $hash = md5($dbkey);
85 return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
86 } else {
87 return '/';
88 }
89 }
90
91 /**
92 * Returns the image URL of an image's old version
93 *
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'
96 * @public
97 */
98 function wfImageArchiveUrl( $name, $subdir='archive' ) {
99 global $wgUploadPath, $wgHashedUploadDirectory;
100
101 if ($wgHashedUploadDirectory) {
102 $hash = md5( substr( $name, 15) );
103 $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
104 substr( $hash, 0, 2 ) . '/'.$name;
105 } else {
106 $url = $wgUploadPath.'/'.$subdir.'/'.$name;
107 }
108 return wfUrlencode($url);
109 }
110
111 /**
112 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
113 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
114 *
115 * @param $length String: CSS/SVG length.
116 * @return Integer: length in pixels
117 */
118 function wfScaleSVGUnit( $length ) {
119 static $unitLength = array(
120 'px' => 1.0,
121 'pt' => 1.25,
122 'pc' => 15.0,
123 'mm' => 3.543307,
124 'cm' => 35.43307,
125 'in' => 90.0,
126 '' => 1.0, // "User units" pixels by default
127 '%' => 2.0, // Fake it!
128 );
129 $matches = array();
130 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
131 $length = floatval( $matches[1] );
132 $unit = $matches[2];
133 return round( $length * $unitLength[$unit] );
134 } else {
135 // Assume pixels
136 return round( floatval( $length ) );
137 }
138 }
139
140 /**
141 * Compatible with PHP getimagesize()
142 * @todo support gzipped SVGZ
143 * @todo check XML more carefully
144 * @todo sensible defaults
145 *
146 * @param $filename String: full name of the file (passed to php fopen()).
147 * @return array
148 */
149 function wfGetSVGsize( $filename ) {
150 $width = 256;
151 $height = 256;
152
153 // Read a chunk of the file
154 $f = fopen( $filename, "rt" );
155 if( !$f ) return false;
156 $chunk = fread( $f, 4096 );
157 fclose( $f );
158
159 // Uber-crappy hack! Run through a real XML parser.
160 $matches = array();
161 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
162 return false;
163 }
164 $tag = $matches[1];
165 if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
166 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
167 }
168 if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
169 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
170 }
171
172 return array( $width, $height, 'SVG',
173 "width=\"$width\" height=\"$height\"" );
174 }
175
176 /**
177 * Determine if an image exists on the 'bad image list'.
178 *
179 * The format of MediaWiki:Bad_image_list is as follows:
180 * * Only list items (lines starting with "*") are considered
181 * * The first link on a line must be a link to a bad image
182 * * Any subsequent links on the same line are considered to be exceptions,
183 * i.e. articles where the image may occur inline.
184 *
185 * @param string $name the image name to check
186 * @param Title $contextTitle The page on which the image occurs, if known
187 * @return bool
188 */
189 function wfIsBadImage( $name, $contextTitle = false ) {
190 static $badImages = false;
191 wfProfileIn( __METHOD__ );
192
193 # Run the extension hook
194 $bad = false;
195 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
196 wfProfileOut( __METHOD__ );
197 return $bad;
198 }
199
200 if( !$badImages ) {
201 # Build the list now
202 $badImages = array();
203 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
204 foreach( $lines as $line ) {
205 # List items only
206 if ( substr( $line, 0, 1 ) !== '*' ) {
207 continue;
208 }
209
210 # Find all links
211 $m = array();
212 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
213 continue;
214 }
215
216 $exceptions = array();
217 $imageDBkey = false;
218 foreach ( $m[1] as $i => $titleText ) {
219 $title = Title::newFromText( $titleText );
220 if ( !is_null( $title ) ) {
221 if ( $i == 0 ) {
222 $imageDBkey = $title->getDBkey();
223 } else {
224 $exceptions[$title->getPrefixedDBkey()] = true;
225 }
226 }
227 }
228
229 if ( $imageDBkey !== false ) {
230 $badImages[$imageDBkey] = $exceptions;
231 }
232 }
233 }
234
235 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
236 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
237 wfProfileOut( __METHOD__ );
238 return $bad;
239 }
240
241 /**
242 * Calculate the largest thumbnail width for a given original file size
243 * such that the thumbnail's height is at most $maxHeight.
244 * @param $boxWidth Integer Width of the thumbnail box.
245 * @param $boxHeight Integer Height of the thumbnail box.
246 * @param $maxHeight Integer Maximum height expected for the thumbnail.
247 * @return Integer.
248 */
249 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
250 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
251 $roundedUp = ceil( $idealWidth );
252 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
253 return floor( $idealWidth );
254 else
255 return $roundedUp;
256 }
257
258
259 ?>