Added crop support for inline images.
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Mon, 10 Aug 2009 21:02:24 +0000 (21:02 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Mon, 10 Aug 2009 21:02:24 +0000 (21:02 +0000)
Syntax: [[File:foo.jpg|<width>px|<left>x<top>x<width>x<height]]

RELEASE-NOTES
includes/media/Bitmap.php
languages/messages/MessagesEn.php

index 92d8090..7e81183 100644 (file)
@@ -192,6 +192,7 @@ this. Was used when mwEmbed was going to be an extension.
    numbers outside the permitted ranges), etc.
 ** The summary attribute has been removed from tables of contents.  summary is
    obsolete in HTML 5 and wasn't useful here anyway.
+* Added crop for inline images.
 
 === Bug fixes in 1.16 ===
 
index d4a7c35..b3a0c8a 100644 (file)
@@ -8,6 +8,54 @@
  * @ingroup Media
  */
 class BitmapHandler extends ImageHandler {
+       function getParamMap() {
+               return array(
+                       'img_width'     => 'width',
+                       'img_crop' => 'crop',
+               );
+       }
+       function validateParam( $name, $value ) {
+               if ( $name == 'crop' ) {
+                       return $this->splitCropParam( $value ) !== false;
+               } else {
+                       return parent::validateParam( $name, $value );
+               }               
+       }
+       function splitCropParam( $value ) {
+               $parts = explode( 'x', $value );
+               if ( count( $parts ) > 4 )
+                       return false;
+               foreach ( $parts as &$part ) {
+                       $intVal = intval( $part );
+                       if ( $intVal === 0 && !( $part === '0' || $part === '' ) )
+                               return false;
+                       if ( $intVal < 0 )
+                               return false;
+                       
+                       $part = $intVal;
+               }
+               
+               return $parts;
+       }
+       
+       function parseParamString( $str ) {
+               $res = parent::parseParamString( $str );
+               if ( $res === false ) {
+                       $m = false;
+                       if ( preg_match( '/^(\d+)px-([x0-9])crop$/', $str, $m ) ) {
+                               return array( 'width' => $m[1], 'crop' => $m[2] );
+                       } else {
+                               return false;
+                       }
+               }
+       }
+       function makeParamString( $params ) { 
+               $res = parent::makeParamString( $params );
+               if ( !empty( $params['crop'] ) )
+                       $res .= '-'.implode( 'x', $params['crop'] ).'crop';
+               return $res;
+       }
+       
        function normaliseParams( $image, &$params ) {
                global $wgMaxImageArea;
                if ( !parent::normaliseParams( $image, $params ) ) {
@@ -36,6 +84,45 @@ class BitmapHandler extends ImageHandler {
                        $params['physicalHeight'] = $srcHeight;
                        return true;
                }
+               
+               # Validate crop params
+               if ( isset( $params['crop'] ) ) {
+                       # $cropParams = array( x, y, width, height );
+                       $cropParams = $this->splitCropParam( $params['crop'] );
+                       # Fill up params
+                       switch ( count( $cropParams ) ) {
+                               # All fall through
+                               case 1:
+                                       $cropParams[1] = 0;
+                               case 2:
+                                       $cropParams[2] = $srcWidth - $cropParams[0];
+                               case 3:
+                                       $cropParams[3] = $srcHeight - $cropParams[1];
+                       }
+                       $cx = $cropParams[0] + $cropParams[2];
+                       $cy = $cropParams[1] + $cropParams[3];
+                       $targetWidth = $cropParams[2];
+                       $targetHeight = $cropParams[3];
+                       # Can't go outside image
+                       if ( $cx > $srcWidth || $cy > $srcHeight ) {
+                               # TODO: Maybe should fail gracefully?
+                               return false; 
+                       }
+                       if ( $targetWidth == $srcWidth && $targetHeight == $srcHeight )
+                       {
+                               # No need to crop
+                               $params['crop'] = false;
+                       }
+                       else
+                       {
+                               header("X-Size: {$targetWidth}x{$targetHeight}");
+                               $params['crop'] = $cropParams;
+                               $params['height'] = $params['physicalHeight'] = File::scaleHeight( 
+                                               $targetWidth, $targetHeight, $params['width'] );
+                       }
+               } else {
+                       $params['crop'] = false;
+               }
 
                return true;
        }
@@ -136,6 +223,13 @@ class BitmapHandler extends ImageHandler {
                        } else {
                                $tempEnv = '';
                        }
+                       
+                       if ( $params['crop'] ) {
+                               $crop = $params['crop'];
+                               $cropCmd = "-crop {$crop[2]}x{$crop[3]}+{$crop[0]}+{$crop[1]}";
+                       }
+                       else
+                               $cropCmd = '';
 
                        # Specify white background color, will be used for transparent images
                        # in Internet Explorer/Windows instead of default black.
@@ -147,7 +241,7 @@ class BitmapHandler extends ImageHandler {
                        $cmd  = 
                                $tempEnv .
                                wfEscapeShellArg($wgImageMagickConvertCommand) .
-                               " {$quality} -background white -size {$physicalWidth} ".
+                               " {$cropCmd} {$quality} -background white -size {$physicalWidth} ".
                                wfEscapeShellArg($srcPath . $frame) .
                                $animation .
                                // For the -resize option a "!" is needed to force exact size,
index afbb1fa..9ad8fb0 100644 (file)
@@ -288,6 +288,7 @@ $magicWords = array(
        'img_text_bottom'        => array( 1,    'text-bottom'            ),
        'img_link'               => array( 1,    'link=$1'                ),
        'img_alt'                => array( 1,    'alt=$1'                 ),
+       'img_crop'                               => array( 1,    'crop=$1'                                ),
        'int'                    => array( 0,    'INT:'                   ),
        'sitename'               => array( 1,    'SITENAME'               ),
        'ns'                     => array( 0,    'NS:'                    ),