3 * Global functions related to images
9 * Return a rounded pixel equivalent for a labeled CSS/SVG length.
10 * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
12 * @param $length String: CSS/SVG length.
13 * @param $viewportSize: Float optional scale for percentage units...
14 * @return float: length in pixels
16 function wfScaleSVGUnit( $length, $viewportSize=512 ) {
17 static $unitLength = array(
24 'em' => 16.0, // fake it?
25 'ex' => 12.0, // fake it?
26 '' => 1.0, // "User units" pixels by default
29 if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
30 $length = floatval( $matches[1] );
33 return $length * 0.01 * $viewportSize;
35 return $length * $unitLength[$unit];
39 return floatval( $length );
44 const DEFAULT_WIDTH
= 512;
45 const DEFAULT_HEIGHT
= 512;
47 var $width = self
::DEFAULT_WIDTH
;
48 var $height = self
::DEFAULT_HEIGHT
;
49 function filter( $name, $attribs ) {
51 $defaultWidth = self
::DEFAULT_WIDTH
;
52 $defaultHeight = self
::DEFAULT_HEIGHT
;
57 if( isset( $attribs['viewBox'] ) ) {
58 // min-x min-y width height
59 $viewBox = preg_split( '/\s+/', trim( $attribs['viewBox'] ) );
60 if( count( $viewBox ) == 4 ) {
61 $viewWidth = wfScaleSVGUnit( $viewBox[2] );
62 $viewHeight = wfScaleSVGUnit( $viewBox[3] );
63 if( $viewWidth > 0 && $viewHeight > 0 ) {
64 $aspect = $viewWidth / $viewHeight;
65 $defaultHeight = $defaultWidth / $aspect;
69 if( isset( $attribs['width'] ) ) {
70 $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
72 if( isset( $attribs['height'] ) ) {
73 $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
76 if( !isset( $width ) && !isset( $height ) ) {
77 $width = $defaultWidth;
78 $height = $width / $aspect;
79 } elseif( isset( $width ) && !isset( $height ) ) {
80 $height = $width / $aspect;
81 } elseif( isset( $height ) && !isset( $width ) ) {
82 $width = $height * $aspect;
85 if( $width > 0 && $height > 0 ) {
86 $this->width
= intval( round( $width ) );
87 $this->height
= intval( round( $height ) );
96 * Compatible with PHP getimagesize()
97 * @todo support gzipped SVGZ
98 * @todo check XML more carefully
99 * @todo sensible defaults
101 * @param $filename String: full name of the file (passed to php fopen()).
104 function wfGetSVGsize( $filename ) {
105 $filter = new XmlSizeFilter();
106 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
107 if( $xml->wellFormed
) {
108 return array( $filter->width
, $filter->height
, 'SVG',
109 "width=\"$filter->width\" height=\"$filter->height\"" );
116 * Determine if an image exists on the 'bad image list'.
118 * The format of MediaWiki:Bad_image_list is as follows:
119 * * Only list items (lines starting with "*") are considered
120 * * The first link on a line must be a link to a bad image
121 * * Any subsequent links on the same line are considered to be exceptions,
122 * i.e. articles where the image may occur inline.
124 * @param $name string the image name to check
125 * @param $contextTitle Title: the page on which the image occurs, if known
128 function wfIsBadImage( $name, $contextTitle = false ) {
129 static $badImages = false;
130 wfProfileIn( __METHOD__
);
133 $redirectTitle = RepoGroup
::singleton()->checkRedirect( Title
::makeTitle( NS_FILE
, $name ) );
134 if( $redirectTitle ) {
135 $name = $redirectTitle->getDbKey();
138 # Run the extension hook
140 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
141 wfProfileOut( __METHOD__
);
147 $badImages = array();
148 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
149 foreach( $lines as $line ) {
151 if ( substr( $line, 0, 1 ) !== '*' ) {
157 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
161 $exceptions = array();
163 foreach ( $m[1] as $i => $titleText ) {
164 $title = Title
::newFromText( $titleText );
165 if ( !is_null( $title ) ) {
167 $imageDBkey = $title->getDBkey();
169 $exceptions[$title->getPrefixedDBkey()] = true;
174 if ( $imageDBkey !== false ) {
175 $badImages[$imageDBkey] = $exceptions;
180 $contextKey = $contextTitle ?
$contextTitle->getPrefixedDBkey() : false;
181 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
182 wfProfileOut( __METHOD__
);
187 * Calculate the largest thumbnail width for a given original file size
188 * such that the thumbnail's height is at most $maxHeight.
189 * @param $boxWidth Integer Width of the thumbnail box.
190 * @param $boxHeight Integer Height of the thumbnail box.
191 * @param $maxHeight Integer Maximum height expected for the thumbnail.
194 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
195 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
196 $roundedUp = ceil( $idealWidth );
197 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
198 return floor( $idealWidth );