3 * Handler for SVG images.
10 * Handler for SVG images.
14 class SvgHandler
extends ImageHandler
{
15 function isEnabled() {
16 global $wgSVGConverters, $wgSVGConverter;
17 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
18 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
25 function mustRender( $file ) {
29 function isVectorized( $file ) {
33 function normaliseParams( $image, &$params ) {
35 if ( !parent
::normaliseParams( $image, $params ) ) {
38 # Don't make an image bigger than wgMaxSVGSize
39 $params['physicalWidth'] = $params['width'];
40 $params['physicalHeight'] = $params['height'];
41 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
42 $srcWidth = $image->getWidth( $params['page'] );
43 $srcHeight = $image->getHeight( $params['page'] );
44 $params['physicalWidth'] = $wgSVGMaxSize;
45 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
50 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
51 if ( !$this->normaliseParams( $image, $params ) ) {
52 return new TransformParameterError( $params );
54 $clientWidth = $params['width'];
55 $clientHeight = $params['height'];
56 $physicalWidth = $params['physicalWidth'];
57 $physicalHeight = $params['physicalHeight'];
58 $srcPath = $image->getPath();
60 if ( $flags & self
::TRANSFORM_LATER
) {
61 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
64 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
65 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
66 wfMsg( 'thumbnail_dest_directory' ) );
69 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
70 if( $status === true ) {
71 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
73 return $status; // MediaTransformError
78 * Transform an SVG file to PNG
79 * This function can be called outside of thumbnail contexts
80 * @param string $srcPath
81 * @param string $dstPath
82 * @param string $width
83 * @param string $height
84 * @returns TRUE/MediaTransformError
86 public function rasterize( $srcPath, $dstPath, $width, $height ) {
87 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
90 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
92 array( '$path/', '$width', '$height', '$input', '$output' ),
93 array( $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
96 wfEscapeShellArg( $srcPath ),
97 wfEscapeShellArg( $dstPath ) ),
98 $wgSVGConverters[$wgSVGConverter]
100 wfProfileIn( 'rsvg' );
101 wfDebug( __METHOD__
.": $cmd\n" );
102 $err = wfShellExec( $cmd, $retval );
103 wfProfileOut( 'rsvg' );
105 $removed = $this->removeBadFile( $dstPath, $retval );
106 if ( $retval != 0 ||
$removed ) {
107 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
108 wfHostname(), $retval, trim($err), $cmd ) );
109 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
114 function getImageSize( $image, $path ) {
115 return wfGetSVGsize( $path );
118 function getThumbType( $ext, $mime, $params = null ) {
119 return array( 'png', 'image/png' );
122 function getLongDesc( $file ) {
124 return wfMsgExt( 'svg-long-desc', 'parseinline',
125 $wgLang->formatNum( $file->getWidth() ),
126 $wgLang->formatNum( $file->getHeight() ),
127 $wgLang->formatSize( $file->getSize() ) );