38588209b4ac9108444079e3272cf8a752c76180
[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 const SVG_METADATA_VERSION = 1;
16
17 function isEnabled() {
18 global $wgSVGConverters, $wgSVGConverter;
19 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
20 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
21 return false;
22 } else {
23 return true;
24 }
25 }
26
27 function mustRender( $file ) {
28 return true;
29 }
30
31 function isVectorized( $file ) {
32 return true;
33 }
34
35 function isAnimatedImage( $image ) {
36 # TODO: detect animated SVGs
37 return false;
38 }
39
40 function normaliseParams( $image, &$params ) {
41 global $wgSVGMaxSize;
42 if ( !parent::normaliseParams( $image, $params ) ) {
43 return false;
44 }
45 # Don't make an image bigger than wgMaxSVGSize
46 $params['physicalWidth'] = $params['width'];
47 $params['physicalHeight'] = $params['height'];
48 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
49 $srcWidth = $image->getWidth( $params['page'] );
50 $srcHeight = $image->getHeight( $params['page'] );
51 $params['physicalWidth'] = $wgSVGMaxSize;
52 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
53 }
54 return true;
55 }
56
57 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
58 if ( !$this->normaliseParams( $image, $params ) ) {
59 return new TransformParameterError( $params );
60 }
61 $clientWidth = $params['width'];
62 $clientHeight = $params['height'];
63 $physicalWidth = $params['physicalWidth'];
64 $physicalHeight = $params['physicalHeight'];
65 $srcPath = $image->getPath();
66
67 if ( $flags & self::TRANSFORM_LATER ) {
68 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
69 }
70
71 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
72 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
73 wfMsg( 'thumbnail_dest_directory' ) );
74 }
75
76 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
77 if( $status === true ) {
78 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
79 } else {
80 return $status; // MediaTransformError
81 }
82 }
83
84 /*
85 * Transform an SVG file to PNG
86 * This function can be called outside of thumbnail contexts
87 * @param string $srcPath
88 * @param string $dstPath
89 * @param string $width
90 * @param string $height
91 * @returns TRUE/MediaTransformError
92 */
93 public function rasterize( $srcPath, $dstPath, $width, $height ) {
94 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
95 $err = false;
96 $retval = '';
97 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
98 $cmd = str_replace(
99 array( '$path/', '$width', '$height', '$input', '$output' ),
100 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
101 intval( $width ),
102 intval( $height ),
103 wfEscapeShellArg( $srcPath ),
104 wfEscapeShellArg( $dstPath ) ),
105 $wgSVGConverters[$wgSVGConverter]
106 ) . " 2>&1";
107 wfProfileIn( 'rsvg' );
108 wfDebug( __METHOD__.": $cmd\n" );
109 $err = wfShellExec( $cmd, $retval );
110 wfProfileOut( 'rsvg' );
111 }
112 $removed = $this->removeBadFile( $dstPath, $retval );
113 if ( $retval != 0 || $removed ) {
114 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
115 wfHostname(), $retval, trim($err), $cmd ) );
116 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
117 }
118 return true;
119 }
120
121 function getImageSize( $file, $path, $metadata = false ) {
122 if ( $metadata === false ) {
123 $metadata = $file->getMetaData();
124 }
125 $metadata = $this->unpackMetaData( $metadata );
126
127 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
128 return array( $metadata['width'], $metadata['height'], 'SVG',
129 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
130 }
131 }
132
133 function getThumbType( $ext, $mime, $params = null ) {
134 return array( 'png', 'image/png' );
135 }
136
137 function getLongDesc( $file ) {
138 global $wgLang;
139 return wfMsgExt( 'svg-long-desc', 'parseinline',
140 $wgLang->formatNum( $file->getWidth() ),
141 $wgLang->formatNum( $file->getHeight() ),
142 $wgLang->formatSize( $file->getSize() ) );
143 }
144
145 function formatMetadata( $file ) {
146 return false;
147 }
148
149 function getMetadata( $file, $filename ) {
150 $metadata = array();
151 try {
152 $metadata = SVGMetadataExtractor::getMetadata( $filename );
153 } catch( Exception $e ) {
154 // Broken file?
155 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
156 return '0';
157 }
158 $metadata['version'] = self::SVG_METADATA_VERSION;
159 return serialize( $metadata );
160 }
161
162 function unpackMetadata( $metadata ) {
163 $unser = @unserialize( $metadata );
164 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
165 return $unser;
166 } else {
167 return false;
168 }
169 }
170
171 function getMetadataType( $image ) {
172 return 'parsed-svg';
173 }
174
175 function isMetadataValid( $image, $metadata ) {
176 return $this->unpackMetadata( $metadata ) !== false;
177 }
178 }