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 ) {
39 function isAnimatedImage( $file ) {
40 # TODO: detect animated SVGs
41 $metadata = $file->getMetadata();
43 $metadata = $this->unpackMetadata( $metadata );
44 if( isset( $metadata['animated'] ) ) {
45 return $metadata['animated'];
56 function normaliseParams( $image, &$params ) {
58 if ( !parent
::normaliseParams( $image, $params ) ) {
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 );
79 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
81 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
82 if ( !$this->normaliseParams( $image, $params ) ) {
83 return new TransformParameterError( $params );
85 $clientWidth = $params['width'];
86 $clientHeight = $params['height'];
87 $physicalWidth = $params['physicalWidth'];
88 $physicalHeight = $params['physicalHeight'];
89 $srcPath = $image->getPath();
91 if ( $flags & self
::TRANSFORM_LATER
) {
92 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
95 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
96 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
97 wfMsg( 'thumbnail_dest_directory' ) );
100 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
101 if( $status === true ) {
102 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
104 return $status; // MediaTransformError
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
117 public function rasterize( $srcPath, $dstPath, $width, $height ) {
118 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
121 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
123 array( '$path/', '$width', '$height', '$input', '$output' ),
124 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
127 wfEscapeShellArg( $srcPath ),
128 wfEscapeShellArg( $dstPath ) ),
129 $wgSVGConverters[$wgSVGConverter]
131 wfProfileIn( 'rsvg' );
132 wfDebug( __METHOD__
.": $cmd\n" );
133 $err = wfShellExec( $cmd, $retval );
134 wfProfileOut( 'rsvg' );
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 );
148 * @param bool $metadata
151 function getImageSize( $file, $path, $metadata = false ) {
152 if ( $metadata === false ) {
153 $metadata = $file->getMetaData();
155 $metadata = $this->unpackMetaData( $metadata );
157 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
158 return array( $metadata['width'], $metadata['height'], 'SVG',
159 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
163 function getThumbType( $ext, $mime, $params = null ) {
164 return array( 'png', 'image/png' );
171 function getLongDesc( $file ) {
173 return wfMsgExt( 'svg-long-desc', 'parseinline',
174 $wgLang->formatNum( $file->getWidth() ),
175 $wgLang->formatNum( $file->getHeight() ),
176 $wgLang->formatSize( $file->getSize() ) );
179 function getMetadata( $file, $filename ) {
181 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
182 } catch( Exception
$e ) {
184 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
187 $metadata['version'] = self
::SVG_METADATA_VERSION
;
188 return serialize( $metadata );
191 function unpackMetadata( $metadata ) {
192 $unser = @unserialize
( $metadata );
193 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
200 function getMetadataType( $image ) {
204 function isMetadataValid( $image, $metadata ) {
205 return $this->unpackMetadata( $metadata ) !== false;
208 function visibleMetadataFields() {
209 $fields = array( 'title', 'description', 'animated' );
217 function formatMetadata( $file ) {
219 'visible' => array(),
220 'collapsed' => array()
222 $metadata = $file->getMetadata();
226 $metadata = $this->unpackMetadata( $metadata );
230 unset( $metadata['version'] );
231 unset( $metadata['metadata'] ); /* non-formatted XML */
233 /* TODO: add a formatter
234 $format = new FormatSVG( $metadata );
235 $formatted = $format->getFormattedData();
238 // Sort fields into visible and collapsed
239 $visibleFields = $this->visibleMetadataFields();
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',
248 foreach ( $metadata as $name => $value ) {
249 $tag = strtolower( $name );
250 if ( isset( $conversion[$tag] ) ) {
251 $tag = $conversion[$tag];
253 self
::addMeta( $result,
254 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',