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] ) ) {
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" );
130 $err = call_user_func_array( $func, $args );
131 $retval = (bool)$err;
135 array( '$path/', '$width', '$height', '$input', '$output' ),
136 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
139 wfEscapeShellArg( $srcPath ),
140 wfEscapeShellArg( $dstPath ) ),
141 $wgSVGConverters[$wgSVGConverter]
143 wfProfileIn( 'rsvg' );
144 wfDebug( __METHOD__
.": $cmd\n" );
145 $err = wfShellExec( $cmd, $retval );
146 wfProfileOut( 'rsvg' );
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 );
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 );
164 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
165 return 'Could not resize image';
167 if ( !$im->writeImage( $dstPath ) ) {
168 return "Could not write to $dstPath";
175 * @param bool $metadata
178 function getImageSize( $file, $path, $metadata = false ) {
179 if ( $metadata === false ) {
180 $metadata = $file->getMetaData();
182 $metadata = $this->unpackMetaData( $metadata );
184 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
185 return array( $metadata['width'], $metadata['height'], 'SVG',
186 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
190 function getThumbType( $ext, $mime, $params = null ) {
191 return array( 'png', 'image/png' );
198 function getLongDesc( $file ) {
200 return wfMsgExt( 'svg-long-desc', 'parseinline',
201 $wgLang->formatNum( $file->getWidth() ),
202 $wgLang->formatNum( $file->getHeight() ),
203 $wgLang->formatSize( $file->getSize() ) );
206 function getMetadata( $file, $filename ) {
208 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
209 } catch( Exception
$e ) {
211 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
214 $metadata['version'] = self
::SVG_METADATA_VERSION
;
215 return serialize( $metadata );
218 function unpackMetadata( $metadata ) {
219 $unser = @unserialize
( $metadata );
220 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
227 function getMetadataType( $image ) {
231 function isMetadataValid( $image, $metadata ) {
232 return $this->unpackMetadata( $metadata ) !== false;
235 function visibleMetadataFields() {
236 $fields = array( 'title', 'description', 'animated' );
244 function formatMetadata( $file ) {
246 'visible' => array(),
247 'collapsed' => array()
249 $metadata = $file->getMetadata();
253 $metadata = $this->unpackMetadata( $metadata );
257 unset( $metadata['version'] );
258 unset( $metadata['metadata'] ); /* non-formatted XML */
260 /* TODO: add a formatter
261 $format = new FormatSVG( $metadata );
262 $formatted = $format->getFormattedData();
265 // Sort fields into visible and collapsed
266 $visibleFields = $this->visibleMetadataFields();
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',
275 foreach ( $metadata as $name => $value ) {
276 $tag = strtolower( $name );
277 if ( isset( $conversion[$tag] ) ) {
278 $tag = $conversion[$tag];
280 self
::addMeta( $result,
281 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',