3 * Handler for SVG images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Handler for SVG images.
29 class SvgHandler
extends ImageHandler
{
30 const SVG_METADATA_VERSION
= 2;
32 function isEnabled() {
33 global $wgSVGConverters, $wgSVGConverter;
34 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
35 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
42 function mustRender( $file ) {
46 function isVectorized( $file ) {
54 function isAnimatedImage( $file ) {
55 # TODO: detect animated SVGs
56 $metadata = $file->getMetadata();
58 $metadata = $this->unpackMetadata( $metadata );
59 if( isset( $metadata['animated'] ) ) {
60 return $metadata['animated'];
71 function normaliseParams( $image, &$params ) {
73 if ( !parent
::normaliseParams( $image, $params ) ) {
76 # Don't make an image bigger than wgMaxSVGSize on the smaller side
77 if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
78 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
79 $srcWidth = $image->getWidth( $params['page'] );
80 $srcHeight = $image->getHeight( $params['page'] );
81 $params['physicalWidth'] = $wgSVGMaxSize;
82 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
85 if ( $params['physicalHeight'] > $wgSVGMaxSize ) {
86 $srcWidth = $image->getWidth( $params['page'] );
87 $srcHeight = $image->getHeight( $params['page'] );
88 $params['physicalWidth'] = File
::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
89 $params['physicalHeight'] = $wgSVGMaxSize;
101 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
103 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
104 if ( !$this->normaliseParams( $image, $params ) ) {
105 return new TransformParameterError( $params );
107 $clientWidth = $params['width'];
108 $clientHeight = $params['height'];
109 $physicalWidth = $params['physicalWidth'];
110 $physicalHeight = $params['physicalHeight'];
111 $srcPath = $image->getLocalRefPath();
113 if ( $flags & self
::TRANSFORM_LATER
) {
114 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
117 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
118 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
119 wfMsg( 'thumbnail_dest_directory' ) );
122 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
123 if( $status === true ) {
124 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
126 return $status; // MediaTransformError
131 * Transform an SVG file to PNG
132 * This function can be called outside of thumbnail contexts
133 * @param string $srcPath
134 * @param string $dstPath
135 * @param string $width
136 * @param string $height
137 * @return bool|MediaTransformError
139 public function rasterize( $srcPath, $dstPath, $width, $height ) {
140 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
143 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
144 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
145 // This is a PHP callable
146 $func = $wgSVGConverters[$wgSVGConverter][0];
147 $args = array_merge( array( $srcPath, $dstPath, $width, $height ),
148 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
149 if ( !is_callable( $func ) ) {
150 throw new MWException( "$func is not callable" );
152 $err = call_user_func_array( $func, $args );
153 $retval = (bool)$err;
157 array( '$path/', '$width', '$height', '$input', '$output' ),
158 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
161 wfEscapeShellArg( $srcPath ),
162 wfEscapeShellArg( $dstPath ) ),
163 $wgSVGConverters[$wgSVGConverter]
165 wfProfileIn( 'rsvg' );
166 wfDebug( __METHOD__
.": $cmd\n" );
167 $err = wfShellExec( $cmd, $retval );
168 wfProfileOut( 'rsvg' );
171 $removed = $this->removeBadFile( $dstPath, $retval );
172 if ( $retval != 0 ||
$removed ) {
173 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
174 wfHostname(), $retval, trim($err), $cmd ) );
175 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
180 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
181 $im = new Imagick( $srcPath );
182 $im->setImageFormat( 'png' );
183 $im->setBackgroundColor( 'transparent' );
184 $im->setImageDepth( 8 );
186 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
187 return 'Could not resize image';
189 if ( !$im->writeImage( $dstPath ) ) {
190 return "Could not write to $dstPath";
197 * @param bool $metadata
200 function getImageSize( $file, $path, $metadata = false ) {
201 if ( $metadata === false ) {
202 $metadata = $file->getMetaData();
204 $metadata = $this->unpackMetaData( $metadata );
206 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
207 return array( $metadata['width'], $metadata['height'], 'SVG',
208 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
212 function getThumbType( $ext, $mime, $params = null ) {
213 return array( 'png', 'image/png' );
220 function getLongDesc( $file ) {
222 return wfMsgExt( 'svg-long-desc', 'parseinline',
223 $wgLang->formatNum( $file->getWidth() ),
224 $wgLang->formatNum( $file->getHeight() ),
225 $wgLang->formatSize( $file->getSize() ) );
228 function getMetadata( $file, $filename ) {
230 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
231 } catch( Exception
$e ) {
233 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
236 $metadata['version'] = self
::SVG_METADATA_VERSION
;
237 return serialize( $metadata );
240 function unpackMetadata( $metadata ) {
241 wfSuppressWarnings();
242 $unser = unserialize( $metadata );
244 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
251 function getMetadataType( $image ) {
255 function isMetadataValid( $image, $metadata ) {
256 return $this->unpackMetadata( $metadata ) !== false;
259 function visibleMetadataFields() {
260 $fields = array( 'title', 'description', 'animated' );
268 function formatMetadata( $file ) {
270 'visible' => array(),
271 'collapsed' => array()
273 $metadata = $file->getMetadata();
277 $metadata = $this->unpackMetadata( $metadata );
281 unset( $metadata['version'] );
282 unset( $metadata['metadata'] ); /* non-formatted XML */
284 /* TODO: add a formatter
285 $format = new FormatSVG( $metadata );
286 $formatted = $format->getFormattedData();
289 // Sort fields into visible and collapsed
290 $visibleFields = $this->visibleMetadataFields();
292 // Rename fields to be compatible with exif, so that
293 // the labels for these fields work.
294 $conversion = array( 'width' => 'imagewidth',
295 'height' => 'imagelength',
296 'description' => 'imagedescription',
297 'title' => 'objectname',
299 foreach ( $metadata as $name => $value ) {
300 $tag = strtolower( $name );
301 if ( isset( $conversion[$tag] ) ) {
302 $tag = $conversion[$tag];
304 self
::addMeta( $result,
305 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',