http://www.mediawiki.org/wiki/Special:Code/MediaWiki/83779

Revision: 83779
Author:   btongminh
Date:     2011-03-12 19:59:41 +0000 (Sat, 12 Mar 2011)
Log Message:
-----------
(bug 18691) Added support for SVG rasterization using the Imagick PHP 
extension. Based on patch by Yesid Carrillo. Set $wgSVGConverter = 'ImagickExt' 
to use it. Note that the results are extremely ugly, but I believe that happens 
with the command line scaler as well.
Introduced new syntax for $wgSVGConverters, if the selected converter is an 
array, it is assumed to be a PHP callable. Imagick support is done by 
SvgHandler::rasterizeImagickExt.

Modified Paths:
--------------
    trunk/phase3/CREDITS
    trunk/phase3/RELEASE-NOTES
    trunk/phase3/includes/DefaultSettings.php
    trunk/phase3/includes/media/SVG.php

Modified: trunk/phase3/CREDITS
===================================================================
--- trunk/phase3/CREDITS        2011-03-12 19:32:39 UTC (rev 83778)
+++ trunk/phase3/CREDITS        2011-03-12 19:59:41 UTC (rev 83779)
@@ -65,6 +65,7 @@
 * Tom Gries
 * Trevor Parscal
 * Victor Vasiliev
+* Yesid Carrillo
 * Yuri Astrakhan
 
 == Patch Contributors ==

Modified: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES  2011-03-12 19:32:39 UTC (rev 83778)
+++ trunk/phase3/RELEASE-NOTES  2011-03-12 19:59:41 UTC (rev 83779)
@@ -103,6 +103,8 @@
 * When $wgAllowMicrodataAttributes is true, all itemtypes are allowed, not just
   the three that were defined in the original specification.
 * (bug 14706) Added support for the Imagick PHP extension.
+* (bug 18691) Added support for SVG rasterization using the Imagick PHP 
+  extension
 
 === Bug fixes in 1.18 ===
 * (bug 23119) WikiError class and subclasses are now marked as deprecated

Modified: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php   2011-03-12 19:32:39 UTC (rev 
83778)
+++ trunk/phase3/includes/DefaultSettings.php   2011-03-12 19:59:41 UTC (rev 
83779)
@@ -660,6 +660,8 @@
  * necessary to rasterize SVGs to PNG as a fallback format.
  *
  * An external program is required to perform this conversion.
+ * If set to an array, the first item is a PHP callable and any further items
+ * are passed as parameters after $srcPath, $dstPath, $width, $height
  */
 $wgSVGConverters = array(
        'ImageMagick' => '$path/convert -background white -thumbnail 
$widthx$height\! $input PNG:$output',
@@ -668,6 +670,7 @@
        'batik' => 'java -Djava.awt.headless=true -jar 
$path/batik-rasterizer.jar -w $width -d $output $input',
        'rsvg' => '$path/rsvg -w$width -h$height $input $output',
        'imgserv' => '$path/imgserv-wrapper -i svg -o png -w$width $input 
$output',
+       'ImagickExt' => array( 'SvgHandler::rasterizeImagickExt' ),
        );
 /** Pick a converter defined in $wgSVGConverters */
 $wgSVGConverter = 'ImageMagick';

Modified: trunk/phase3/includes/media/SVG.php
===================================================================
--- trunk/phase3/includes/media/SVG.php 2011-03-12 19:32:39 UTC (rev 83778)
+++ trunk/phase3/includes/media/SVG.php 2011-03-12 19:59:41 UTC (rev 83779)
@@ -119,19 +119,32 @@
                $err = false;
                $retval = '';
                if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
-                       $cmd = str_replace(
-                               array( '$path/', '$width', '$height', '$input', 
'$output' ),
-                               array( $wgSVGConverterPath ? wfEscapeShellArg( 
"$wgSVGConverterPath/" ) : "",
-                                          intval( $width ),
-                                          intval( $height ),
-                                          wfEscapeShellArg( $srcPath ),
-                                          wfEscapeShellArg( $dstPath ) ),
-                               $wgSVGConverters[$wgSVGConverter]
-                       ) . " 2>&1";
-                       wfProfileIn( 'rsvg' );
-                       wfDebug( __METHOD__.": $cmd\n" );
-                       $err = wfShellExec( $cmd, $retval );
-                       wfProfileOut( 'rsvg' );
+                       if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
+                               // This is a PHP callable
+                               $func = $wgSVGConverters[$wgSVGConverter][0];
+                               $args = array_merge( array( $srcPath, $dstPath, 
$width, $height ), 
+                                       array_slice( 
$wgSVGConverters[$wgSVGConverter], 1 ) );
+                               if ( !is_callable( $func ) ) {
+                                       throw new MWException( "$func is not 
callable" );
+                               }
+                               $err = call_user_func_array( $func, $args );
+                               $retval = (bool)$err;
+                       } else {
+                               // External command
+                               $cmd = str_replace(
+                                       array( '$path/', '$width', '$height', 
'$input', '$output' ),
+                                       array( $wgSVGConverterPath ? 
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
+                                                  intval( $width ),
+                                                  intval( $height ),
+                                                  wfEscapeShellArg( $srcPath ),
+                                                  wfEscapeShellArg( $dstPath ) 
),
+                                       $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 ) {
@@ -141,6 +154,20 @@
                }
                return true;
        }
+       
+       public static function rasterizeImagickExt( $srcPath, $dstPath, $width, 
$height ) {
+               $im = new Imagick( $srcPath );
+               $im->setImageFormat( 'png' );
+               $im->setBackgroundColor( 'transparent' );
+               $im->setImageDepth( 8 );
+               
+               if ( !$im->thumbnailImage( intval( $width ), intval( $height ), 
/* fit */ false ) ) {
+                       return 'Could not resize image';
+               }
+               if ( !$im->writeImage( $dstPath ) ) {
+                       return "Could not write to $dstPath";
+               }
+       }
 
        /**
         * @param $file File


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to