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'];
67 * We do not support making animated svg thumbnails
69 function canAnimateThumb( $file ) {
78 function normaliseParams( $image, &$params ) {
80 if ( !parent
::normaliseParams( $image, $params ) ) {
83 # Don't make an image bigger than wgMaxSVGSize on the smaller side
84 if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
85 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
86 $srcWidth = $image->getWidth( $params['page'] );
87 $srcHeight = $image->getHeight( $params['page'] );
88 $params['physicalWidth'] = $wgSVGMaxSize;
89 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
92 if ( $params['physicalHeight'] > $wgSVGMaxSize ) {
93 $srcWidth = $image->getWidth( $params['page'] );
94 $srcHeight = $image->getHeight( $params['page'] );
95 $params['physicalWidth'] = File
::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
96 $params['physicalHeight'] = $wgSVGMaxSize;
108 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
110 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
111 if ( !$this->normaliseParams( $image, $params ) ) {
112 return new TransformParameterError( $params );
114 $clientWidth = $params['width'];
115 $clientHeight = $params['height'];
116 $physicalWidth = $params['physicalWidth'];
117 $physicalHeight = $params['physicalHeight'];
119 if ( $flags & self
::TRANSFORM_LATER
) {
120 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
123 $metadata = $this->unpackMetadata( $image->getMetadata() );
124 if ( isset( $metadata['error'] ) ) { // sanity check
125 $err = wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
126 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
129 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
130 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
131 wfMessage( 'thumbnail_dest_directory' )->text() );
134 $srcPath = $image->getLocalRefPath();
135 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
136 if ( $status === true ) {
137 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
139 return $status; // MediaTransformError
144 * Transform an SVG file to PNG
145 * This function can be called outside of thumbnail contexts
146 * @param string $srcPath
147 * @param string $dstPath
148 * @param string $width
149 * @param string $height
150 * @throws MWException
151 * @return bool|MediaTransformError
153 public function rasterize( $srcPath, $dstPath, $width, $height ) {
154 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
157 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
158 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
159 // This is a PHP callable
160 $func = $wgSVGConverters[$wgSVGConverter][0];
161 $args = array_merge( array( $srcPath, $dstPath, $width, $height ),
162 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
163 if ( !is_callable( $func ) ) {
164 throw new MWException( "$func is not callable" );
166 $err = call_user_func_array( $func, $args );
167 $retval = (bool)$err;
171 array( '$path/', '$width', '$height', '$input', '$output' ),
172 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
175 wfEscapeShellArg( $srcPath ),
176 wfEscapeShellArg( $dstPath ) ),
177 $wgSVGConverters[$wgSVGConverter]
179 wfProfileIn( 'rsvg' );
180 wfDebug( __METHOD__
. ": $cmd\n" );
181 $err = wfShellExec( $cmd, $retval );
182 wfProfileOut( 'rsvg' );
185 $removed = $this->removeBadFile( $dstPath, $retval );
186 if ( $retval != 0 ||
$removed ) {
187 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
188 wfHostname(), $retval, trim( $err ), $cmd ) );
189 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
194 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
195 $im = new Imagick( $srcPath );
196 $im->setImageFormat( 'png' );
197 $im->setBackgroundColor( 'transparent' );
198 $im->setImageDepth( 8 );
200 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
201 return 'Could not resize image';
203 if ( !$im->writeImage( $dstPath ) ) {
204 return "Could not write to $dstPath";
211 * @param bool $metadata
214 function getImageSize( $file, $path, $metadata = false ) {
215 if ( $metadata === false ) {
216 $metadata = $file->getMetaData();
218 $metadata = $this->unpackMetaData( $metadata );
220 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
221 return array( $metadata['width'], $metadata['height'], 'SVG',
222 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
224 return array( 0, 0, 'SVG', "width=\"0\" height=\"0\"" );
228 function getThumbType( $ext, $mime, $params = null ) {
229 return array( 'png', 'image/png' );
233 * Subtitle for the image. Different from the base
234 * class so it can be denoted that SVG's have
235 * a "nominal" resolution, and not a fixed one,
236 * as well as so animation can be denoted.
241 function getLongDesc( $file ) {
244 $metadata = $this->unpackMetadata( $file->getMetadata() );
245 if ( isset( $metadata['error'] ) ) {
246 return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
249 $size = $wgLang->formatSize( $file->getSize() );
251 if ( $this->isAnimatedImage( $file ) ) {
252 $msg = wfMessage( 'svg-long-desc-animated' );
254 $msg = wfMessage( 'svg-long-desc' );
257 $msg->numParams( $file->getWidth(), $file->getHeight() )->params( $size );
259 return $msg->parse();
262 function getMetadata( $file, $filename ) {
263 $metadata = array( 'version' => self
::SVG_METADATA_VERSION
);
265 $metadata +
= SVGMetadataExtractor
::getMetadata( $filename );
266 } catch ( MWException
$e ) { // @todo SVG specific exceptions
267 // File not found, broken, etc.
268 $metadata['error'] = array(
269 'message' => $e->getMessage(),
270 'code' => $e->getCode()
272 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
274 return serialize( $metadata );
277 function unpackMetadata( $metadata ) {
278 wfSuppressWarnings();
279 $unser = unserialize( $metadata );
281 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
288 function getMetadataType( $image ) {
292 function isMetadataValid( $image, $metadata ) {
293 $meta = $this->unpackMetadata( $metadata );
294 if ( $meta === false ) {
295 return self
::METADATA_BAD
;
297 if ( !isset( $meta['originalWidth'] ) ) {
298 // Old but compatible
299 return self
::METADATA_COMPATIBLE
;
301 return self
::METADATA_GOOD
;
304 function visibleMetadataFields() {
305 $fields = array( 'objectname', 'imagedescription' );
313 function formatMetadata( $file ) {
315 'visible' => array(),
316 'collapsed' => array()
318 $metadata = $file->getMetadata();
322 $metadata = $this->unpackMetadata( $metadata );
323 if ( !$metadata ||
isset( $metadata['error'] ) ) {
327 /* TODO: add a formatter
328 $format = new FormatSVG( $metadata );
329 $formatted = $format->getFormattedData();
332 // Sort fields into visible and collapsed
333 $visibleFields = $this->visibleMetadataFields();
335 // Rename fields to be compatible with exif, so that
336 // the labels for these fields work and reuse existing messages.
338 'originalwidth' => 'imagewidth',
339 'originalheight' => 'imagelength',
340 'description' => 'imagedescription',
341 'title' => 'objectname',
343 foreach ( $metadata as $name => $value ) {
344 $tag = strtolower( $name );
345 if ( isset( $conversion[$tag] ) ) {
346 $tag = $conversion[$tag];
348 // Do not output other metadata not in list
351 self
::addMeta( $result,
352 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',