]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/ImageFunctions.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / ImageFunctions.php
1 <?php
2 /**
3  * Return a rounded pixel equivalent for a labeled CSS/SVG length.
4  * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
5  *
6  * @param $length String: CSS/SVG length.
7  * @param $viewportSize: Float optional scale for percentage units...
8  * @return float: length in pixels
9  */
10 function wfScaleSVGUnit( $length, $viewportSize=512 ) {
11         static $unitLength = array(
12                 'px' => 1.0,
13                 'pt' => 1.25,
14                 'pc' => 15.0,
15                 'mm' => 3.543307,
16                 'cm' => 35.43307,
17                 'in' => 90.0,
18                 'em' => 16.0, // fake it?
19                 'ex' => 12.0, // fake it?
20                 ''   => 1.0, // "User units" pixels by default
21                 );
22         $matches = array();
23         if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) {
24                 $length = floatval( $matches[1] );
25                 $unit = $matches[2];
26                 if( $unit == '%' ) {
27                         return $length * 0.01 * $viewportSize;
28                 } else {
29                         return $length * $unitLength[$unit];
30                 }
31         } else {
32                 // Assume pixels
33                 return floatval( $length );
34         }
35 }
36
37 class XmlSizeFilter {
38         const DEFAULT_WIDTH = 512;
39         const DEFAULT_HEIGHT = 512;
40         var $first = true;
41         var $width = self::DEFAULT_WIDTH;
42         var $height = self::DEFAULT_HEIGHT;
43         function filter( $name, $attribs ) {
44                 if( $this->first ) {
45                         $defaultWidth = self::DEFAULT_WIDTH;
46                         $defaultHeight = self::DEFAULT_HEIGHT;
47                         $aspect = 1.0;
48                         $width = null;
49                         $height = null;
50                         
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;
60                                         }
61                                 }
62                         }
63                         if( isset( $attribs['width'] ) ) {
64                                 $width = wfScaleSVGUnit( $attribs['width'], $defaultWidth );
65                         }
66                         if( isset( $attribs['height'] ) ) {
67                                 $height = wfScaleSVGUnit( $attribs['height'], $defaultHeight );
68                         }
69                         
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;
77                         }
78                         
79                         if( $width > 0 && $height > 0 ) {
80                                 $this->width = intval( round( $width ) );
81                                 $this->height = intval( round( $height ) );
82                         }
83                         
84                         $this->first = false;
85                 }
86         }
87 }
88
89 /**
90  * Compatible with PHP getimagesize()
91  * @todo support gzipped SVGZ
92  * @todo check XML more carefully
93  * @todo sensible defaults
94  *
95  * @param $filename String: full name of the file (passed to php fopen()).
96  * @return array
97  */
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\"" );
104         }
105         
106         return false;
107 }
108
109 /**
110  * Determine if an image exists on the 'bad image list'.
111  *
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.
117  *
118  * @param $name string the image name to check
119  * @param $contextTitle Title: the page on which the image occurs, if known
120  * @return bool
121  */
122 function wfIsBadImage( $name, $contextTitle = false ) {
123         static $badImages = false;
124         wfProfileIn( __METHOD__ );
125
126         # Run the extension hook
127         $bad = false;
128         if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
129                 wfProfileOut( __METHOD__ );
130                 return $bad;
131         }
132
133         if( !$badImages ) {
134                 # Build the list now
135                 $badImages = array();
136                 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
137                 foreach( $lines as $line ) {
138                         # List items only
139                         if ( substr( $line, 0, 1 ) !== '*' ) {
140                                 continue;
141                         }
142
143                         # Find all links
144                         $m = array();
145                         if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
146                                 continue;
147                         }
148
149                         $exceptions = array();
150                         $imageDBkey = false;
151                         foreach ( $m[1] as $i => $titleText ) {
152                                 $title = Title::newFromText( $titleText );
153                                 if ( !is_null( $title ) ) {
154                                         if ( $i == 0 ) {
155                                                 $imageDBkey = $title->getDBkey();
156                                         } else {
157                                                 $exceptions[$title->getPrefixedDBkey()] = true;
158                                         }
159                                 }
160                         }
161
162                         if ( $imageDBkey !== false ) {
163                                 $badImages[$imageDBkey] = $exceptions;
164                         }
165                 }
166         }
167
168         $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
169         $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
170         wfProfileOut( __METHOD__ );
171         return $bad;
172 }
173
174 /**
175  * Calculate the largest thumbnail width for a given original file size
176  * such that the thumbnail's height is at most $maxHeight.
177  * @param $boxWidth Integer Width of the thumbnail box.
178  * @param $boxHeight Integer Height of the thumbnail box.
179  * @param $maxHeight Integer Maximum height expected for the thumbnail.
180  * @return Integer.
181  */
182 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
183         $idealWidth = $boxWidth * $maxHeight / $boxHeight;
184         $roundedUp = ceil( $idealWidth );
185         if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
186                 return floor( $idealWidth );
187         else
188                 return $roundedUp;
189 }