r101715 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101714‎ | r101715 | r101716 >
Date:21:48, 2 November 2011
Author:hashar
Status:ok
Tags:
Comment:
white space cleaning

Did that while reading the various files.
Added some phpdoc comments
Added license

No code change!
Modified paths:
  • /trunk/extensions/VipsScaler/SpecialVipsTest.php (modified) (history)
  • /trunk/extensions/VipsScaler/VipsScaler.php (modified) (history)
  • /trunk/extensions/VipsScaler/VipsScaler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/VipsScaler/VipsScaler.php
@@ -16,6 +16,8 @@
1717 * with this program; if not, write to the Free Software Foundation, Inc.,
1818 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919 * http://www.gnu.org/copyleft/gpl.html
 20+ *
 21+ * @file
2022 */
2123
2224 $wgExtensionCredits['media'][] = array(
@@ -43,7 +45,7 @@
4446 * with vips. Conditions are mimeType, minArea, maxArea, minShrinkFactor,
4547 * maxShrinkFactor. The other items in the array are options. Options available
4648 * are:
47 - * - sharpen: Set to an array with keys 'radius' and 'sigma', which are
 49+ * - sharpen: Set to an array with keys 'radius' and 'sigma', which are
4850 * parameters to gaussian sharpen matrix.
4951 * - preconvert: Convert the file to a .v file first, which costs some space,
5052 * but saves memory on the actual downsize
@@ -74,7 +76,7 @@
7577 'conditions' => array(
7678 'mimeType' => 'image/png',
7779 ),
78 - ),
 80+ ),
7981 );
8082
8183
Index: trunk/extensions/VipsScaler/SpecialVipsTest.php
@@ -1,4 +1,29 @@
22 <?php
 3+/*
 4+ * Copyright © Bryan Tong Minh, 2011
 5+ *
 6+ * This program is free software; you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation; either version 2 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License along
 17+ * with this program; if not, write to the Free Software Foundation, Inc.,
 18+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 19+ * http://www.gnu.org/copyleft/gpl.html
 20+ *
 21+ * @file
 22+ */
 23+
 24+/**
 25+ * A Special page intended to test the Vipscaler.
 26+ * @author Bryan Tong Minh
 27+ */
