Refactor SvgHandler to have transformSvgToPng() function, which can be used for non...
authorAaron Schulz <aaron@users.mediawiki.org>
Tue, 18 Nov 2008 01:18:12 +0000 (01:18 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Tue, 18 Nov 2008 01:18:12 +0000 (01:18 +0000)
includes/media/SVG.php

index 2604e3b..978749d 100644 (file)
@@ -27,7 +27,6 @@ class SvgHandler extends ImageHandler {
                if ( !parent::normaliseParams( $image, $params ) ) {
                        return false;
                }
-
                # Don't make an image bigger than wgMaxSVGSize
                $params['physicalWidth'] = $params['width'];
                $params['physicalHeight'] = $params['height'];
@@ -60,32 +59,49 @@ class SvgHandler extends ImageHandler {
                        return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
                                wfMsg( 'thumbnail_dest_directory' ) );
                }
-
+               
+               $status = $this->transformSvgToPng( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
+               if( $status === true ) {
+                       return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
+               } else {
+                       return $status; // MediaTransformError
+               }
+       }
+       
+       /*
+       * Transform an SVG file to PNG
+       * This function can be called outside of thumbnail contexts
+       * @param string $srcPath
+       * @param string $dstPath
+       * @param string $width
+       * @param string $height
+       * @returns TRUE/MediaTransformError
+       */
+       public function transformSvgToPng( $srcPath, $dstPath, $width, $height ) {
+               global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
                $err = false;
-               if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
+               if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
                        $cmd = str_replace(
                                array( '$path/', '$width', '$height', '$input', '$output' ),
                                array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
-                                          intval( $physicalWidth ),
-                                          intval( $physicalHeight ),
+                                          intval( $width ),
+                                          intval( $height ),
                                           wfEscapeShellArg( $srcPath ),
                                           wfEscapeShellArg( $dstPath ) ),
-                               $wgSVGConverters[$wgSVGConverter] ) . " 2>&1";
+                               $wgSVGConverters[$wgSVGConverter]
+                       ) . " 2>&1";
                        wfProfileIn( 'rsvg' );
                        wfDebug( __METHOD__.": $cmd\n" );
                        $err = wfShellExec( $cmd, $retval );
                        wfProfileOut( 'rsvg' );
                }
-
                $removed = $this->removeBadFile( $dstPath, $retval );
                if ( $retval != 0 || $removed ) {
-                       wfDebugLog( 'thumbnail',
-                               sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
+                       wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
                                        wfHostname(), $retval, trim($err), $cmd ) );
                        return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
-               } else {
-                       return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
+               return true;
        }
 
        function getImageSize( $image, $path ) {