Parameter documentation
[lhc/web/wiklou.git] / includes / media / SVG.php
1 <?php
2 /**
3 * Handler for SVG images.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Handler for SVG images.
11 *
12 * @ingroup Media
13 */
14 class SvgHandler extends ImageHandler {
15 const SVG_METADATA_VERSION = 2;
16
17 function isEnabled() {
18 global $wgSVGConverters, $wgSVGConverter;
19 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
20 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
21 return false;
22 } else {
23 return true;
24 }
25 }
26
27 function mustRender( $file ) {
28 return true;
29 }
30
31 function isVectorized( $file ) {
32 return true;
33 }
34
35 /**
36 * @param $file File
37 * @return bool
38 */
39 function isAnimatedImage( $file ) {
40 # TODO: detect animated SVGs
41 $metadata = $file->getMetadata();
42 if ( $metadata ) {
43 $metadata = $this->unpackMetadata( $metadata );
44 if( isset( $metadata['animated'] ) ) {
45 return $metadata['animated'];
46 }
47 }
48 return false;
49 }
50
51 /**
52 * @param $image File
53 * @param $params
54 * @return bool
55 */
56 function normaliseParams( $image, &$params ) {
57 global $wgSVGMaxSize;
58 if ( !parent::normaliseParams( $image, $params ) ) {
59 return false;
60 }
61 # Don't make an image bigger than wgMaxSVGSize
62 $params['physicalWidth'] = $params['width'];
63 $params['physicalHeight'] = $params['height'];
64 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
65 $srcWidth = $image->getWidth( $params['page'] );
66 $srcHeight = $image->getHeight( $params['page'] );
67 $params['physicalWidth'] = $wgSVGMaxSize;
68 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
69 }
70 return true;
71 }
72
73 /**
74 * @param $image File
75 * @param $dstPath
76 * @param $dstUrl
77 * @param $params
78 * @param int $flags
79 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
80 */
81 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
82 if ( !$this->normaliseParams( $image, $params ) ) {
83 return new TransformParameterError( $params );
84 }
85 $clientWidth = $params['width'];
86 $clientHeight = $params['height'];
87 $physicalWidth = $params['physicalWidth'];
88 $physicalHeight = $params['physicalHeight'];
89 $srcPath = $image->getPath();
90
91 if ( $flags & self::TRANSFORM_LATER ) {
92 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
93 }
94
95 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
96 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
97 wfMsg( 'thumbnail_dest_directory' ) );
98 }
99
100 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
101 if( $status === true ) {
102 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
103 } else {
104 return $status; // MediaTransformError
105 }
106 }
107
108 /*
109 * Transform an SVG file to PNG
110 * This function can be called outside of thumbnail contexts
111 * @param string $srcPath
112 * @param string $dstPath
113 * @param string $width
114 * @param string $height
115 * @returns TRUE/MediaTransformError
116 */
117 public function rasterize( $srcPath, $dstPath, $width, $height ) {
118 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
119 $err = false;
120 $retval = '';
121 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
122 $cmd = str_replace(
123 array( '$path/', '$width', '$height', '$input', '$output' ),
124 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
125 intval( $width ),
126 intval( $height ),
127 wfEscapeShellArg( $srcPath ),
128 wfEscapeShellArg( $dstPath ) ),
129 $wgSVGConverters[$wgSVGConverter]
130 ) . " 2>&1";
131 wfProfileIn( 'rsvg' );
132 wfDebug( __METHOD__.": $cmd\n" );
133 $err = wfShellExec( $cmd, $retval );
134 wfProfileOut( 'rsvg' );
135 }
136 $removed = $this->removeBadFile( $dstPath, $retval );
137 if ( $retval != 0 || $removed ) {
138 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
139 wfHostname(), $retval, trim($err), $cmd ) );
140 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
141 }
142 return true;
143 }
144
145 /**
146 * @param $file File
147 * @param $path
148 * @param bool $metadata
149 * @return array
150 */
151 function getImageSize( $file, $path, $metadata = false ) {
152 if ( $metadata === false ) {
153 $metadata = $file->getMetaData();
154 }
155 $metadata = $this->unpackMetaData( $metadata );
156
157 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
158 return array( $metadata['width'], $metadata['height'], 'SVG',
159 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
160 }
161 }
162
163 function getThumbType( $ext, $mime, $params = null ) {
164 return array( 'png', 'image/png' );
165 }
166
167 /**
168 * @param $file File
169 * @return string
170 */
171 function getLongDesc( $file ) {
172 global $wgLang;
173 return wfMsgExt( 'svg-long-desc', 'parseinline',
174 $wgLang->formatNum( $file->getWidth() ),
175 $wgLang->formatNum( $file->getHeight() ),
176 $wgLang->formatSize( $file->getSize() ) );
177 }
178
179 function getMetadata( $file, $filename ) {
180 try {
181 $metadata = SVGMetadataExtractor::getMetadata( $filename );
182 } catch( Exception $e ) {
183 // Broken file?
184 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
185 return '0';
186 }
187 $metadata['version'] = self::SVG_METADATA_VERSION;
188 return serialize( $metadata );
189 }
190
191 function unpackMetadata( $metadata ) {
192 $unser = @unserialize( $metadata );
193 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
194 return $unser;
195 } else {
196 return false;
197 }
198 }
199
200 function getMetadataType( $image ) {
201 return 'parsed-svg';
202 }
203
204 function isMetadataValid( $image, $metadata ) {
205 return $this->unpackMetadata( $metadata ) !== false;
206 }
207
208 function visibleMetadataFields() {
209 $fields = array( 'title', 'description', 'animated' );
210 return $fields;
211 }
212
213 /**
214 * @param $file File
215 * @return array|bool
216 */
217 function formatMetadata( $file ) {
218 $result = array(
219 'visible' => array(),
220 'collapsed' => array()
221 );
222 $metadata = $file->getMetadata();
223 if ( !$metadata ) {
224 return false;
225 }
226 $metadata = $this->unpackMetadata( $metadata );
227 if ( !$metadata ) {
228 return false;
229 }
230 unset( $metadata['version'] );
231 unset( $metadata['metadata'] ); /* non-formatted XML */
232
233 /* TODO: add a formatter
234 $format = new FormatSVG( $metadata );
235 $formatted = $format->getFormattedData();
236 */
237
238 // Sort fields into visible and collapsed
239 $visibleFields = $this->visibleMetadataFields();
240
241 // Rename fields to be compatible with exif, so that
242 // the labels for these fields work.
243 $conversion = array( 'width' => 'imagewidth',
244 'height' => 'imagelength',
245 'description' => 'imagedescription',
246 'title' => 'objectname',
247 );
248 foreach ( $metadata as $name => $value ) {
249 $tag = strtolower( $name );
250 if ( isset( $conversion[$tag] ) ) {
251 $tag = $conversion[$tag];
252 }
253 self::addMeta( $result,
254 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
255 'exif',
256 $tag,
257 $value
258 );
259 }
260 return $result;
261 }
262 }