* (bug 3691) Aspect ratio from viewBox attribute is now preserved for SVG
[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 $viewpoerSize: Float optional scale for percentage units...
8 * @return Integer: 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 round( $length * 0.01 * $viewportSize );
28 } else {
29 return round( $length * $unitLength[$unit] );
30 }
31 } else {
32 // Assume pixels
33 return round( 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 $this->width = $width;
80 $this->height = $height;
81 $this->first = false;
82 }
83 }
84 }
85
86 /**
87 * Compatible with PHP getimagesize()
88 * @todo support gzipped SVGZ
89 * @todo check XML more carefully
90 * @todo sensible defaults
91 *
92 * @param $filename String: full name of the file (passed to php fopen()).
93 * @return array
94 */
95 function wfGetSVGsize( $filename ) {
96 $filter = new XmlSizeFilter();
97 $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) );
98 if( $xml->wellFormed ) {
99 return array( $filter->width, $filter->height, 'SVG',
100 "width=\"$filter->width\" height=\"$filter->height\"" );
101 }
102
103 return false;
104 }
105
106 /**
107 * Determine if an image exists on the 'bad image list'.
108 *
109 * The format of MediaWiki:Bad_image_list is as follows:
110 * * Only list items (lines starting with "*") are considered
111 * * The first link on a line must be a link to a bad image
112 * * Any subsequent links on the same line are considered to be exceptions,
113 * i.e. articles where the image may occur inline.
114 *
115 * @param $name string the image name to check
116 * @param $contextTitle Title: the page on which the image occurs, if known
117 * @return bool
118 */
119 function wfIsBadImage( $name, $contextTitle = false ) {
120 static $badImages = false;
121 wfProfileIn( __METHOD__ );
122
123 # Run the extension hook
124 $bad = false;
125 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
126 wfProfileOut( __METHOD__ );
127 return $bad;
128 }
129
130 if( !$badImages ) {
131 # Build the list now
132 $badImages = array();
133 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
134 foreach( $lines as $line ) {
135 # List items only
136 if ( substr( $line, 0, 1 ) !== '*' ) {
137 continue;
138 }
139
140 # Find all links
141 $m = array();
142 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
143 continue;
144 }
145
146 $exceptions = array();
147 $imageDBkey = false;
148 foreach ( $m[1] as $i => $titleText ) {
149 $title = Title::newFromText( $titleText );
150 if ( !is_null( $title ) ) {
151 if ( $i == 0 ) {
152 $imageDBkey = $title->getDBkey();
153 } else {
154 $exceptions[$title->getPrefixedDBkey()] = true;
155 }
156 }
157 }
158
159 if ( $imageDBkey !== false ) {
160 $badImages[$imageDBkey] = $exceptions;
161 }
162 }
163 }
164
165 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
166 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
167 wfProfileOut( __METHOD__ );
168 return $bad;
169 }
170
171 /**
172 * Calculate the largest thumbnail width for a given original file size
173 * such that the thumbnail's height is at most $maxHeight.
174 * @param $boxWidth Integer Width of the thumbnail box.
175 * @param $boxHeight Integer Height of the thumbnail box.
176 * @param $maxHeight Integer Maximum height expected for the thumbnail.
177 * @return Integer.
178 */
179 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
180 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
181 $roundedUp = ceil( $idealWidth );
182 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
183 return floor( $idealWidth );
184 else
185 return $roundedUp;
186 }