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