3 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
4 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
6 * @param $length String: CSS/SVG length.
7 * @return Integer: length in pixels
9 function wfScaleSVGUnit( $length ) {
10 static $unitLength = array(
17 '' => 1.0, // "User units" pixels by default
18 '%' => 2.0, // Fake it!
21 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
22 $length = floatval( $matches[1] );
24 return round( $length * $unitLength[$unit] );
27 return round( floatval( $length ) );
32 * Compatible with PHP getimagesize()
33 * @todo support gzipped SVGZ
34 * @todo check XML more carefully
35 * @todo sensible defaults
37 * @param $filename String: full name of the file (passed to php fopen()).
40 function wfGetSVGsize( $filename ) {
44 // Read a chunk of the file
45 $f = fopen( $filename, "rt" );
46 if( !$f ) return false;
47 $chunk = fread( $f, 4096 );
50 // Uber-crappy hack! Run through a real XML parser.
52 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
56 if( preg_match( '/(?:^|\s)width\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
57 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
59 if( preg_match( '/(?:^|\s)height\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
60 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
63 return array( $width, $height, 'SVG',
64 "width=\"$width\" height=\"$height\"" );
68 * Determine if an image exists on the 'bad image list'.
70 * The format of MediaWiki:Bad_image_list is as follows:
71 * * Only list items (lines starting with "*") are considered
72 * * The first link on a line must be a link to a bad image
73 * * Any subsequent links on the same line are considered to be exceptions,
74 * i.e. articles where the image may occur inline.
76 * @param $name string the image name to check
77 * @param $contextTitle Title: the page on which the image occurs, if known
80 function wfIsBadImage( $name, $contextTitle = false ) {
81 static $badImages = false;
82 wfProfileIn( __METHOD__
);
84 # Run the extension hook
86 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
87 wfProfileOut( __METHOD__
);
94 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
95 foreach( $lines as $line ) {
97 if ( substr( $line, 0, 1 ) !== '*' ) {
103 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
107 $exceptions = array();
109 foreach ( $m[1] as $i => $titleText ) {
110 $title = Title
::newFromText( $titleText );
111 if ( !is_null( $title ) ) {
113 $imageDBkey = $title->getDBkey();
115 $exceptions[$title->getPrefixedDBkey()] = true;
120 if ( $imageDBkey !== false ) {
121 $badImages[$imageDBkey] = $exceptions;
126 $contextKey = $contextTitle ?
$contextTitle->getPrefixedDBkey() : false;
127 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
128 wfProfileOut( __METHOD__
);
133 * Calculate the largest thumbnail width for a given original file size
134 * such that the thumbnail's height is at most $maxHeight.
135 * @param $boxWidth Integer Width of the thumbnail box.
136 * @param $boxHeight Integer Height of the thumbnail box.
137 * @param $maxHeight Integer Maximum height expected for the thumbnail.
140 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
141 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
142 $roundedUp = ceil( $idealWidth );
143 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
144 return floor( $idealWidth );