merging latest master
[lhc/web/wiklou.git] / includes / media / SVG.php
index d425b94..9846c71 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * Handler for SVG images.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Media
  */
@@ -58,14 +73,21 @@ 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'];
-               if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
-                       $srcWidth = $image->getWidth( $params['page'] );
-                       $srcHeight = $image->getHeight( $params['page'] );
-                       $params['physicalWidth'] = $wgSVGMaxSize;
-                       $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
+               # Don't make an image bigger than wgMaxSVGSize on the smaller side
+               if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
+                       if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
+                               $srcWidth = $image->getWidth( $params['page'] );
+                               $srcHeight = $image->getHeight( $params['page'] );
+                               $params['physicalWidth'] = $wgSVGMaxSize;
+                               $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
+                       }
+               } else {
+                       if ( $params['physicalHeight'] > $wgSVGMaxSize ) {
+                               $srcWidth = $image->getWidth( $params['page'] );
+                               $srcHeight = $image->getHeight( $params['page'] );
+                               $params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
+                               $params['physicalHeight'] = $wgSVGMaxSize;
+                       }
                }
                return true;
        }
@@ -86,17 +108,17 @@ class SvgHandler extends ImageHandler {
                $clientHeight = $params['height'];
                $physicalWidth = $params['physicalWidth'];
                $physicalHeight = $params['physicalHeight'];
-               $srcPath = $image->getPath();
 
                if ( $flags & self::TRANSFORM_LATER ) {
                        return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
 
-               if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
+               if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
                        return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
                                wfMsg( 'thumbnail_dest_directory' ) );
                }
 
+               $srcPath = $image->getLocalRefPath();
                $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
                if( $status === true ) {
                        return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
@@ -105,33 +127,46 @@ class SvgHandler extends ImageHandler {
                }
        }
 
-       /*
+       /**
        * 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
+       * @return bool|MediaTransformError
        */
        public function rasterize( $srcPath, $dstPath, $width, $height ) {
                global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
                $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 ) {
@@ -142,6 +177,20 @@ class SvgHandler extends ImageHandler {
                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
         * @param  $path
@@ -189,7 +238,9 @@ class SvgHandler extends ImageHandler {
        }
 
        function unpackMetadata( $metadata ) {
-               $unser = @unserialize( $metadata );
+               wfSuppressWarnings();
+               $unser = unserialize( $metadata );
+               wfRestoreWarnings();
                if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
                        return $unser;
                } else {