r83779 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83778‎ | r83779 | r83780 >
Date:19:59, 12 March 2011
Author:btongminh
Status:ok
Tags:
Comment:
(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 (modified) (history)
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/media/SVG.php (modified) (history)

Diff [purge]

Index: trunk/phase3/CREDITS
@@ -65,6 +65,7 @@
6666 * Tom Gries
6767 * Trevor Parscal
6868 * Victor Vasiliev
 69+* Yesid Carrillo
6970 * Yuri Astrakhan
7071
7172 == Patch Contributors ==
Index: trunk/phase3/includes/media/SVG.php
@@ -119,19 +119,32 @@
120120 $err = false;
121121 $retval = '';
122122 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
123 - $cmd = str_replace(
124 - array( '$path/', '$width', '$height', '$input', '$output' ),
125 - array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
126 - intval( $width ),
127 - intval( $height ),
128 - wfEscapeShellArg( $srcPath ),
129 - wfEscapeShellArg( $dstPath ) ),
130 - $wgSVGConverters[$wgSVGConverter]
131 - ) . " 2>&1";
132 - wfProfileIn( 'rsvg' );
133 - wfDebug( __METHOD__.": $cmd\n" );
134 - $err = wfShellExec( $cmd, $retval );
135 - wfProfileOut( 'rsvg' );
 123+ if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
 124+ // This is a PHP callable
 125+ $func = $wgSVGConverters[$wgSVGConverter][0];
 126+ $args = array_merge( array( $srcPath, $dstPath, $width, $height ),
 127+ array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
 128+ if ( !is_callable( $func ) ) {
 129+ throw new MWException( "$func is not callable" );
 130+ }
 131+ $err = call_user_func_array( $func, $args );
 132+ $retval = (bool)$err;
 133+ } else {
 134+ // External command
 135+ $cmd = str_replace(
 136+ array( '$path/', '$width', '$height', '$input', '$output' ),
 137+ array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
 138+ intval( $width ),
 139+ intval( $height ),
 140+ wfEscapeShellArg( $srcPath ),
 141+ wfEscapeShellArg( $dstPath ) ),
 142+ $wgSVGConverters[$wgSVGConverter]
 143+ ) . " 2>&1";
 144+ wfProfileIn( 'rsvg' );
 145+ wfDebug( __METHOD__.": $cmd\n" );
 146+ $err = wfShellExec( $cmd, $retval );
 147+ wfProfileOut( 'rsvg' );
 148+ }
136149 }
137150 $removed = $this->removeBadFile( $dstPath, $retval );
138151 if ( $retval != 0 || $removed ) {
@@ -141,6 +154,20 @@
142155 }
143156 return true;
144157 }
 158+
 159+ public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
 160+ $im = new Imagick( $srcPath );
 161+ $im->setImageFormat( 'png' );
 162+ $im->setBackgroundColor( 'transparent' );
 163+ $im->setImageDepth( 8 );
 164+
 165+ if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
 166+ return 'Could not resize image';
 167+ }
 168+ if ( !$im->writeImage( $dstPath ) ) {
 169+ return "Could not write to $dstPath";
 170+ }
 171+ }
145172
146173 /**
147174 * @param $file File
Index: trunk/phase3/includes/DefaultSettings.php
@@ -660,6 +660,8 @@
661661 * necessary to rasterize SVGs to PNG as a fallback format.
662662 *
663663 * An external program is required to perform this conversion.
 664+ * If set to an array, the first item is a PHP callable and any further items
 665+ * are passed as parameters after $srcPath, $dstPath, $width, $height
664666 */
665667 $wgSVGConverters = array(
666668 'ImageMagick' => '$path/convert -background white -thumbnail $widthx$height\! $input PNG:$output',
@@ -668,6 +670,7 @@
669671 'batik' => 'java -Djava.awt.headless=true -jar $path/batik-rasterizer.jar -w $width -d $output $input',
670672 'rsvg' => '$path/rsvg -w$width -h$height $input $output',
671673 'imgserv' => '$path/imgserv-wrapper -i svg -o png -w$width $input $output',
 674+ 'ImagickExt' => array( 'SvgHandler::rasterizeImagickExt' ),
672675 );
673676 /** Pick a converter defined in $wgSVGConverters */
674677 $wgSVGConverter = 'ImageMagick';
Index: trunk/phase3/RELEASE-NOTES
@@ -103,6 +103,8 @@
104104 * When $wgAllowMicrodataAttributes is true, all itemtypes are allowed, not just
105105 the three that were defined in the original specification.
106106 * (bug 14706) Added support for the Imagick PHP extension.
 107+* (bug 18691) Added support for SVG rasterization using the Imagick PHP
 108+ extension
107109
108110 === Bug fixes in 1.18 ===
109111 * (bug 23119) WikiError class and subclasses are now marked as deprecated

Status & tagging log