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 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
124 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
125 wfMessage( 'thumbnail_dest_directory' )->text() );
128 $srcPath = $image->getLocalRefPath();
129 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
130 if( $status === true ) {
131 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
133 return $status; // MediaTransformError
138 * Transform an SVG file to PNG
139 * This function can be called outside of thumbnail contexts
140 * @param string $srcPath
141 * @param string $dstPath
142 * @param string $width
143 * @param string $height
144 * @return bool|MediaTransformError
146 public function rasterize( $srcPath, $dstPath, $width, $height ) {
147 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
150 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
151 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
152 // This is a PHP callable
153 $func = $wgSVGConverters[$wgSVGConverter][0];
154 $args = array_merge( array( $srcPath, $dstPath, $width, $height ),
155 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
156 if ( !is_callable( $func ) ) {
157 throw new MWException( "$func is not callable" );
159 $err = call_user_func_array( $func, $args );
160 $retval = (bool)$err;
164 array( '$path/', '$width', '$height', '$input', '$output' ),
165 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
168 wfEscapeShellArg( $srcPath ),
169 wfEscapeShellArg( $dstPath ) ),
170 $wgSVGConverters[$wgSVGConverter]
172 wfProfileIn( 'rsvg' );
173 wfDebug( __METHOD__
.": $cmd\n" );
174 $err = wfShellExec( $cmd, $retval );
175 wfProfileOut( 'rsvg' );
178 $removed = $this->removeBadFile( $dstPath, $retval );
179 if ( $retval != 0 ||
$removed ) {
180 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
181 wfHostname(), $retval, trim($err), $cmd ) );
182 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
187 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
188 $im = new Imagick( $srcPath );
189 $im->setImageFormat( 'png' );
190 $im->setBackgroundColor( 'transparent' );
191 $im->setImageDepth( 8 );
193 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
194 return 'Could not resize image';
196 if ( !$im->writeImage( $dstPath ) ) {
197 return "Could not write to $dstPath";
204 * @param bool $metadata
207 function getImageSize( $file, $path, $metadata = false ) {
208 if ( $metadata === false ) {
209 $metadata = $file->getMetaData();
211 $metadata = $this->unpackMetaData( $metadata );
213 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
214 return array( $metadata['width'], $metadata['height'], 'SVG',
215 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
219 function getThumbType( $ext, $mime, $params = null ) {
220 return array( 'png', 'image/png' );
224 * Subtitle for the image. Different from the base
225 * class so it can be denoted that SVG's have
226 * a "nominal" resolution, and not a fixed one,
227 * as well as so animation can be denoted.
232 function getLongDesc( $file ) {
234 $size = $wgLang->formatSize( $file->getSize() );
236 if ( $this->isAnimatedImage( $file ) ) {
237 $msg = wfMessage( 'svg-long-desc-animated' );
239 $msg = wfMessage( 'svg-long-desc' );
246 $msg->Params( $size );
247 return $msg->parse();
250 function getMetadata( $file, $filename ) {
252 $metadata = SVGMetadataExtractor
::getMetadata( $filename );
253 } catch( Exception
$e ) {
255 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
258 $metadata['version'] = self
::SVG_METADATA_VERSION
;
259 return serialize( $metadata );
262 function unpackMetadata( $metadata ) {
263 wfSuppressWarnings();
264 $unser = unserialize( $metadata );
266 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
273 function getMetadataType( $image ) {
277 function isMetadataValid( $image, $metadata ) {
278 $meta = $this->unpackMetadata( $metadata );
279 if ( $meta === false ) {
280 return self
::METADATA_BAD
;
282 if ( !isset( $meta['originalWidth'] ) ) {
283 // Old but compatible
284 return self
::METADATA_COMPATIBLE
;
286 return self
::METADATA_GOOD
;
289 function visibleMetadataFields() {
290 $fields = array( 'objectname', 'imagedescription' );
298 function formatMetadata( $file ) {
300 'visible' => array(),
301 'collapsed' => array()
303 $metadata = $file->getMetadata();
307 $metadata = $this->unpackMetadata( $metadata );
312 /* TODO: add a formatter
313 $format = new FormatSVG( $metadata );
314 $formatted = $format->getFormattedData();
317 // Sort fields into visible and collapsed
318 $visibleFields = $this->visibleMetadataFields();
320 // Rename fields to be compatible with exif, so that
321 // the labels for these fields work and reuse existing messages.
323 'originalwidth' => 'imagewidth',
324 'originalheight' => 'imagelength',
325 'description' => 'imagedescription',
326 'title' => 'objectname',
328 foreach ( $metadata as $name => $value ) {
329 $tag = strtolower( $name );
330 if ( isset( $conversion[$tag] ) ) {
331 $tag = $conversion[$tag];
333 // Do not output other metadata not in list
336 self
::addMeta( $result,
337 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',