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 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
00044
00045 if ( !$this->normaliseParams( $image, $params ) ) {
00046 return new TransformParameterError( $params );
00047 }
00048 $clientWidth = $params['width'];
00049 $clientHeight = $params['height'];
00050 $physicalWidth = $params['physicalWidth'];
00051 $physicalHeight = $params['physicalHeight'];
00052 $srcPath = $image->getPath();
00053
00054 if ( $flags & self::TRANSFORM_LATER ) {
00055 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
00056 }
00057
00058 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
00059 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
00060 wfMsg( 'thumbnail_dest_directory' ) );
00061 }
00062
00063 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
00064 if( $status === true ) {
00065 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
00066 } else {
00067 return $status;
00068 }
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public function rasterize( $srcPath, $dstPath, $width, $height ) {
00081 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
00082 $err = false;
00083 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
00084 $cmd = str_replace(
00085 array( '$path/', '$width', '$height', '$input', '$output' ),
00086 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
00087 intval( $width ),
00088 intval( $height ),
00089 wfEscapeShellArg( $srcPath ),
00090 wfEscapeShellArg( $dstPath ) ),
00091 $wgSVGConverters[$wgSVGConverter]
00092 ) . " 2>&1";
00093 wfProfileIn( 'rsvg' );
00094 wfDebug( __METHOD__.": $cmd\n" );
00095 $err = wfShellExec( $cmd, $retval );
00096 wfProfileOut( 'rsvg' );
00097 }
00098 $removed = $this->removeBadFile( $dstPath, $retval );
00099 if ( $retval != 0 || $removed ) {
00100 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
00101 wfHostname(), $retval, trim($err), $cmd ) );
00102 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
00103 }
00104 return true;
00105 }
00106
00107 function getImageSize( $image, $path ) {
00108 return wfGetSVGsize( $path );
00109 }
00110
00111 function getThumbType( $ext, $mime ) {
00112 return array( 'png', 'image/png' );
00113 }
00114
00115 function getLongDesc( $file ) {
00116 global $wgLang;
00117 return wfMsgExt( 'svg-long-desc', 'parseinline',
00118 $wgLang->formatNum( $file->getWidth() ),
00119 $wgLang->formatNum( $file->getHeight() ),
00120 $wgLang->formatSize( $file->getSize() ) );
00121 }
00122 }