3 * Handler for SVG images.
10 * Handler for SVG images.
14 class SvgHandler
extends ImageHandler
{
15 const SVG_METADATA_VERSION
= 2;
17 function isEnabled() {
18 global $wgSVGConverters, $wgSVGConverter;
19 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
20 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
27 function mustRender( $file ) {
31 function isVectorized( $file ) {
35 function isAnimatedImage( $file ) {
36 # TODO: detect animated SVGs
37 $metadata = $file->getMetadata();
39 $metadata = $this->unpackMetadata( $metadata );
40 if( isset( $metadata['animated'] ) ) {
41 return $metadata['animated'];
47 function normaliseParams( $image, &$params ) {
49 if ( !parent
::normaliseParams( $image, $params ) ) {
52 # Don't make an image bigger than wgMaxSVGSize
53 $params['physicalWidth'] = $params['width'];
54 $params['physicalHeight'] = $params['height'];
55 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
56 $srcWidth = $image->getWidth( $params['page'] );
57 $srcHeight = $image->getHeight( $params['page'] );
58 $params['physicalWidth'] = $wgSVGMaxSize;
59 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
64 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
65 if ( !$this->normaliseParams( $image, $params ) ) {
66 return new TransformParameterError( $params );
68 $clientWidth = $params['width'];
69 $clientHeight = $params['height'];
70 $physicalWidth = $params['physicalWidth'];
71 $physicalHeight = $params['physicalHeight'];
72 $srcPath = $image->getPath();
74 if ( $flags & self
::TRANSFORM_LATER
) {
75 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
78 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
79 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
80 wfMsg( 'thumbnail_dest_directory' ) );
83 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
84 if( $status === true ) {
85 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
87 return $status; // MediaTransformError
92 * Transform an SVG file to PNG
93 * This function can be called outside of thumbnail contexts
94 * @param string $srcPath
95 * @param string $dstPath
96 * @param string $width
97 * @param string $height
98 * @returns TRUE/MediaTransformError
100 public function rasterize( $srcPath, $dstPath, $width, $height ) {
101 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
104 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
106 array( '$path/', '$width', '$height', '$input', '$output' ),
107 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
110 wfEscapeShellArg( $srcPath ),
111 wfEscapeShellArg( $dstPath ) ),
112 $wgSVGConverters[$wgSVGConverter]
114 wfProfileIn( 'rsvg' );
115 wfDebug( __METHOD__
.": $cmd\n" );
116 $err = wfShellExec( $cmd, $retval );
117 wfProfileOut( 'rsvg' );
119 $removed = $this->removeBadFile( $dstPath, $retval );
120 if ( $retval != 0 ||
$removed ) {
121 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
122 wfHostname(), $retval, trim($err), $cmd ) );
123 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
128 function getImageSize( $file, $path, $metadata = false ) {
129 if ( $metadata === false ) {
130 $metadata = $file->getMetaData();
132 $metadata = $this->unpackMetaData( $metadata );
134 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
135 return array( $metadata['width'], $metadata['height'], 'SVG',
136 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
140 function getThumbType( $ext, $mime, $params = null ) {
141 return array( 'png', 'image/png' );
144 function getLongDesc( $file ) {
146 return wfMsgExt( 'svg-long-desc', 'parseinline',
147 $wgLang->formatNum( $file->getWidth() ),
148 $wgLang->formatNum( $file->getHeight() ),
149 $wgLang->formatSize( $file->getSize() ) );
152 function getMetadata( $file, $filename ) {
154 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
155 } catch( Exception
$e ) {
157 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
160 $metadata['version'] = self
::SVG_METADATA_VERSION
;
161 return serialize( $metadata );
164 function unpackMetadata( $metadata ) {
165 $unser = @unserialize
( $metadata );
166 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
173 function getMetadataType( $image ) {
177 function isMetadataValid( $image, $metadata ) {
178 return $this->unpackMetadata( $metadata ) !== false;
181 function visibleMetadataFields() {
182 $fields = array( 'title', 'description', 'animated' );
186 function formatMetadata( $file ) {
188 'visible' => array(),
189 'collapsed' => array()
191 $metadata = $file->getMetadata();
195 $metadata = $this->unpackMetadata( $metadata );
199 unset( $metadata['version'] );
200 unset( $metadata['metadata'] ); /* non-formatted XML */
202 /* TODO: add a formatter
203 $format = new FormatSVG( $metadata );
204 $formatted = $format->getFormattedData();
207 // Sort fields into visible and collapsed
208 $visibleFields = $this->visibleMetadataFields();
209 foreach ( $metadata as $name => $value ) {
210 $tag = strtolower( $name );
211 self
::addMeta( $result,
212 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',