r68418 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68417‎ | r68418 | r68419 >
Date:17:40, 22 June 2010
Author:daniel
Status:ok (Comments)
Tags:
Comment:
fixed thumbnail parameter handling, as suggested by TimStarling in a comment to bug 23258
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php
@@ -120,10 +120,10 @@
121121 // lossy and lossless
122122 $params = array('width'=>'100', 'height'=>'100', 'page'=>'1');
123123 $this->handler->normaliseParams($this->image, $params );
124 - $this->assertEquals($params['lossy'], '1');
 124+ $this->assertEquals($params['lossy'], 'lossy');
125125 $params = array('width'=>'100', 'height'=>'100', 'page'=>'2');
126126 $this->handler->normaliseParams($this->image, $params );
127 - $this->assertEquals($params['lossy'], '0');
 127+ $this->assertEquals($params['lossy'], 'lossless');
128128 // makeParamString
129129 $this->assertEquals(
130130 $this->handler->makeParamString(
@@ -146,10 +146,15 @@
147147 $error = $this->handler->doTransform( wfFindFile( Title::newFromText( 'Image:Caspian.tif' ) ), dirname( __FILE__ ) . '/testImages/caspian.tif', 'Caspian.tif', array( 'width' => 100, 'height' => 100 ) );
148148 $this->assertEquals( $error->textMsg, wfMsg( 'thumbnail_error', wfMsg( 'tiff_bad_file' ) ) );
149149 // ---- Image information
150 - // getThumbExtension
151 - $this->assertEquals( $this->handler->getThumbExtension( $this->image, 2, 1 ), '.jpg' );
152 - // TODO: 0 is obviously the same as NULL
153 - $this->assertEquals( $this->handler->getThumbExtension( $this->image, 2, '0' ), '.png' );
 150+ // getThumbType
 151+ $type = $this->handler->getThumbType( '.tiff', 'image/tiff', array( 'lossy' => 'lossy' ) );
 152+ $this->assertEquals( $type[0], 'jpg' );
 153+ $this->assertEquals( $type[1], 'image/jpeg' );
 154+
 155+ $type = $this->handler->getThumbType( '.tiff', 'image/tiff', array( 'lossy' => 'lossless' ) );
 156+ $this->assertEquals( $type[0], 'png' );
 157+ $this->assertEquals( $type[1], 'image/png' );
 158+
154159 // getLongDesc
155160 if ( $wgLanguageCode == 'de' ) {
156161 $this->assertEquals( $this->handler->getLongDesc( $this->image ), wfMsg( 'tiff-file-info-size', '1.024', '768', '2,64 MB', 'image/tiff', '1' ) );
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
@@ -122,13 +122,11 @@
123123
124124 /**
125125 * Maps MagicWord-IDs to parameters.
126 - * Parameter 'lossy' was added.
 126+ * In this case, width, page, and lossy.
127127 */
128128 function getParamMap() {
129 - // FIXME: This function is unused and should probably be a static member anyway
130129 return array(
131130 'img_width' => 'width',
132 - // @todo check height ## MediaWiki doesn't have a MagicWord-ID for height, appears to be a bug. ^SU
133131 'img_page' => 'page',
134132 'img_lossy' => 'lossy',
135133 );
@@ -158,18 +156,11 @@
159157 * Page number was added.
160158 */
161159 function makeParamString( $params ) {
162 - $page = isset( $params['page'] ) ? $params['page'] : 1;
163 - if ( !isset( $params['width'] ) ) {
 160+ if ( !isset( $params['width'] ) || !isset( $params['lossy'] ) || !isset( $params['page'] )) {
164161 return false;
165162 }
166163
167 - if ( !isset( $params['lossy'] ) || in_array( $params['lossy'], array( 1, '1', 'true', 'lossy' ) ) ) {
168 - $lossy = 'lossy';
169 - } else {
170 - $lossy = 'lossless';
171 - }
172 -
173 - return "{$lossy}-page{$page}-{$params['width']}px";
 164+ return "{$params['lossy']}-page{$params['page']}-{$params['width']}px";
174165 }
175166
176167 /**
@@ -185,12 +176,12 @@
186177 }
187178
188179 /**
189 - * Lossy parameter added
190 - * TODO: The purpose of this function is not yet fully clear.
191 - */
192 - function getScriptParams( $params ) { # # wtf?? ^DK
193 - // FIXME: This function is unused, seems to be useless,
194 - // and could be replaced with an array_intersect() call
 180+ * The function is used to specify which parameters to File::transform() should be
 181+ * passed through to thumb.php, in the case where the configuration specifies
 182+ * thumb.php is to be used (e.g. $wgThumbnailScriptPath !== false). You should
 183+ * pass through the same parameters as in makeParamString().
 184+ */
 185+ function getScriptParams( $params ) {
195186 return array(
196187 'width' => $params['width'],
197188 'page' => $params['page'],
@@ -203,38 +194,37 @@
204195 * Standard values for page and lossy are added.
205196 */
206197 function normaliseParams( $image, &$params ) {
207 - $mimeType = $image->getMimeType();
208 -
209 - if ( !isset( $params['width'] ) ) {
 198+ $data = $this->getMetaArray( $image );
 199+ if ( !$data ) {
210200 return false;
211201 }
212 - if ( !isset( $params['page'] ) || $params['page'] < 1 ) {
213 - $params['page'] = 1;
214 - }
215 - if ( $params['page'] > $this->pageCount( $image ) ) {
216 - $params['page'] = $this->pageCount( $image );
217 - }
218 - if ( !isset( $params['lossy'] ) || $params['lossy'] == null ) {
219 - $data = $this->getMetaArray( $image );
220 - if ( ( strtolower( $data['page_data'][$params['page']]['alpha'] ) == 'true' ) ) $params['lossy'] = '0';
221 - else $params['lossy'] = 1;
222 - }
223 - $size = PagedTiffImage::getPageSize( $this->getMetaArray( $image ), $params['page'] );
224 - $srcWidth = $size['width'];
225 - $srcHeight = $size['height'];
226202
227 - if ( isset( $params['height'] ) && $params['height'] != - 1 ) {
228 - // If the image is in letter format and the height parameter is set, the width
229 - // parameter is adjusted so the original ratio doesn't get messed up. This is
230 - // so the thumbnails on an ImagePage don't mess up the layout. ^SU
231 - if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
232 - $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
 203+ if ( isset( $params['page'] ) ) {
 204+ $pages = $data['page_amount'];
 205+
 206+ if ( $params['page'] > $pages ) {
 207+ $params['page'] = intval( $pages );
233208 }
234209 }
235 - $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
236 - if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
 210+
 211+ if ( !parent::normaliseParams( $image, $params ) ) {
237212 return false;
238213 }
 214+
 215+ if ( isset( $params['lossy'] ) ) {
 216+ if ( in_array( $params['lossy'], array( 1, '1', 'true', 'lossy' ) ) ) {
 217+ $params['lossy'] = 'lossy';
 218+ } else {
 219+ $params['lossy'] = 'lossless';
 220+ }
 221+ } else {
 222+ if ( ( strtolower( $data['page_data'][$params['page']]['alpha'] ) == 'true' ) ) {
 223+ $params['lossy'] = 'lossless';
 224+ } else {
 225+ $params['lossy'] = 'lossy';
 226+ }
 227+ }
 228+
239229 return true;
240230 }
241231
@@ -245,15 +235,14 @@
246236 */
247237 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
248238 global $wgImageMagickConvertCommand, $wgTiffMaxEmbedFileResolution, $wgTiffUseVips, $wgTiffVipsCommand;
249 -
250239 $metadata = $image->getMetadata();
251240
252241 if ( !$metadata ) {
253242 if ( $metadata == - 1 ) {
254 - return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_error_cached' ); # #test this ^DK
 243+ return $this->doThumbError( $params['width'], $params['height'], 'tiff_error_cached' ); # #test this ^DK
255244 // $wgCacheType = CACHE_DB
256245 }
257 - return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_no_metadata' );
 246+ return $this->doThumbError( $params['width'], $params['height'], 'tiff_no_metadata' );
258247 }
259248 if ( !$this->normaliseParams( $image, $params ) )
260249 return new TransformParameterError( $params );
@@ -264,10 +253,6 @@
265254 $srcPath = $image->getPath();
266255 $page = intval( $params['page'] );
267256
268 - $extension = $this->getThumbExtension( $image, $page, $params['lossy'] );
269 - $dstPath .= $extension;
270 - $dstUrl .= $extension;
271 -
272257 if ( $flags & self::TRANSFORM_LATER ) { //pretend the thumbnail exists, let it be created by a 404-handler
273258 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
274259 }
@@ -275,7 +260,7 @@
276261 $meta = unserialize( $metadata );
277262
278263 if ( !$this->extCheck( $meta, $error, $dstPath ) ) {
279 - return $this->doThumbError( @$params['width'], @$params['height'], $error );
 264+ return $this->doThumbError( $params['width'], $params['height'], $error );
280265 }
281266
282267 if ( is_file( $dstPath ) )
@@ -323,25 +308,14 @@
324309 }
325310
326311 /**
327 - * Decides (taking lossy parameter into account) the filetype of the thumbnail.
328 - * If there is no lossy parameter (null = not set), the decision is made
329 - * according to the presence of an alpha value.
330 - * (alpha == true = png, alpha == false = jpg)
 312+ * Get the thumbnail extension and MIME type for a given source MIME type
 313+ * @return array thumbnail extension and MIME type
331314 */
332 - function getThumbExtension( $image, $page, $lossy ) {
333 - if ( $lossy === null ) {
334 - $data = $this->getMetaArray( $image );
335 - if ( ( strtolower( $data['page_data'][$page]['alpha'] ) == 'true' ) ) {
336 - return '.png';
337 - } else {
338 - return '.jpg';
339 - }
 315+ function getThumbType( $ext, $mime, $params ) {
 316+ if ( $params[ 'lossy' ] == 'lossy' ) {
 317+ return array( 'jpg', 'image/jpeg' );
340318 } else {
341 - if ( in_array( $lossy, array( 1, '1', 'true', 'lossy' ) ) ) {
342 - return '.jpg';
343 - } else {
344 - return '.png';
345 - }
 319+ return array( 'png', 'image/png' );
346320 }
347321 }
348322

Follow-up revisions

RevisionCommit summaryAuthorDate
r68661improved meta-data and error handling and persistance, as suggested by tim in...daniel14:41, 28 June 2010
r68664use internal Exif per default, as suggested by tim in a comment to bug 23258;...daniel16:15, 28 June 2010
r68714added normalization for parameter 'page', based on pageCount, as by tims comm...daniel08:40, 29 June 2010
r68715normalisation of parameter "page" was moved to ImageHandler as of r68714; thi...daniel08:41, 29 June 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r68409passing thumbnail params to getThumbType, as suggested by TimStarling in a co...daniel16:01, 22 June 2010

Comments

#Comment by Tim Starling (talk | contribs)   04:38, 29 June 2010
		if ( isset( $params['page'] ) ) {
			$pages = $data['page_amount'];
			
			if ( $params['page'] > $pages ) {
				$params['page'] = intval( $pages );
			}
		}

This code should be moved to ImageHandler::normaliseParams(). We have MediaHandler::pageCount() so there's no need for this to be TIFF-specific.

Otherwise, looks good.

#Comment by Duesentrieb (talk | contribs)   08:42, 29 June 2010

fixed in r68714 and 68715.

Status & tagging log