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