r54745 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r54744‎ | r54745 | r54746 >
Date:21:02, 10 August 2009
Author:btongminh
Status:reverted (Comments)
Tags:
Comment:
Added crop support for inline images.
Syntax: [[File:foo.jpg|<width>px|<left>x<top>x<width>x<height]]
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/media/Bitmap.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/media/Bitmap.php
@@ -8,6 +8,54 @@
99 * @ingroup Media
1010 */
1111 class BitmapHandler extends ImageHandler {
 12+ function getParamMap() {
 13+ return array(
 14+ 'img_width' => 'width',
 15+ 'img_crop' => 'crop',
 16+ );
 17+ }
 18+ function validateParam( $name, $value ) {
 19+ if ( $name == 'crop' ) {
 20+ return $this->splitCropParam( $value ) !== false;
 21+ } else {
 22+ return parent::validateParam( $name, $value );
 23+ }
 24+ }
 25+ function splitCropParam( $value ) {
 26+ $parts = explode( 'x', $value );
 27+ if ( count( $parts ) > 4 )
 28+ return false;
 29+ foreach ( $parts as &$part ) {
 30+ $intVal = intval( $part );
 31+ if ( $intVal === 0 && !( $part === '0' || $part === '' ) )
 32+ return false;
 33+ if ( $intVal < 0 )
 34+ return false;
 35+
 36+ $part = $intVal;
 37+ }
 38+
 39+ return $parts;
 40+ }
 41+
 42+ function parseParamString( $str ) {
 43+ $res = parent::parseParamString( $str );
 44+ if ( $res === false ) {
 45+ $m = false;
 46+ if ( preg_match( '/^(\d+)px-([x0-9])crop$/', $str, $m ) ) {
 47+ return array( 'width' => $m[1], 'crop' => $m[2] );
 48+ } else {
 49+ return false;
 50+ }
 51+ }
 52+ }
 53+ function makeParamString( $params ) {
 54+ $res = parent::makeParamString( $params );
 55+ if ( !empty( $params['crop'] ) )
 56+ $res .= '-'.implode( 'x', $params['crop'] ).'crop';
 57+ return $res;
 58+ }
 59+
1260 function normaliseParams( $image, &$params ) {
1361 global $wgMaxImageArea;
1462 if ( !parent::normaliseParams( $image, $params ) ) {
@@ -36,6 +84,45 @@
3785 $params['physicalHeight'] = $srcHeight;
3886 return true;
3987 }
 88+
 89+ # Validate crop params
 90+ if ( isset( $params['crop'] ) ) {
 91+ # $cropParams = array( x, y, width, height );
 92+ $cropParams = $this->splitCropParam( $params['crop'] );
 93+ # Fill up params
 94+ switch ( count( $cropParams ) ) {
 95+ # All fall through
 96+ case 1:
 97+ $cropParams[1] = 0;
 98+ case 2:
 99+ $cropParams[2] = $srcWidth - $cropParams[0];
 100+ case 3:
 101+ $cropParams[3] = $srcHeight - $cropParams[1];
 102+ }
 103+ $cx = $cropParams[0] + $cropParams[2];
 104+ $cy = $cropParams[1] + $cropParams[3];
 105+ $targetWidth = $cropParams[2];
 106+ $targetHeight = $cropParams[3];
 107+ # Can't go outside image
 108+ if ( $cx > $srcWidth || $cy > $srcHeight ) {
 109+ # TODO: Maybe should fail gracefully?
 110+ return false;
 111+ }
 112+ if ( $targetWidth == $srcWidth && $targetHeight == $srcHeight )
 113+ {
 114+ # No need to crop
 115+ $params['crop'] = false;
 116+ }
 117+ else
 118+ {
 119+ header("X-Size: {$targetWidth}x{$targetHeight}");
 120+ $params['crop'] = $cropParams;
 121+ $params['height'] = $params['physicalHeight'] = File::scaleHeight(
 122+ $targetWidth, $targetHeight, $params['width'] );
 123+ }
 124+ } else {
 125+ $params['crop'] = false;
 126+ }
40127
41128 return true;
42129 }
@@ -136,6 +223,13 @@
137224 } else {
138225 $tempEnv = '';
139226 }
 227+
 228+ if ( $params['crop'] ) {
 229+ $crop = $params['crop'];
 230+ $cropCmd = "-crop {$crop[2]}x{$crop[3]}+{$crop[0]}+{$crop[1]}";
 231+ }
 232+ else
 233+ $cropCmd = '';
140234
141235 # Specify white background color, will be used for transparent images
142236 # in Internet Explorer/Windows instead of default black.
@@ -147,7 +241,7 @@
148242 $cmd =
149243 $tempEnv .
150244 wfEscapeShellArg($wgImageMagickConvertCommand) .
151 - " {$quality} -background white -size {$physicalWidth} ".
 245+ " {$cropCmd} {$quality} -background white -size {$physicalWidth} ".
152246 wfEscapeShellArg($srcPath . $frame) .
153247 $animation .
154248 // For the -resize option a "!" is needed to force exact size,
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -288,6 +288,7 @@
289289 'img_text_bottom' => array( 1, 'text-bottom' ),
290290 'img_link' => array( 1, 'link=$1' ),
291291 'img_alt' => array( 1, 'alt=$1' ),
 292+ 'img_crop' => array( 1, 'crop=$1' ),
292293 'int' => array( 0, 'INT:' ),
293294 'sitename' => array( 1, 'SITENAME' ),
294295 'ns' => array( 0, 'NS:' ),
Index: trunk/phase3/RELEASE-NOTES
@@ -192,6 +192,7 @@
193193 numbers outside the permitted ranges), etc.
194194 ** The summary attribute has been removed from tables of contents. summary is
195195 obsolete in HTML 5 and wasn't useful here anyway.
 196+* Added crop for inline images.
196197
197198 === Bug fixes in 1.16 ===
198199

Follow-up revisions

RevisionCommit summaryAuthorDate
r54746Update formatting for r54745siebrand21:14, 10 August 2009
r55302Revert image crop for now:...brion02:07, 19 August 2009

Comments

#Comment by Raymond (talk | contribs)   10:01, 11 August 2009

Does it fix bug 7757 "allow cropping images when rendered"?

Am I right that this works with ImageMagick only?

#Comment by Bryan (talk | contribs)   14:21, 11 August 2009

Yes & yes.

gd probably supports cropping, but I'm not sure if I have a working test environment for that.

#Comment by Revolus (talk | contribs)   20:13, 11 August 2009

At least gd supports copying parts of images: http://de.php.net/manual/en/function.imagecopy.php

Status & tagging log