* Introduced media handler modules for file-type specific operations: thumbnailing...
[lhc/web/wiklou.git] / includes / media / SVG.php
1 <?php
2
3 class SvgHandler extends ImageHandler {
4 function isEnabled() {
5 global $wgSVGConverters, $wgSVGConverter;
6 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
7 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
8 return false;
9 } else {
10 return true;
11 }
12 }
13
14 function mustRender() {
15 return true;
16 }
17
18 function normaliseParams( $image, &$params ) {
19 global $wgSVGMaxSize;
20 if ( !parent::normaliseParams( $image, $params ) ) {
21 return false;
22 }
23
24 # Don't make an image bigger than wgMaxSVGSize
25 $params['physicalWidth'] = $params['width'];
26 $params['physicalHeight'] = $params['height'];
27 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
28 $srcWidth = $image->getWidth( $params['page'] );
29 $srcHeight = $image->getHeight( $params['page'] );
30 $params['physicalWidth'] = $wgSVGMaxSize;
31 $params['physicalHeight'] = Image::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
32 }
33 return true;
34 }
35
36 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
37 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
38
39 if ( !$this->normaliseParams( $image, $params ) ) {
40 return new TransformParameterError( $params );
41 }
42 $clientWidth = $params['width'];
43 $clientHeight = $params['height'];
44 $physicalWidth = $params['physicalWidth'];
45 $physicalHeight = $params['physicalHeight'];
46 $srcWidth = $image->getWidth();
47 $srcHeight = $image->getHeight();
48 $srcPath = $image->getImagePath();
49
50 if ( $flags & self::TRANSFORM_LATER ) {
51 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight );
52 }
53
54 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
55 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
56 wfMsg( 'thumbnail_dest_directory' ) );
57 }
58
59 $err = false;
60 if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
61 $cmd = str_replace(
62 array( '$path/', '$width', '$height', '$input', '$output' ),
63 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
64 intval( $physicalWidth ),
65 intval( $physicalHeight ),
66 wfEscapeShellArg( $srcPath ),
67 wfEscapeShellArg( $dstPath ) ),
68 $wgSVGConverters[$wgSVGConverter] ) . " 2>&1";
69 wfProfileIn( 'rsvg' );
70 wfDebug( __METHOD__.": $cmd\n" );
71 $err = wfShellExec( $cmd, $retval );
72 wfProfileOut( 'rsvg' );
73 }
74
75 $removed = $this->removeBadFile( $dstPath, $retval );
76 if ( $retval != 0 || $removed ) {
77 wfDebugLog( 'thumbnail',
78 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
79 wfHostname(), $retval, trim($err), $cmd ) );
80 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
81 } else {
82 return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight );
83 }
84 }
85
86 function getImageSize( $image, $path ) {
87 return wfGetSVGsize( $path );
88 }
89
90 function getThumbType( $ext, $mime ) {
91 return array( 'png', 'image/png' );
92 }
93 }
94 ?>