328 class SpecialVipsTest extends SpecialPage {
429 public function __construct() {
530 parent::__construct( 'VipsTest', 'vipstest' );
@@ -6,9 +31,11 @@
732
833 /**
934 * Entry point
 35+ * @param $par Array TODO describe what is expected there
1036 */
1137 public function execute( $par ) {
1238 $request = $this->getRequest();
 39+
1340 if ( $request->getText( 'thumb' ) && $request->getText( 'file' ) ) {
1441 $this->streamThumbnail();
1542 } elseif ( $par || $request->getText( 'file' ) ) {
@@ -18,77 +45,84 @@
1946 $this->showForm();
2047 }
2148 }
22 -
 49+
 50+ /**
 51+ */
2352 protected function showThumbnails() {
2453 $request = $this->getRequest();
25 -
 54+
2655 $title = Title::makeTitleSafe( NS_FILE, $request->getText( 'file' ) );
2756 if ( is_null( $title ) ) {
2857 $this->getOutput()->addWikiMsg( 'vips-invalid-file' );
2958 return;
30 - }
 59+ }
3160 $file = wfFindFile( $title );
3261 if ( !$file || !$file->exists() ) {
3362 $this->getOutput()->addWikiMsg( 'vips-invalid-file' );
3463 return;
3564 }
36 -
 65+
3766 $width = $request->getInt( 'width' );
3867 if ( !$width ) {
3968 $this->getOutput()->addWikiMsg( 'vips-invalid-width' );
4069 return;
4170 }
42 -
 71+
4372 $params = array( 'width' => $width );
4473 $thumb = $file->transform( $params );
4574 if ( !$thumb || $thumb->isError() ) {
4675 $this->getOutput()->addWikiMsg( 'vips-thumb-error' );
4776 }
48 -
 77+
4978 $vipsThumbUrl = $this->makeUrl( $file, $width );
50 -
51 -
 79+
5280 }
53 -
 81+
 82+ /**
 83+ * TODO
 84+ */
5485 protected function showForm() {
55 -
 86+
5687 }
57 -
 88+
 89+ /**
 90+ *
 91+ */
5892 protected function streamThumbnail() {
5993 global $wgVipsThumbnailerUrl;
60 -
 94+
6195 $request = $this->getRequest();
62 -
 96+
6397 # Validate title and file existance
6498 $title = Title::makeTitleSafe( NS_FILE, $request->getText( 'file' ) );
6599 if ( is_null( $title ) ) {
66100 return $this->streamError( 404 );
67 - }
 101+ }
68102 $file = wfFindFile( $title );
69103 if ( !$file || !$file->exists() ) {
70104 return $this->streamError( 404 );
71105 }
72 -
 106+
73107 # Check if vips can handle this file
74108 if ( VipsScaler::getVipsHandler( $file ) === false ) {
75109 return $this->streamError( 500 );
76110 }
77 -
 111+
78112 # Validate param string
79113 $handler = $file->getHandler();
80114 $params = $handler->parseParamString( $request->getText( 'thumb' ) );
81115 if ( !$handler->normaliseParams( $file, $params ) ) {
82116 return $this->streamError( 500 );
83117 }
84 -
 118+
85119 # Get the thumbnail
86120 if ( is_null( $wgVipsThumbnailerUrl ) ) {
87121 # No remote scaler, need to do it ourselves.
88122 # Emulate the BitmapHandlerTransform hook
89 -
 123+
90124 $dstPath = VipsCommand::makeTemp( strrchr( $file->getName(), '.' ) );
91125 $dstUrl = '';
92 -
 126+
93127 $scalerParams = array(
94128 # The size to which the image will be resized
95129 'physicalWidth' => $params['physicalWidth'],
@@ -108,8 +142,8 @@
109143 'dstPath' => $dstPath,
110144 'dstUrl' => $dstUrl,
111145 );
112 -
113 -
 146+
 147+
114148 # Call the hook
115149 $mto = null;
116150 if ( VipsScaler::onTransform( $handler, $file, $params, $mto ) ) {
@@ -117,16 +151,16 @@
118152 } else {
119153 $this->streamError( 500 );
120154 }
121 -
 155+
122156 # Cleanup the temporary file
123157 wfSuppressWarnings();
124158 unlink( $dstPath );
125159 wfRestoreWarning();
126 -
 160+
127161 } else {
128162 # Request the thumbnail at a remote scaler
129163 global $wgVipsThumbnailerProxy;
130 -
 164+
131165 $url = wfAppendQuery( $wgVipsThumbnailerUrl, array(
132166 'file' => $file->getName(),
133167 'thumb' => $handler->makeParamString( $params ) . '-' . $file->getName()
@@ -135,7 +169,7 @@
136170 if ( $wgVipsThumbnailerProxy ) {
137171 $options['proxy'] = $wgVipsThumbnailerProxy;
138172 }
139 -
 173+
140174 $req = MWHttpRequest::factory( $url, $options );
141175 $status = $req->execute();
142176 if ( $status->isOk() ) {
@@ -146,18 +180,21 @@
147181 print "\r\n";
148182 print $req->getContent();
149183 } else {
150 - return $this->streamError( 500 );
 184+ return $this->streamError( 500 );
151185 }
152 -
 186+
153187 }
154188 }
155 -
156 - protected function makeUrl( ) {
157 -
 189+
 190+ /**
 191+ *
 192+ */
 193+ protected function makeUrl( $file, $width ) {
 194+
158195 }
159 -
 196+
160197 protected function streamError( $code ) {
161 -
 198+
162199 }
163 -
164 -}
\ No newline at end of file
 200+
 201+}
Index: trunk/extensions/VipsScaler/VipsScaler_body.php
@@ -1,9 +1,39 @@
22 <?php
 3+/**
 4+ * PHP wrapper class for VIPS under MediaWiki
 5+ *
 6+ * Copyright © Bryan Tong Minh, 2011
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * You should have received a copy of the GNU General Public License along
 19+ * with this program; if not, write to the Free Software Foundation, Inc.,
 20+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 21+ * http://www.gnu.org/copyleft/gpl.html
 22+ * @file
 23+ */
 24+
 25+/**
 26+ * Wrapper class for VIPS, a free image processing system good at handling
 27+ * large pictures.
 28+ *
 29+ * http://www.vips.ecs.soton.ac.uk/
 30+ *
 31+ * @author Bryan Tong Minh
 32+ */
333 class VipsScaler {
434 /**
535 * Hook to BitmapHandlerTransform. Transforms the file using VIPS if it
636 * matches a condition in $wgVipsConditions
7 - *
 37+ *
838 * @param BitmapHandler $handler
939 * @param File $file
1040 * @param array $params
@@ -15,16 +45,16 @@
1646 if ( !$options ) {
1747 return true;
1848 }
19 -
 49+
2050 wfDebug( __METHOD__ . ': scaling ' . $file->getName() . " using vips\n" );
21 -
 51+
2252 $vipsCommands = self::makeCommands( $handler, $file, $params, $options );
2353 if ( count( $vipsCommands ) == 0 ) {
2454 return true;
2555 }
26 -
 56+
2757 # Execute the commands
28 - foreach ( $vipsCommands as $i => $command ) {
 58+ foreach ( $vipsCommands as $i => $command ) {
2959 # Set input/output files
3060 if ( $i == 0 && count( $vipsCommands ) == 1 ) {
3161 # Single command, so output directly to dstPath
@@ -38,7 +68,7 @@
3969 } else {
4070 $command->setIO( $vipsCommands[$i - 1], 'v', VipsCommand::TEMP_OUTPUT );
4171 }
42 -
 72+
4373 $retval = $command->execute();
4474 if ( $retval != 0 ) {
4575 wfDebug( __METHOD__ . ": vips command failed!\n" );
@@ -46,41 +76,47 @@
4777 return false;
4878 }
4979 }
50 -
 80+
5181 # Set comment
5282 if ( !empty( $options['setcomment'] ) && !empty( $params['comment'] ) ) {
5383 self::setJpegComment( $params['dstPath'], $params['comment'] );
5484 }
55 -
 85+
5686 # Set the output variable
57 - $mto = new ThumbnailImage( $file, $params['dstUrl'],
 87+ $mto = new ThumbnailImage( $file, $params['dstUrl'],
5888 $params['clientWidth'], $params['clientHeight'], $params['dstPath'] );
59 -
 89+
6090 # Stop processing
6191 return false;
6292 }
63 -
 93+
 94+ /**
 95+ * @param $handler
 96+ * @param $file
 97+ * @param $params
 98+ * @param $options
 99+ */
64100 public static function makeCommands( $handler, $file, $params, $options ) {
65101 global $wgVipsCommand;
66102 $commands = array();
67 -
 103+
68104 # Get the proper im_XXX2vips handler
69105 $vipsHandler = self::getVipsHandler( $file );
70106 if ( !$vipsHandler ) {
71107 return array();
72108 }
73 -
 109+
74110 # Check if we need to convert to a .v file first
75111 if ( !empty( $options['preconvert'] ) ) {
76112 $commands[] = new VipsCommand( $wgVipsCommand, array( $vipsHandler ) );
77113 }
78 -
 114+
79115 # Do the resizing
80116 $rotation = 360 - $handler->getRotation( $file );
81 -
 117+
82118 if ( empty( $options['bilinear'] ) ) {
83 - # Calculate shrink factors. Offsetting by 0.5 pixels is required
84 - # because of rounding down of the target size by VIPS. See 25990#c7
 119+ # Calculate shrink factors. Offsetting by 0.5 pixels is required
 120+ # because of rounding down of the target size by VIPS. See 25990#c7
85121 if ( $rotation % 180 == 90 ) {
86122 # Rotated 90 degrees, so width = height and vice versa
87123 $rx = $params['srcWidth'] / ($params['physicalHeight'] + 0.5);
@@ -99,10 +135,10 @@
100136 $dstWidth = $params['physicalWidth'];
101137 $dstHeight = $params['physicalHeight'];
102138 }
103 - $commands[] = new VipsCommand( $wgVipsCommand,
 139+ $commands[] = new VipsCommand( $wgVipsCommand,
104140 array( 'im_resize_linear', $dstWidth, $dstHeight ) );
105141 }
106 -
 142+
107143 if ( !empty( $options['sharpen'] ) ) {
108144 $options['convolution'] = self::makeSharpenMatrix( $options['sharpen'] );
109145 }
@@ -111,45 +147,45 @@
112148 $commands[] = new VipsConvolution( $wgVipsCommand,
113149 array( 'im_convf', $options['convolution'] ) );
114150 }
115 -
 151+
116152 # Rotation
117153 if ( $rotation % 360 != 0 && $rotation % 90 == 0 ) {
118154 $commands[] = new VipsCommand( $wgVipsCommand, array( "im_rot{$rotation}" ) );
119155 }
120 -
 156+
121157 return $commands;
122158 }
123 -
 159+
124160 /**
125161 * Create a sharpening matrix suitable for im_convf. Uses the ImageMagick
126162 * sharpening algorithm from SharpenImage() in magick/effect.c
127 - *
 163+ *
128164 * @param mixed $params
129165 * @return array
130166 */
131167 public static function makeSharpenMatrix( $params ) {
132168 $sigma = $params['sigma'];
133 - $radius = empty( $params['radius'] ) ?
 169+ $radius = empty( $params['radius'] ) ?
134170 # After 3 sigma there should be no significant values anymore
135171 intval( round( $sigma * 3 ) ) : $params['radius'];
136172
137173 $norm = 0;
138174 $conv = array();
139 -
 175+
140176 # Fill the matrix with a negative Gaussian distribution
141177 $variance = $sigma * $sigma;
142178 for ( $x = -$radius; $x <= $radius; $x++ ) {
143179 $row = array();
144180 for ( $y = -$radius; $y <= $radius; $y++ ) {
145 - $z = -exp( -( $x*$x + $y*$y ) / ( 2 * $variance ) ) /
 181+ $z = -exp( -( $x*$x + $y*$y ) / ( 2 * $variance ) ) /
146182 ( 2 * pi() * $variance );
147183 $row[] = $z;
148184 $norm += $z;
149185 }
150186 $conv[] = $row;
151187 }
152 -
153 - # Calculate the scaling parameter to ensure that the mean of the
 188+
 189+ # Calculate the scaling parameter to ensure that the mean of the
154190 # matrix is zero
155191 $scale = - $conv[$radius][$radius] - $norm;
156192 # Set the center pixel to obtain a sharpening matrix
@@ -158,11 +194,11 @@
159195 array_unshift( $conv, array( $radius * 2 + 1, $radius * 2 + 1, $scale, 0 ) );
160196 return $conv;
161197 }
162 -
163 -
 198+
 199+
164200 /**
165201 * Check the file and params against $wgVipsOptions
166 - *
 202+ *
167203 * @param BitmapHandler $handler
168204 * @param File $file
169205 * @param array $params
@@ -178,12 +214,12 @@
179215 # Unconditionally pass
180216 return $option;
181217 }
182 -
183 - if ( isset( $condition['mimeType'] ) &&
 218+
 219+ if ( isset( $condition['mimeType'] ) &&
184220 $file->getMimeType() != $condition['mimeType'] ) {
185 - continue;
 221+ continue;
186222 }
187 -
 223+
188224 $area = $handler->getImageArea( $file );
189225 if ( isset( $condition['minArea'] ) && $area < $condition['minArea'] ) {
190226 continue;
@@ -191,18 +227,18 @@
192228 if ( isset( $condition['maxArea'] ) && $area >= $condition['maxArea'] ) {
193229 continue;
194230 }
195 -
196 - $shrinkFactor = $file->getWidth() / (
197 - ( ( $handler->getRotation( $file ) % 180 ) == 90 ) ?
 231+
 232+ $shrinkFactor = $file->getWidth() / (
 233+ ( ( $handler->getRotation( $file ) % 180 ) == 90 ) ?
198234 $params['physicalHeight'] : $params['physicalWidth'] );
199 - if ( isset( $condition['minShrinkFactor'] ) &&
 235+ if ( isset( $condition['minShrinkFactor'] ) &&
200236 $shrinkFactor < $condition['minShrinkFactor'] ) {
201 - continue;
 237+ continue;
202238 }
203 - if ( isset( $condition['maxShrinkFactor'] ) &&
 239+ if ( isset( $condition['maxShrinkFactor'] ) &&
204240 $shrinkFactor >= $condition['maxShrinkFactor'] ) {
205 - continue;
206 - }
 241+ continue;
 242+ }
207243
208244 # This condition passed
209245 return $option;
@@ -210,23 +246,25 @@
211247 # All conditions failed
212248 return false;
213249 }
214 -
 250+
215251 /**
216 - * Sets the JPEG comment on a file using exiv2.
 252+ * Sets the JPEG comment on a file using exiv2.
217253 * Requires $wgExiv2Command to be setup properly.
218 - *
 254+ *
 255+ * @todo FIXME need to handle errors such as $wgExiv2Command not available
 256+ *
219257 * @param string $fileName File where the comment needs to be set
220258 * @param string $comment The comment
221259 */
222260 public static function setJpegComment( $fileName, $comment ) {
223261 global $wgExiv2Command;
224 -
 262+
225263 wfShellExec( wfEscapeShellArg( $wgExiv2Command ) . ' mo -c '
226264 . wfEscapeShellArg( $comment ) . ' '
227265 . wfEscapeShellArg( $fileName )
228266 );
229267 }
230 -
 268+
231269 /**
232270 * Return the appropriate im_XXX2vips handler for this file
233271 * @param File $file
@@ -241,11 +279,11 @@
242280 return false;
243281 }
244282 }
245 -
 283+
246284 /**
247 - * Hook to BitmapHandlerCheckImageArea. Will set $result to true if the
 285+ * Hook to BitmapHandlerCheckImageArea. Will set $result to true if the
248286 * file will by handled by VipsScaler.
249 - *
 287+ *
250288 * @param File $file
251289 * @param array &$params
252290 * @param mixed &$result
@@ -259,20 +297,19 @@
260298 }
261299 return true;
262300 }
263 -
264 -
265301 }
266302
267303 /**
268 - * Wrapper class around the vips command, useful to chain multiple commands
 304+ * Wrapper class around the vips command, useful to chain multiple commands
269305 * with intermediate .v files
270306 */
271307 class VipsCommand {
272 - /** Flag to indicate that the output file should be a temporary .v file */
 308+
 309+ /** Flag to indicate that the output file should be a temporary .v file */
273310 const TEMP_OUTPUT = true;
274 - /**
 311+ /**
275312 * Constructor
276 - *
 313+ *
277314 * @param string $vips Path to binary
278315 * @param array $args Array or arguments
279316 */
@@ -282,7 +319,7 @@
283320 }
284321 /**
285322 * Set the input and output file of this command
286 - *
 323+ *
287324 * @param mixed $input Input file name or an VipsCommand object to use the
288325 * output of that command
289326 * @param string $output Output file name or extension of the temporary file
@@ -316,56 +353,56 @@
317354 public function getErrorString() {
318355 return $this->err;
319356 }
320 -
 357+
321358 /**
322359 * Call the vips binary with varargs and returns the return value.
323 - *
 360+ *
324361 * @return int Return value
325362 */
326363 public function execute() {
327364 # Build and escape the command string
328 - $cmd = wfEscapeShellArg( $this->vips,
 365+ $cmd = wfEscapeShellArg( $this->vips,
329366 array_shift( $this->args ),
330367 $this->input, $this->output );
331 -
 368+
332369 foreach ( $this->args as $arg ) {
333370 $cmd .= ' ' . wfEscapeShellArg( $arg );
334371 }
335 -
 372+
336373 $cmd .= ' 2>&1';
337 -
 374+
338375 # Execute
339376 $retval = 0;
340377 $this->err = wfShellExec( $cmd, $retval );
341 -
 378+
342379 # Cleanup temp file
343380 if ( $this->removeInput ) {
344381 unlink( $this->input );
345382 }
346 -
347 - return $retval;
 383+
 384+ return $retval;
348385 }
349 -
 386+
350387 /**
351 - * Generate a random, non-existent temporary file with a specified
 388+ * Generate a random, non-existent temporary file with a specified
352389 * extension.
353 - *
 390+ *
354391 * @param string Extension
355392 * @return string
356393 */
357394 public static function makeTemp( $extension ) {
358395 do {
359396 # Generate a random file
360 - $fileName = wfTempDir() . DIRECTORY_SEPARATOR .
361 - dechex( mt_rand() ) . dechex( mt_rand() ) .
 397+ $fileName = wfTempDir() . DIRECTORY_SEPARATOR .
 398+ dechex( mt_rand() ) . dechex( mt_rand() ) .
362399 '.' . $extension;
363400 } while ( file_exists( $fileName ) );
364401 # Create the file
365402 touch( $fileName );
366 -
 403+
367404 return $fileName;
368405 }
369 -
 406+
370407 }
371408
372409 /**
@@ -395,4 +432,4 @@
396433
397434 return $retval;
398435 }
399 -}
\ No newline at end of file
 436+}

Status & tagging log