Index: trunk/extensions/VipsScaler/VipsScaler.php |
— | — | @@ -16,6 +16,8 @@ |
17 | 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | 19 | * http://www.gnu.org/copyleft/gpl.html |
| 20 | + * |
| 21 | + * @file |
20 | 22 | */ |
21 | 23 | |
22 | 24 | $wgExtensionCredits['media'][] = array( |
— | — | @@ -43,7 +45,7 @@ |
44 | 46 | * with vips. Conditions are mimeType, minArea, maxArea, minShrinkFactor, |
45 | 47 | * maxShrinkFactor. The other items in the array are options. Options available |
46 | 48 | * 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 |
48 | 50 | * parameters to gaussian sharpen matrix. |
49 | 51 | * - preconvert: Convert the file to a .v file first, which costs some space, |
50 | 52 | * but saves memory on the actual downsize |
— | — | @@ -74,7 +76,7 @@ |
75 | 77 | 'conditions' => array( |
76 | 78 | 'mimeType' => 'image/png', |
77 | 79 | ), |
78 | | - ), |
| 80 | + ), |
79 | 81 | ); |
80 | 82 | |
81 | 83 | |
Index: trunk/extensions/VipsScaler/SpecialVipsTest.php |
— | — | @@ -1,4 +1,29 @@ |
2 | 2 | <?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 | + */ |
3 | 28 | class SpecialVipsTest extends SpecialPage { |
4 | 29 | public function __construct() { |
5 | 30 | parent::__construct( 'VipsTest', 'vipstest' ); |
— | — | @@ -6,9 +31,11 @@ |
7 | 32 | |
8 | 33 | /** |
9 | 34 | * Entry point |
| 35 | + * @param $par Array TODO describe what is expected there |
10 | 36 | */ |
11 | 37 | public function execute( $par ) { |
12 | 38 | $request = $this->getRequest(); |
| 39 | + |
13 | 40 | if ( $request->getText( 'thumb' ) && $request->getText( 'file' ) ) { |
14 | 41 | $this->streamThumbnail(); |
15 | 42 | } elseif ( $par || $request->getText( 'file' ) ) { |
— | — | @@ -18,77 +45,84 @@ |
19 | 46 | $this->showForm(); |
20 | 47 | } |
21 | 48 | } |
22 | | - |
| 49 | + |
| 50 | + /** |
| 51 | + */ |
23 | 52 | protected function showThumbnails() { |
24 | 53 | $request = $this->getRequest(); |
25 | | - |
| 54 | + |
26 | 55 | $title = Title::makeTitleSafe( NS_FILE, $request->getText( 'file' ) ); |
27 | 56 | if ( is_null( $title ) ) { |
28 | 57 | $this->getOutput()->addWikiMsg( 'vips-invalid-file' ); |
29 | 58 | return; |
30 | | - } |
| 59 | + } |
31 | 60 | $file = wfFindFile( $title ); |
32 | 61 | if ( !$file || !$file->exists() ) { |
33 | 62 | $this->getOutput()->addWikiMsg( 'vips-invalid-file' ); |
34 | 63 | return; |
35 | 64 | } |
36 | | - |
| 65 | + |
37 | 66 | $width = $request->getInt( 'width' ); |
38 | 67 | if ( !$width ) { |
39 | 68 | $this->getOutput()->addWikiMsg( 'vips-invalid-width' ); |
40 | 69 | return; |
41 | 70 | } |
42 | | - |
| 71 | + |
43 | 72 | $params = array( 'width' => $width ); |
44 | 73 | $thumb = $file->transform( $params ); |
45 | 74 | if ( !$thumb || $thumb->isError() ) { |
46 | 75 | $this->getOutput()->addWikiMsg( 'vips-thumb-error' ); |
47 | 76 | } |
48 | | - |
| 77 | + |
49 | 78 | $vipsThumbUrl = $this->makeUrl( $file, $width ); |
50 | | - |
51 | | - |
| 79 | + |
52 | 80 | } |
53 | | - |
| 81 | + |
| 82 | + /** |
| 83 | + * TODO |
| 84 | + */ |
54 | 85 | protected function showForm() { |
55 | | - |
| 86 | + |
56 | 87 | } |
57 | | - |
| 88 | + |
| 89 | + /** |
| 90 | + * |
| 91 | + */ |
58 | 92 | protected function streamThumbnail() { |
59 | 93 | global $wgVipsThumbnailerUrl; |
60 | | - |
| 94 | + |
61 | 95 | $request = $this->getRequest(); |
62 | | - |
| 96 | + |
63 | 97 | # Validate title and file existance |
64 | 98 | $title = Title::makeTitleSafe( NS_FILE, $request->getText( 'file' ) ); |
65 | 99 | if ( is_null( $title ) ) { |
66 | 100 | return $this->streamError( 404 ); |
67 | | - } |
| 101 | + } |
68 | 102 | $file = wfFindFile( $title ); |
69 | 103 | if ( !$file || !$file->exists() ) { |
70 | 104 | return $this->streamError( 404 ); |
71 | 105 | } |
72 | | - |
| 106 | + |
73 | 107 | # Check if vips can handle this file |
74 | 108 | if ( VipsScaler::getVipsHandler( $file ) === false ) { |
75 | 109 | return $this->streamError( 500 ); |
76 | 110 | } |
77 | | - |
| 111 | + |
78 | 112 | # Validate param string |
79 | 113 | $handler = $file->getHandler(); |
80 | 114 | $params = $handler->parseParamString( $request->getText( 'thumb' ) ); |
81 | 115 | if ( !$handler->normaliseParams( $file, $params ) ) { |
82 | 116 | return $this->streamError( 500 ); |
83 | 117 | } |
84 | | - |
| 118 | + |
85 | 119 | # Get the thumbnail |
86 | 120 | if ( is_null( $wgVipsThumbnailerUrl ) ) { |
87 | 121 | # No remote scaler, need to do it ourselves. |
88 | 122 | # Emulate the BitmapHandlerTransform hook |
89 | | - |
| 123 | + |
90 | 124 | $dstPath = VipsCommand::makeTemp( strrchr( $file->getName(), '.' ) ); |
91 | 125 | $dstUrl = ''; |
92 | | - |
| 126 | + |
93 | 127 | $scalerParams = array( |
94 | 128 | # The size to which the image will be resized |
95 | 129 | 'physicalWidth' => $params['physicalWidth'], |
— | — | @@ -108,8 +142,8 @@ |
109 | 143 | 'dstPath' => $dstPath, |
110 | 144 | 'dstUrl' => $dstUrl, |
111 | 145 | ); |
112 | | - |
113 | | - |
| 146 | + |
| 147 | + |
114 | 148 | # Call the hook |
115 | 149 | $mto = null; |
116 | 150 | if ( VipsScaler::onTransform( $handler, $file, $params, $mto ) ) { |
— | — | @@ -117,16 +151,16 @@ |
118 | 152 | } else { |
119 | 153 | $this->streamError( 500 ); |
120 | 154 | } |
121 | | - |
| 155 | + |
122 | 156 | # Cleanup the temporary file |
123 | 157 | wfSuppressWarnings(); |
124 | 158 | unlink( $dstPath ); |
125 | 159 | wfRestoreWarning(); |
126 | | - |
| 160 | + |
127 | 161 | } else { |
128 | 162 | # Request the thumbnail at a remote scaler |
129 | 163 | global $wgVipsThumbnailerProxy; |
130 | | - |
| 164 | + |
131 | 165 | $url = wfAppendQuery( $wgVipsThumbnailerUrl, array( |
132 | 166 | 'file' => $file->getName(), |
133 | 167 | 'thumb' => $handler->makeParamString( $params ) . '-' . $file->getName() |
— | — | @@ -135,7 +169,7 @@ |
136 | 170 | if ( $wgVipsThumbnailerProxy ) { |
137 | 171 | $options['proxy'] = $wgVipsThumbnailerProxy; |
138 | 172 | } |
139 | | - |
| 173 | + |
140 | 174 | $req = MWHttpRequest::factory( $url, $options ); |
141 | 175 | $status = $req->execute(); |
142 | 176 | if ( $status->isOk() ) { |
— | — | @@ -146,18 +180,21 @@ |
147 | 181 | print "\r\n"; |
148 | 182 | print $req->getContent(); |
149 | 183 | } else { |
150 | | - return $this->streamError( 500 ); |
| 184 | + return $this->streamError( 500 ); |
151 | 185 | } |
152 | | - |
| 186 | + |
153 | 187 | } |
154 | 188 | } |
155 | | - |
156 | | - protected function makeUrl( ) { |
157 | | - |
| 189 | + |
| 190 | + /** |
| 191 | + * |
| 192 | + */ |
| 193 | + protected function makeUrl( $file, $width ) { |
| 194 | + |
158 | 195 | } |
159 | | - |
| 196 | + |
160 | 197 | protected function streamError( $code ) { |
161 | | - |
| 198 | + |
162 | 199 | } |
163 | | - |
164 | | -} |
\ No newline at end of file |
| 200 | + |
| 201 | +} |
Index: trunk/extensions/VipsScaler/VipsScaler_body.php |
— | — | @@ -1,9 +1,39 @@ |
2 | 2 | <?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 | + */ |
3 | 33 | class VipsScaler { |
4 | 34 | /** |
5 | 35 | * Hook to BitmapHandlerTransform. Transforms the file using VIPS if it |
6 | 36 | * matches a condition in $wgVipsConditions |
7 | | - * |
| 37 | + * |
8 | 38 | * @param BitmapHandler $handler |
9 | 39 | * @param File $file |
10 | 40 | * @param array $params |
— | — | @@ -15,16 +45,16 @@ |
16 | 46 | if ( !$options ) { |
17 | 47 | return true; |
18 | 48 | } |
19 | | - |
| 49 | + |
20 | 50 | wfDebug( __METHOD__ . ': scaling ' . $file->getName() . " using vips\n" ); |
21 | | - |
| 51 | + |
22 | 52 | $vipsCommands = self::makeCommands( $handler, $file, $params, $options ); |
23 | 53 | if ( count( $vipsCommands ) == 0 ) { |
24 | 54 | return true; |
25 | 55 | } |
26 | | - |
| 56 | + |
27 | 57 | # Execute the commands |
28 | | - foreach ( $vipsCommands as $i => $command ) { |
| 58 | + foreach ( $vipsCommands as $i => $command ) { |
29 | 59 | # Set input/output files |
30 | 60 | if ( $i == 0 && count( $vipsCommands ) == 1 ) { |
31 | 61 | # Single command, so output directly to dstPath |
— | — | @@ -38,7 +68,7 @@ |
39 | 69 | } else { |
40 | 70 | $command->setIO( $vipsCommands[$i - 1], 'v', VipsCommand::TEMP_OUTPUT ); |
41 | 71 | } |
42 | | - |
| 72 | + |
43 | 73 | $retval = $command->execute(); |
44 | 74 | if ( $retval != 0 ) { |
45 | 75 | wfDebug( __METHOD__ . ": vips command failed!\n" ); |
— | — | @@ -46,41 +76,47 @@ |
47 | 77 | return false; |
48 | 78 | } |
49 | 79 | } |
50 | | - |
| 80 | + |
51 | 81 | # Set comment |
52 | 82 | if ( !empty( $options['setcomment'] ) && !empty( $params['comment'] ) ) { |
53 | 83 | self::setJpegComment( $params['dstPath'], $params['comment'] ); |
54 | 84 | } |
55 | | - |
| 85 | + |
56 | 86 | # Set the output variable |
57 | | - $mto = new ThumbnailImage( $file, $params['dstUrl'], |
| 87 | + $mto = new ThumbnailImage( $file, $params['dstUrl'], |
58 | 88 | $params['clientWidth'], $params['clientHeight'], $params['dstPath'] ); |
59 | | - |
| 89 | + |
60 | 90 | # Stop processing |
61 | 91 | return false; |
62 | 92 | } |
63 | | - |
| 93 | + |
| 94 | + /** |
| 95 | + * @param $handler |
| 96 | + * @param $file |
| 97 | + * @param $params |
| 98 | + * @param $options |
| 99 | + */ |
64 | 100 | public static function makeCommands( $handler, $file, $params, $options ) { |
65 | 101 | global $wgVipsCommand; |
66 | 102 | $commands = array(); |
67 | | - |
| 103 | + |
68 | 104 | # Get the proper im_XXX2vips handler |
69 | 105 | $vipsHandler = self::getVipsHandler( $file ); |
70 | 106 | if ( !$vipsHandler ) { |
71 | 107 | return array(); |
72 | 108 | } |
73 | | - |
| 109 | + |
74 | 110 | # Check if we need to convert to a .v file first |
75 | 111 | if ( !empty( $options['preconvert'] ) ) { |
76 | 112 | $commands[] = new VipsCommand( $wgVipsCommand, array( $vipsHandler ) ); |
77 | 113 | } |
78 | | - |
| 114 | + |
79 | 115 | # Do the resizing |
80 | 116 | $rotation = 360 - $handler->getRotation( $file ); |
81 | | - |
| 117 | + |
82 | 118 | 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 |
85 | 121 | if ( $rotation % 180 == 90 ) { |
86 | 122 | # Rotated 90 degrees, so width = height and vice versa |
87 | 123 | $rx = $params['srcWidth'] / ($params['physicalHeight'] + 0.5); |
— | — | @@ -99,10 +135,10 @@ |
100 | 136 | $dstWidth = $params['physicalWidth']; |
101 | 137 | $dstHeight = $params['physicalHeight']; |
102 | 138 | } |
103 | | - $commands[] = new VipsCommand( $wgVipsCommand, |
| 139 | + $commands[] = new VipsCommand( $wgVipsCommand, |
104 | 140 | array( 'im_resize_linear', $dstWidth, $dstHeight ) ); |
105 | 141 | } |
106 | | - |
| 142 | + |
107 | 143 | if ( !empty( $options['sharpen'] ) ) { |
108 | 144 | $options['convolution'] = self::makeSharpenMatrix( $options['sharpen'] ); |
109 | 145 | } |
— | — | @@ -111,45 +147,45 @@ |
112 | 148 | $commands[] = new VipsConvolution( $wgVipsCommand, |
113 | 149 | array( 'im_convf', $options['convolution'] ) ); |
114 | 150 | } |
115 | | - |
| 151 | + |
116 | 152 | # Rotation |
117 | 153 | if ( $rotation % 360 != 0 && $rotation % 90 == 0 ) { |
118 | 154 | $commands[] = new VipsCommand( $wgVipsCommand, array( "im_rot{$rotation}" ) ); |
119 | 155 | } |
120 | | - |
| 156 | + |
121 | 157 | return $commands; |
122 | 158 | } |
123 | | - |
| 159 | + |
124 | 160 | /** |
125 | 161 | * Create a sharpening matrix suitable for im_convf. Uses the ImageMagick |
126 | 162 | * sharpening algorithm from SharpenImage() in magick/effect.c |
127 | | - * |
| 163 | + * |
128 | 164 | * @param mixed $params |
129 | 165 | * @return array |
130 | 166 | */ |
131 | 167 | public static function makeSharpenMatrix( $params ) { |
132 | 168 | $sigma = $params['sigma']; |
133 | | - $radius = empty( $params['radius'] ) ? |
| 169 | + $radius = empty( $params['radius'] ) ? |
134 | 170 | # After 3 sigma there should be no significant values anymore |
135 | 171 | intval( round( $sigma * 3 ) ) : $params['radius']; |
136 | 172 | |
137 | 173 | $norm = 0; |
138 | 174 | $conv = array(); |
139 | | - |
| 175 | + |
140 | 176 | # Fill the matrix with a negative Gaussian distribution |
141 | 177 | $variance = $sigma * $sigma; |
142 | 178 | for ( $x = -$radius; $x <= $radius; $x++ ) { |
143 | 179 | $row = array(); |
144 | 180 | for ( $y = -$radius; $y <= $radius; $y++ ) { |
145 | | - $z = -exp( -( $x*$x + $y*$y ) / ( 2 * $variance ) ) / |
| 181 | + $z = -exp( -( $x*$x + $y*$y ) / ( 2 * $variance ) ) / |
146 | 182 | ( 2 * pi() * $variance ); |
147 | 183 | $row[] = $z; |
148 | 184 | $norm += $z; |
149 | 185 | } |
150 | 186 | $conv[] = $row; |
151 | 187 | } |
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 |
154 | 190 | # matrix is zero |
155 | 191 | $scale = - $conv[$radius][$radius] - $norm; |
156 | 192 | # Set the center pixel to obtain a sharpening matrix |
— | — | @@ -158,11 +194,11 @@ |
159 | 195 | array_unshift( $conv, array( $radius * 2 + 1, $radius * 2 + 1, $scale, 0 ) ); |
160 | 196 | return $conv; |
161 | 197 | } |
162 | | - |
163 | | - |
| 198 | + |
| 199 | + |
164 | 200 | /** |
165 | 201 | * Check the file and params against $wgVipsOptions |
166 | | - * |
| 202 | + * |
167 | 203 | * @param BitmapHandler $handler |
168 | 204 | * @param File $file |
169 | 205 | * @param array $params |
— | — | @@ -178,12 +214,12 @@ |
179 | 215 | # Unconditionally pass |
180 | 216 | return $option; |
181 | 217 | } |
182 | | - |
183 | | - if ( isset( $condition['mimeType'] ) && |
| 218 | + |
| 219 | + if ( isset( $condition['mimeType'] ) && |
184 | 220 | $file->getMimeType() != $condition['mimeType'] ) { |
185 | | - continue; |
| 221 | + continue; |
186 | 222 | } |
187 | | - |
| 223 | + |
188 | 224 | $area = $handler->getImageArea( $file ); |
189 | 225 | if ( isset( $condition['minArea'] ) && $area < $condition['minArea'] ) { |
190 | 226 | continue; |
— | — | @@ -191,18 +227,18 @@ |
192 | 228 | if ( isset( $condition['maxArea'] ) && $area >= $condition['maxArea'] ) { |
193 | 229 | continue; |
194 | 230 | } |
195 | | - |
196 | | - $shrinkFactor = $file->getWidth() / ( |
197 | | - ( ( $handler->getRotation( $file ) % 180 ) == 90 ) ? |
| 231 | + |
| 232 | + $shrinkFactor = $file->getWidth() / ( |
| 233 | + ( ( $handler->getRotation( $file ) % 180 ) == 90 ) ? |
198 | 234 | $params['physicalHeight'] : $params['physicalWidth'] ); |
199 | | - if ( isset( $condition['minShrinkFactor'] ) && |
| 235 | + if ( isset( $condition['minShrinkFactor'] ) && |
200 | 236 | $shrinkFactor < $condition['minShrinkFactor'] ) { |
201 | | - continue; |
| 237 | + continue; |
202 | 238 | } |
203 | | - if ( isset( $condition['maxShrinkFactor'] ) && |
| 239 | + if ( isset( $condition['maxShrinkFactor'] ) && |
204 | 240 | $shrinkFactor >= $condition['maxShrinkFactor'] ) { |
205 | | - continue; |
206 | | - } |
| 241 | + continue; |
| 242 | + } |
207 | 243 | |
208 | 244 | # This condition passed |
209 | 245 | return $option; |
— | — | @@ -210,23 +246,25 @@ |
211 | 247 | # All conditions failed |
212 | 248 | return false; |
213 | 249 | } |
214 | | - |
| 250 | + |
215 | 251 | /** |
216 | | - * Sets the JPEG comment on a file using exiv2. |
| 252 | + * Sets the JPEG comment on a file using exiv2. |
217 | 253 | * Requires $wgExiv2Command to be setup properly. |
218 | | - * |
| 254 | + * |
| 255 | + * @todo FIXME need to handle errors such as $wgExiv2Command not available |
| 256 | + * |
219 | 257 | * @param string $fileName File where the comment needs to be set |
220 | 258 | * @param string $comment The comment |
221 | 259 | */ |
222 | 260 | public static function setJpegComment( $fileName, $comment ) { |
223 | 261 | global $wgExiv2Command; |
224 | | - |
| 262 | + |
225 | 263 | wfShellExec( wfEscapeShellArg( $wgExiv2Command ) . ' mo -c ' |
226 | 264 | . wfEscapeShellArg( $comment ) . ' ' |
227 | 265 | . wfEscapeShellArg( $fileName ) |
228 | 266 | ); |
229 | 267 | } |
230 | | - |
| 268 | + |
231 | 269 | /** |
232 | 270 | * Return the appropriate im_XXX2vips handler for this file |
233 | 271 | * @param File $file |
— | — | @@ -241,11 +279,11 @@ |
242 | 280 | return false; |
243 | 281 | } |
244 | 282 | } |
245 | | - |
| 283 | + |
246 | 284 | /** |
247 | | - * Hook to BitmapHandlerCheckImageArea. Will set $result to true if the |
| 285 | + * Hook to BitmapHandlerCheckImageArea. Will set $result to true if the |
248 | 286 | * file will by handled by VipsScaler. |
249 | | - * |
| 287 | + * |
250 | 288 | * @param File $file |
251 | 289 | * @param array &$params |
252 | 290 | * @param mixed &$result |
— | — | @@ -259,20 +297,19 @@ |
260 | 298 | } |
261 | 299 | return true; |
262 | 300 | } |
263 | | - |
264 | | - |
265 | 301 | } |
266 | 302 | |
267 | 303 | /** |
268 | | - * Wrapper class around the vips command, useful to chain multiple commands |
| 304 | + * Wrapper class around the vips command, useful to chain multiple commands |
269 | 305 | * with intermediate .v files |
270 | 306 | */ |
271 | 307 | 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 */ |
273 | 310 | const TEMP_OUTPUT = true; |
274 | | - /** |
| 311 | + /** |
275 | 312 | * Constructor |
276 | | - * |
| 313 | + * |
277 | 314 | * @param string $vips Path to binary |
278 | 315 | * @param array $args Array or arguments |
279 | 316 | */ |
— | — | @@ -282,7 +319,7 @@ |
283 | 320 | } |
284 | 321 | /** |
285 | 322 | * Set the input and output file of this command |
286 | | - * |
| 323 | + * |
287 | 324 | * @param mixed $input Input file name or an VipsCommand object to use the |
288 | 325 | * output of that command |
289 | 326 | * @param string $output Output file name or extension of the temporary file |
— | — | @@ -316,56 +353,56 @@ |
317 | 354 | public function getErrorString() { |
318 | 355 | return $this->err; |
319 | 356 | } |
320 | | - |
| 357 | + |
321 | 358 | /** |
322 | 359 | * Call the vips binary with varargs and returns the return value. |
323 | | - * |
| 360 | + * |
324 | 361 | * @return int Return value |
325 | 362 | */ |
326 | 363 | public function execute() { |
327 | 364 | # Build and escape the command string |
328 | | - $cmd = wfEscapeShellArg( $this->vips, |
| 365 | + $cmd = wfEscapeShellArg( $this->vips, |
329 | 366 | array_shift( $this->args ), |
330 | 367 | $this->input, $this->output ); |
331 | | - |
| 368 | + |
332 | 369 | foreach ( $this->args as $arg ) { |
333 | 370 | $cmd .= ' ' . wfEscapeShellArg( $arg ); |
334 | 371 | } |
335 | | - |
| 372 | + |
336 | 373 | $cmd .= ' 2>&1'; |
337 | | - |
| 374 | + |
338 | 375 | # Execute |
339 | 376 | $retval = 0; |
340 | 377 | $this->err = wfShellExec( $cmd, $retval ); |
341 | | - |
| 378 | + |
342 | 379 | # Cleanup temp file |
343 | 380 | if ( $this->removeInput ) { |
344 | 381 | unlink( $this->input ); |
345 | 382 | } |
346 | | - |
347 | | - return $retval; |
| 383 | + |
| 384 | + return $retval; |
348 | 385 | } |
349 | | - |
| 386 | + |
350 | 387 | /** |
351 | | - * Generate a random, non-existent temporary file with a specified |
| 388 | + * Generate a random, non-existent temporary file with a specified |
352 | 389 | * extension. |
353 | | - * |
| 390 | + * |
354 | 391 | * @param string Extension |
355 | 392 | * @return string |
356 | 393 | */ |
357 | 394 | public static function makeTemp( $extension ) { |
358 | 395 | do { |
359 | 396 | # 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() ) . |
362 | 399 | '.' . $extension; |
363 | 400 | } while ( file_exists( $fileName ) ); |
364 | 401 | # Create the file |
365 | 402 | touch( $fileName ); |
366 | | - |
| 403 | + |
367 | 404 | return $fileName; |
368 | 405 | } |
369 | | - |
| 406 | + |
370 | 407 | } |
371 | 408 | |
372 | 409 | /** |
— | — | @@ -395,4 +432,4 @@ |
396 | 433 | |
397 | 434 | return $retval; |
398 | 435 | } |
399 | | -} |
\ No newline at end of file |
| 436 | +} |