38588209b4ac9108444079e3272cf8a752c76180
3 * Handler for SVG images.
10 * Handler for SVG images.
14 class SvgHandler
extends ImageHandler
{
15 const SVG_METADATA_VERSION
= 1;
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( $image ) {
36 # TODO: detect animated SVGs
40 function normaliseParams( $image, &$params ) {
42 if ( !parent
::normaliseParams( $image, $params ) ) {
45 # Don't make an image bigger than wgMaxSVGSize
46 $params['physicalWidth'] = $params['width'];
47 $params['physicalHeight'] = $params['height'];
48 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
49 $srcWidth = $image->getWidth( $params['page'] );
50 $srcHeight = $image->getHeight( $params['page'] );
51 $params['physicalWidth'] = $wgSVGMaxSize;
52 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
57 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
58 if ( !$this->normaliseParams( $image, $params ) ) {
59 return new TransformParameterError( $params );
61 $clientWidth = $params['width'];
62 $clientHeight = $params['height'];
63 $physicalWidth = $params['physicalWidth'];
64 $physicalHeight = $params['physicalHeight'];
65 $srcPath = $image->getPath();
67 if ( $flags & self
::TRANSFORM_LATER
) {
68 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
71 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
72 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
73 wfMsg( 'thumbnail_dest_directory' ) );
76 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
77 if( $status === true ) {
78 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
80 return $status; // MediaTransformError
85 * Transform an SVG file to PNG
86 * This function can be called outside of thumbnail contexts
87 * @param string $srcPath
88 * @param string $dstPath
89 * @param string $width
90 * @param string $height
91 * @returns TRUE/MediaTransformError
93 public function rasterize( $srcPath, $dstPath, $width, $height ) {
94 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
97 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
99 array( '$path/', '$width', '$height', '$input', '$output' ),
100 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
103 wfEscapeShellArg( $srcPath ),
104 wfEscapeShellArg( $dstPath ) ),
105 $wgSVGConverters[$wgSVGConverter]
107 wfProfileIn( 'rsvg' );
108 wfDebug( __METHOD__
.": $cmd\n" );
109 $err = wfShellExec( $cmd, $retval );
110 wfProfileOut( 'rsvg' );
112 $removed = $this->removeBadFile( $dstPath, $retval );
113 if ( $retval != 0 ||
$removed ) {
114 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
115 wfHostname(), $retval, trim($err), $cmd ) );
116 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
121 function getImageSize( $file, $path, $metadata = false ) {
122 if ( $metadata === false ) {
123 $metadata = $file->getMetaData();
125 $metadata = $this->unpackMetaData( $metadata );
127 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
128 return array( $metadata['width'], $metadata['height'], 'SVG',
129 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
133 function getThumbType( $ext, $mime, $params = null ) {
134 return array( 'png', 'image/png' );
137 function getLongDesc( $file ) {
139 return wfMsgExt( 'svg-long-desc', 'parseinline',
140 $wgLang->formatNum( $file->getWidth() ),
141 $wgLang->formatNum( $file->getHeight() ),
142 $wgLang->formatSize( $file->getSize() ) );
145 function formatMetadata( $file ) {
149 function getMetadata( $file, $filename ) {
152 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
153 } catch( Exception
$e ) {
155 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
158 $metadata['version'] = self
::SVG_METADATA_VERSION
;
159 return serialize( $metadata );
162 function unpackMetadata( $metadata ) {
163 $unser = @unserialize
( $metadata );
164 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
171 function getMetadataType( $image ) {
175 function isMetadataValid( $image, $metadata ) {
176 return $this->unpackMetadata( $metadata ) !== false;