follow-up r86195 (Sort of). Make sure the string there can really be exploded before...
[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 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
123 // This is a PHP callable
124 $func = $wgSVGConverters[$wgSVGConverter][0];
125 $args = array_merge( array( $srcPath, $dstPath, $width, $height ),
126 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
127 if ( !is_callable( $func ) ) {
128 throw new MWException( "$func is not callable" );
129 }
130 $err = call_user_func_array( $func, $args );
131 $retval = (bool)$err;
132 } else {
133 // External command
134 $cmd = str_replace(
135 array( '$path/', '$width', '$height', '$input', '$output' ),
136 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
137 intval( $width ),
138 intval( $height ),
139 wfEscapeShellArg( $srcPath ),
140 wfEscapeShellArg( $dstPath ) ),
141 $wgSVGConverters[$wgSVGConverter]
142 ) . " 2>&1";
143 wfProfileIn( 'rsvg' );
144 wfDebug( __METHOD__.": $cmd\n" );
145 $err = wfShellExec( $cmd, $retval );
146 wfProfileOut( 'rsvg' );
147 }
148 }
149 $removed = $this->removeBadFile( $dstPath, $retval );
150 if ( $retval != 0 || $removed ) {
151 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
152 wfHostname(), $retval, trim($err), $cmd ) );
153 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
154 }
155 return true;
156 }
157
158 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
159 $im = new Imagick( $srcPath );
160 $im->setImageFormat( 'png' );
161 $im->setBackgroundColor( 'transparent' );
162 $im->setImageDepth( 8 );
163
164 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
165 return 'Could not resize image';
166 }
167 if ( !$im->writeImage( $dstPath ) ) {
168 return "Could not write to $dstPath";
169 }
170 }
171
172 /**
173 * @param $file File
174 * @param $path
175 * @param bool $metadata
176 * @return array
177 */
178 function getImageSize( $file, $path, $metadata = false ) {
179 if ( $metadata === false ) {
180 $metadata = $file->getMetaData();
181 }
182 $metadata = $this->unpackMetaData( $metadata );
183
184 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
185 return array( $metadata['width'], $metadata['height'], 'SVG',
186 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
187 }
188 }
189
190 function getThumbType( $ext, $mime, $params = null ) {
191 return array( 'png', 'image/png' );
192 }
193
194 /**
195 * @param $file File
196 * @return string
197 */
198 function getLongDesc( $file ) {
199 global $wgLang;
200 return wfMsgExt( 'svg-long-desc', 'parseinline',
201 $wgLang->formatNum( $file->getWidth() ),
202 $wgLang->formatNum( $file->getHeight() ),
203 $wgLang->formatSize( $file->getSize() ) );
204 }
205
206 function getMetadata( $file, $filename ) {
207 try {
208 $metadata = SVGMetadataExtractor::getMetadata( $filename );
209 } catch( Exception $e ) {
210 // Broken file?
211 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
212 return '0';
213 }
214 $metadata['version'] = self::SVG_METADATA_VERSION;
215 return serialize( $metadata );
216 }
217
218 function unpackMetadata( $metadata ) {
219 $unser = @unserialize( $metadata );
220 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
221 return $unser;
222 } else {
223 return false;
224 }
225 }
226
227 function getMetadataType( $image ) {
228 return 'parsed-svg';
229 }
230
231 function isMetadataValid( $image, $metadata ) {
232 return $this->unpackMetadata( $metadata ) !== false;
233 }
234
235 function visibleMetadataFields() {
236 $fields = array( 'title', 'description', 'animated' );
237 return $fields;
238 }
239
240 /**
241 * @param $file File
242 * @return array|bool
243 */
244 function formatMetadata( $file ) {
245 $result = array(
246 'visible' => array(),
247 'collapsed' => array()
248 );
249 $metadata = $file->getMetadata();
250 if ( !$metadata ) {
251 return false;
252 }
253 $metadata = $this->unpackMetadata( $metadata );
254 if ( !$metadata ) {
255 return false;
256 }
257 unset( $metadata['version'] );
258 unset( $metadata['metadata'] ); /* non-formatted XML */
259
260 /* TODO: add a formatter
261 $format = new FormatSVG( $metadata );
262 $formatted = $format->getFormattedData();
263 */
264
265 // Sort fields into visible and collapsed
266 $visibleFields = $this->visibleMetadataFields();
267
268 // Rename fields to be compatible with exif, so that
269 // the labels for these fields work.
270 $conversion = array( 'width' => 'imagewidth',
271 'height' => 'imagelength',
272 'description' => 'imagedescription',
273 'title' => 'objectname',
274 );
275 foreach ( $metadata as $name => $value ) {
276 $tag = strtolower( $name );
277 if ( isset( $conversion[$tag] ) ) {
278 $tag = $conversion[$tag];
279 }
280 self::addMeta( $result,
281 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
282 'exif',
283 $tag,
284 $value
285 );
286 }
287 return $result;
288 }
289 }