WARNING: HUGE COMMIT
[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 * @return Integer: length in pixels
8 */
9 function wfScaleSVGUnit( $length ) {
10 static $unitLength = array(
11 'px' => 1.0,
12 'pt' => 1.25,
13 'pc' => 15.0,
14 'mm' => 3.543307,
15 'cm' => 35.43307,
16 'in' => 90.0,
17 '' => 1.0, // "User units" pixels by default
18 '%' => 2.0, // Fake it!
19 );
20 $matches = array();
21 if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
22 $length = floatval( $matches[1] );
23 $unit = $matches[2];
24 return round( $length * $unitLength[$unit] );
25 } else {
26 // Assume pixels
27 return round( floatval( $length ) );
28 }
29 }
30
31 /**
32 * Compatible with PHP getimagesize()
33 * @todo support gzipped SVGZ
34 * @todo check XML more carefully
35 * @todo sensible defaults
36 *
37 * @param $filename String: full name of the file (passed to php fopen()).
38 * @return array
39 */
40 function wfGetSVGsize( $filename ) {
41 $width = 256;
42 $height = 256;
43
44 // Read a chunk of the file
45 $f = fopen( $filename, "rt" );
46 if( !$f ) return false;
47 $chunk = fread( $f, 4096 );
48 fclose( $f );
49
50 // Uber-crappy hack! Run through a real XML parser.
51 $matches = array();
52 if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
53 return false;
54 }
55 $tag = $matches[1];
56 if( preg_match( '/(?:^|\s)width\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
57 $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
58 }
59 if( preg_match( '/(?:^|\s)height\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
60 $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
61 }
62
63 return array( $width, $height, 'SVG',
64 "width=\"$width\" height=\"$height\"" );
65 }
66
67 /**
68 * Determine if an image exists on the 'bad image list'.
69 *
70 * The format of MediaWiki:Bad_image_list is as follows:
71 * * Only list items (lines starting with "*") are considered
72 * * The first link on a line must be a link to a bad image
73 * * Any subsequent links on the same line are considered to be exceptions,
74 * i.e. articles where the image may occur inline.
75 *
76 * @param $name string the image name to check
77 * @param $contextTitle Title: the page on which the image occurs, if known
78 * @return bool
79 */
80 function wfIsBadImage( $name, $contextTitle = false ) {
81 static $badImages = false;
82 wfProfileIn( __METHOD__ );
83
84 # Run the extension hook
85 $bad = false;
86 if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
87 wfProfileOut( __METHOD__ );
88 return $bad;
89 }
90
91 if( !$badImages ) {
92 # Build the list now
93 $badImages = array();
94 $lines = explode( "\n", wfMsgForContentNoTrans( 'bad_image_list' ) );
95 foreach( $lines as $line ) {
96 # List items only
97 if ( substr( $line, 0, 1 ) !== '*' ) {
98 continue;
99 }
100
101 # Find all links
102 $m = array();
103 if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
104 continue;
105 }
106
107 $exceptions = array();
108 $imageDBkey = false;
109 foreach ( $m[1] as $i => $titleText ) {
110 $title = Title::newFromText( $titleText );
111 if ( !is_null( $title ) ) {
112 if ( $i == 0 ) {
113 $imageDBkey = $title->getDBkey();
114 } else {
115 $exceptions[$title->getPrefixedDBkey()] = true;
116 }
117 }
118 }
119
120 if ( $imageDBkey !== false ) {
121 $badImages[$imageDBkey] = $exceptions;
122 }
123 }
124 }
125
126 $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
127 $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
128 wfProfileOut( __METHOD__ );
129 return $bad;
130 }
131
132 /**
133 * Calculate the largest thumbnail width for a given original file size
134 * such that the thumbnail's height is at most $maxHeight.
135 * @param $boxWidth Integer Width of the thumbnail box.
136 * @param $boxHeight Integer Height of the thumbnail box.
137 * @param $maxHeight Integer Maximum height expected for the thumbnail.
138 * @return Integer.
139 */
140 function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
141 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
142 $roundedUp = ceil( $idealWidth );
143 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
144 return floor( $idealWidth );
145 else
146 return $roundedUp;
147 }