69a36f31e6fcc1e5aa388e50aa0891c17c9fd61c
[lhc/web/wiklou.git] / includes / media / SVG.php
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
6
7 /**
8 * @ingroup Media
9 */
10 class SvgHandler extends ImageHandler {
11 function isEnabled() {
12 global $wgSVGConverters, $wgSVGConverter;
13 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
14 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
15 return false;
16 } else {
17 return true;
18 }
19 }
20
21 function mustRender( $file ) {
22 return true;
23 }
24
25 function normaliseParams( $image, &$params ) {
26 global $wgSVGMaxSize;
27 if ( !parent::normaliseParams( $image, $params ) ) {
28 return false;
29 }
30 # Don't make an image bigger than wgMaxSVGSize
31 $params['physicalWidth'] = $params['width'];
32 $params['physicalHeight'] = $params['height'];
33 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
34 $srcWidth = $image->getWidth( $params['page'] );
35 $srcHeight = $image->getHeight( $params['page'] );
36 $params['physicalWidth'] = $wgSVGMaxSize;
37 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
38 }
39 return true;
40 }
41
42 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
43 if ( !$this->normaliseParams( $image, $params ) ) {
44 return new TransformParameterError( $params );
45 }
46 $clientWidth = $params['width'];
47 $clientHeight = $params['height'];
48 $physicalWidth = $params['physicalWidth'];
49 $physicalHeight = $params['physicalHeight'];
50 $srcPath = $image->getPath();
51
52 if ( $flags & self::TRANSFORM_LATER ) {
53 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
54 }
55
56 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
57 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
58 wfMsg( 'thumbnail_dest_directory' ) );
59 }
60
61 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
62 if( $status === true ) {
63 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
64 } else {
65 return $status; // MediaTransformError
66 }
67 }
68
69 /*
70 * Transform an SVG file to PNG
71 * This function can be called outside of thumbnail contexts
72 * @param string $srcPath
73 * @param string $dstPath
74 * @param string $width
75 * @param string $height
76 * @returns TRUE/MediaTransformError
77 */
78 public function rasterize( $srcPath, $dstPath, $width, $height ) {
79 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
80 $err = false;
81 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
82 $cmd = str_replace(
83 array( '$path/', '$width', '$height', '$input', '$output' ),
84 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
85 intval( $width ),
86 intval( $height ),
87 wfEscapeShellArg( $srcPath ),
88 wfEscapeShellArg( $dstPath ) ),
89 $wgSVGConverters[$wgSVGConverter]
90 ) . " 2>&1";
91 wfProfileIn( 'rsvg' );
92 wfDebug( __METHOD__.": $cmd\n" );
93 $err = wfShellExec( $cmd, $retval );
94 wfProfileOut( 'rsvg' );
95 }
96 $removed = $this->removeBadFile( $dstPath, $retval );
97 if ( $retval != 0 || $removed ) {
98 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
99 wfHostname(), $retval, trim($err), $cmd ) );
100 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
101 }
102 return true;
103 }
104
105 function getImageSize( $image, $path ) {
106 return wfGetSVGsize( $path );
107 }
108
109 function getThumbType( $ext, $mime, $params = null ) {
110 return array( 'png', 'image/png' );
111 }
112
113 function getLongDesc( $file ) {
114 global $wgLang;
115 return wfMsgExt( 'svg-long-desc', 'parseinline',
116 $wgLang->formatNum( $file->getWidth() ),
117 $wgLang->formatNum( $file->getHeight() ),
118 $wgLang->formatSize( $file->getSize() ) );
119 }
120 }