00001 <?php
00010 class SvgHandler extends ImageHandler {
00011 function isEnabled() {
00012 global $wgSVGConverters, $wgSVGConverter;
00013 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
00014 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
00015 return false;
00016 } else {
00017 return true;
00018 }
00019 }
00020
00021 function mustRender( $file ) {
00022 return true;
00023 }
00024
00025 function normaliseParams( $image, &$params ) {
00026 global $wgSVGMaxSize;
00027 if ( !parent::normaliseParams( $image, $params ) ) {
00028 return false;
00029 }
00030 # Don't make an image bigger than wgMaxSVGSize
00031 $params['physicalWidth'] = $params['width'];
00032 $params['physicalHeight'] = $params['height'];
00033 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
00034 $srcWidth = $image->getWidth( $params['page'] );
00035 $srcHeight = $image->getHeight( $params['page'] );
00036 $params['physicalWidth'] = $wgSVGMaxSize;
00037 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
00038 }
00039 return true;
00040 }
00041
00042 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
00043 if ( !$this->normaliseParams( $image, $params ) ) {
00044 return new TransformParameterError( $params );
00045 }
00046 $clientWidth = $params['width'];
00047 $clientHeight = $params['height'];
00048 $physicalWidth = $params['physicalWidth'];
00049 $physicalHeight = $params['physicalHeight'];
00050 $srcPath = $image->getPath();
00051
00052 if ( $flags & self::TRANSFORM_LATER ) {
00053 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
00054 }
00055
00056 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
00057 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
00058 wfMsg( 'thumbnail_dest_directory' ) );
00059 }
00060
00061 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
00062 if( $status === true ) {
00063 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
00064 } else {
00065 return $status;
00066 }
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 public function rasterize( $srcPath, $dstPath, $width, $height ) {
00079 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
00080 $err = false;
00081 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
00082 $cmd = str_replace(
00083 array( '$path/', '$width', '$height', '$input', '$output' ),
00084 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
00085 intval( $width ),
00086 intval( $height ),
00087 wfEscapeShellArg( $srcPath ),
00088 wfEscapeShellArg( $dstPath ) ),
00089 $wgSVGConverters[$wgSVGConverter]
00090 ) . " 2>&1";
00091 wfProfileIn( 'rsvg' );
00092 wfDebug( __METHOD__.": $cmd\n" );
00093 $err = wfShellExec( $cmd, $retval );
00094 wfProfileOut( 'rsvg' );
00095 }
00096 $removed = $this->removeBadFile( $dstPath, $retval );
00097 if ( $retval != 0 || $removed ) {
00098 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
00099 wfHostname(), $retval, trim($err), $cmd ) );
00100 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
00101 }
00102 return true;
00103 }
00104
00105 function getImageSize( $image, $path ) {
00106 return wfGetSVGsize( $path );
00107 }
00108
00109 function getThumbType( $ext, $mime ) {
00110 return array( 'png', 'image/png' );
00111 }
00112
00113 function getLongDesc( $file ) {
00114 global $wgLang;
00115 return wfMsgExt( 'svg-long-desc', 'parseinline',
00116 $wgLang->formatNum( $file->getWidth() ),
00117 $wgLang->formatNum( $file->getHeight() ),
00118 $wgLang->formatSize( $file->getSize() ) );
00119 }
00120 }