More reversion of r77297, 1 of 2 commits to keep it readable in CR (hopefully)
[mediawiki.git] / includes / media / SVG.php
blob899429cc9e800d94ac483329e2a88cae5df1066f
1 <?php
2 /**
3 * Handler for SVG images.
5 * @file
6 * @ingroup Media
7 */
9 /**
10 * Handler for SVG images.
12 * @ingroup Media
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" );
21 return false;
22 } else {
23 return true;
27 function mustRender( $file ) {
28 return true;
31 function isVectorized( $file ) {
32 return true;
35 function isAnimatedImage( $file ) {
36 # TODO: detect animated SVGs
37 $metadata = $file->getMetadata();
38 if ( $metadata ) {
39 $metadata = $this->unpackMetadata( $metadata );
40 if( isset( $metadata['animated'] ) ) {
41 return $metadata['animated'];
44 return false;
47 function normaliseParams( $image, &$params ) {
48 global $wgSVGMaxSize;
49 if ( !parent::normaliseParams( $image, $params ) ) {
50 return false;
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 );
61 return true;
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 );
86 } else {
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;
102 $err = false;
103 $retval = '';
104 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
105 $cmd = str_replace(
106 array( '$path/', '$width', '$height', '$input', '$output' ),
107 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
108 intval( $width ),
109 intval( $height ),
110 wfEscapeShellArg( $srcPath ),
111 wfEscapeShellArg( $dstPath ) ),
112 $wgSVGConverters[$wgSVGConverter]
113 ) . " 2>&1";
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 );
125 return true;
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 ) {
145 global $wgLang;
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 ) {
153 try {
154 $metadata = SVGMetadataExtractor::getMetadata( $filename );
155 } catch( Exception $e ) {
156 // Broken file?
157 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
158 return '0';
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 ) {
167 return $unser;
168 } else {
169 return false;
173 function getMetadataType( $image ) {
174 return 'parsed-svg';
177 function isMetadataValid( $image, $metadata ) {
178 return $this->unpackMetadata( $metadata ) !== false;
181 function visibleMetadataFields() {
182 $fields = array( 'title', 'description', 'animated' );
183 return $fields;
186 function formatMetadata( $file ) {
187 $result = array(
188 'visible' => array(),
189 'collapsed' => array()
191 $metadata = $file->getMetadata();
192 if ( !$metadata ) {
193 return false;
195 $metadata = $this->unpackMetadata( $metadata );
196 if ( !$metadata ) {
197 return false;
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',
213 'svg',
214 $tag,
215 $value
218 return $result;