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 * @param $viewportSize: Float optional scale for percentage units...
8 * @return float: length in pixels
10 function wfScaleSVGUnit( $length, $viewportSize=512 ) {
11 static $unitLength = array(
18 'em' => 16.0, // fake it?
19 'ex' => 12.0, // fake it?
20 '' => 1.0, // "User units" pixels by default
23 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
24 $length = floatval( $matches[1] );
27 return $length * 0.01 * $viewportSize;
29 return $length * $unitLength[$unit];
33 return floatval( $length );
38 const DEFAULT_WIDTH
= 512;
39 const DEFAULT_HEIGHT
= 512;
41 var $width = self
::DEFAULT_WIDTH
;
42 var $height = self
::DEFAULT_HEIGHT
;
43 function filter( $name, $attribs ) {
45 $defaultWidth = self
::DEFAULT_WIDTH
;
46 $defaultHeight = self
::DEFAULT_HEIGHT
;
51 if( isset( $attribs['viewBox'] ) ) {
52 // min-x min-y width height
53 $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
54 if( count( $viewBox ) == 4 ) {
55 $viewWidth = wfScaleSVGUnit( $viewBox[2] );
56 $viewHeight = wfScaleSVGUnit( $viewBox[3] );
57 if( $viewWidth > 0 && $viewHeight > 0 ) {
58 $aspect = $viewWidth / $viewHeight;
59 $defaultHeight = $defaultWidth / $aspect;
63 if( isset( $attribs['width'] ) ) {
64 $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
66 if( isset( $attribs['height'] ) ) {
67 $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
70 if( !isset( $width ) && !isset( $height ) ) {
71 $width = $defaultWidth;
72 $height = $width / $aspect;
73 } elseif( isset( $width ) && !isset( $height ) ) {
74 $height = $width / $aspect;
75 } elseif( isset( $height ) && !isset( $width ) ) {
76 $width = $height * $aspect;
79 if( $width > 0 && $height > 0 ) {
80 $this->width
= intval( round( $width ) );
81 $this->height
= intval( round( $height ) );
90 * Compatible with PHP getimagesize()
91 * @todo support gzipped SVGZ
92 * @todo check XML more carefully
93 * @todo sensible defaults
95 * @param $filename String: full name of the file (passed to php fopen()).
98 function wfGetSVGsize( $filename ) {
99 $filter = new XmlSizeFilter();
100 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
101 if( $xml->wellFormed
) {
102 return array( $filter->width
, $filter->height
, 'SVG',
103 "width=\"$filter->width\" height=\"$filter->height\"" );
110 * Determine if an image exists on the 'bad image list'.
112 * The format of MediaWiki:Bad_image_list is as follows:
113 * * Only list items (lines starting with "*") are considered
114 * * The first link on a line must be a link to a bad image
115 * * Any subsequent links on the same line are considered to be exceptions,
116 * i.e. articles where the image may occur inline.
118 * @param $name string the image name to check
119 * @param $contextTitle Title: the page on which the image occurs, if known
122 function wfIsBadImage( $name, $contextTitle = false ) {
123 static $badImages = false;
124 wfProfileIn( __METHOD__
);
127 $redirectTitle = RepoGroup
::singleton()->checkRedirect( Title
::makeTitle( NS_FILE
, $name ) );
128 if( $redirectTitle ) {
129 $name = $redirectTitle->getDbKey();
132 # Run the extension hook
134 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
135 wfProfileOut( __METHOD__
);
141 $badImages = array();
142 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
143 foreach( $lines as $line ) {
145 if ( substr( $line, 0, 1 ) !== '*' ) {
151 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
155 $exceptions = array();
157 foreach ( $m[1] as $i => $titleText ) {
158 $title = Title
::newFromText( $titleText );
159 if ( !is_null( $title ) ) {
161 $imageDBkey = $title->getDBkey();
163 $exceptions[$title->getPrefixedDBkey()] = true;
168 if ( $imageDBkey !== false ) {
169 $badImages[$imageDBkey] = $exceptions;
174 $contextKey = $contextTitle ?
$contextTitle->getPrefixedDBkey() : false;
175 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
176 wfProfileOut( __METHOD__
);
181 * Calculate the largest thumbnail width for a given original file size
182 * such that the thumbnail's height is at most $maxHeight.
183 * @param $boxWidth Integer Width of the thumbnail box.
184 * @param $boxHeight Integer Height of the thumbnail box.
185 * @param $maxHeight Integer Maximum height expected for the thumbnail.
188 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
189 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
190 $roundedUp = ceil( $idealWidth );
191 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
192 return floor( $idealWidth );