Change layout of the mustBePosted format to standardise it
[lhc/web/wiklou.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 # Handle redirects
127 $redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
128 if( $redirectTitle ) {
129 $name = $redirectTitle->getDbKey();
130 }
131
132 # Run the extension hook
133 $bad = false;
134 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
135 wfProfileOut( __METHOD__ );
136 return $bad;
137 }
138
139 if( !$badImages ) {
140 # Build the list now
141 $badImages = array();
142 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
143 foreach( $lines as $line ) {
144 # List items only
145 if ( substr( $line, 0, 1 ) !== '*' ) {
146 continue;
147 }
148
149 # Find all links
150 $m = array();
151 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
152 continue;
153 }
154
155 $exceptions = array();
156 $imageDBkey = false;
157 foreach ( $m[1] as $i => $titleText ) {
158 $title = Title::newFromText( $titleText );
159 if ( !is_null( $title ) ) {
160 if ( $i == 0 ) {
161 $imageDBkey = $title->getDBkey();
162 } else {
163 $exceptions[$title->getPrefixedDBkey()] = true;
164 }
165 }
166 }
167
168 if ( $imageDBkey !== false ) {
169 $badImages[$imageDBkey] = $exceptions;
170 }
171 }
172 }
173
174 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
175 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
176 wfProfileOut( __METHOD__ );
177 return $bad;
178 }
179
180 /**
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.
186 * @return Integer.
187 */
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 );
193 else
194 return $roundedUp;
195 }