* (bug 153) Adjust thumbnail size calculations to match consistently;
authorBrion Vibber <brion@users.mediawiki.org>
Sun, 4 Dec 2005 08:52:01 +0000 (08:52 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sun, 4 Dec 2005 08:52:01 +0000 (08:52 +0000)
  patch by David Benbennick

RELEASE-NOTES
includes/Image.php
includes/ImagePage.php
includes/Linker.php

index fb5e1dd..32d2109 100644 (file)
@@ -287,6 +287,8 @@ fully support the editing toolbar, but was found to be too confusing.
 * (bug 3844) ab: av: ba: ce: & kv: now inherit from LanguageRu.php
              ii: & za: now inherit from LanguageZn_cn.php
 * Optional summary parameter to action=rollback, for user javascript
+* (bug 153) Adjust thumbnail size calculations to match consistently;
+  patch by David Benbennick
 
 
 === Caveats ===
index cea096f..96ff937 100644 (file)
@@ -859,25 +859,18 @@ class Image
         * @access public
         */
        function getThumbnail( $width, $height=-1 ) {
-               if ( $height == -1 ) {
+               if ( $height <= 0 ) {
                        return $this->renderThumb( $width );
                }
                $this->load();
                
                if ($this->canRender()) {
-                       if ( $width < $this->width ) {
-                               $thumbheight = $this->height * $width / $this->width;
-                               $thumbwidth = $width;
-                       } else {
-                               $thumbheight = $this->height;
-                               $thumbwidth = $this->width;
-                       }
-                       if ( $thumbheight > $height ) {
-                               $thumbwidth = $thumbwidth * $height / $thumbheight;
-                               $thumbheight = $height;
-                       }
-                       
-                       $thumb = $this->renderThumb( $thumbwidth );
+                       if ( $width > $this->width * $height / $this->height )
+                               $width = floor( $this->width * $height / $this->height );
+                               # Note this is the largest width such that the thumbnail's
+                               # height is at most $height.
+
+                       $thumb = $this->renderThumb( $width );
                }
                else $thumb= NULL; #not a bitmap or renderable image, don't try.
                
@@ -962,7 +955,7 @@ class Image
                        return $thumb;
                }
                
-               $height = floor( $this->height * ( $width/$this->width ) );
+               $height = round( $this->height * $width / $this->width );
                
                list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
                if ( $isScriptUrl && $useScript ) {
@@ -1046,8 +1039,12 @@ class Image
                        # use ImageMagick
                        # Specify white background color, will be used for transparent images
                        # in Internet Explorer/Windows instead of default black.
+
+                       # Note, we specify "-size {$width}" and NOT "-size {$width}x{$height}".
+                       # It seems that ImageMagick has a bug wherein it produces thumbnails of
+                       # the wrong size in the second case.
                        $cmd  =  $wgImageMagickConvertCommand .
-                               " -quality 85 -background white -size {$width}x{$height} ".
+                               " -quality 85 -background white -size {$width} ".
                                wfEscapeShellArg($this->imagePath) . " -resize {$width}x{$height} " .
                                wfEscapeShellArg($thumbPath);                           
                        wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n");
@@ -1800,8 +1797,11 @@ class ThumbnailImage {
         */
        function ThumbnailImage( $url, $width, $height, $path = false ) {
                $this->url = $url;
-               $this->width = $width;
-               $this->height = $height;
+               $this->width = round( $width );
+               $this->height = round( $height );
+                       # These should be integers when they get here.
+                       # If not, there's a bug somewhere.  But let's at
+                       # least produce valid HTML code regardless.
                $this->path = $path;
        }
 
index 1ffa52d..7e64769 100644 (file)
@@ -193,15 +193,23 @@ class ImagePage extends Article {
 
                                # "Download high res version" link below the image
                                $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
-                               if ( $width > $maxWidth ) {
-                                       $height = floor( $height * $maxWidth / $width );
-                                       $width  = $maxWidth;
-                               }
-                               if ( $height > $maxHeight ) {
-                                       $width = floor( $width * $maxHeight / $height );
-                                       $height = $maxHeight;
-                               }
-                               if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
+
+                               # We'll show a thumbnail of this image
+                               if ( $width > $maxWidth || $height > $maxHeight ) {
+                                       # Calculate the thumbnail size.
+                                       # First case, the limiting factor is the width, not the height.
+                                       if ( $width / $height >= $maxWidth / $maxHeight ) {
+                                               $height = round( $height * $maxWidth / $width);
+                                               $width = $maxWidth;
+                                               # Note that $height <= $maxHeight now.
+                                       } else {
+                                               $newwidth = floor( $width * $maxHeight / $height);
+                                               $height = round( $height * $newwidth / $width );
+                                               $width = $newwidth;
+                                               # Note that $height <= $maxHeight now, but might not be identical
+                                               # because of rounding.
+                                       }
+
                                        if( $wgUseImageResize ) {
                                                $thumbnail = $this->img->getThumbnail( $width );
                                                if ( $thumbnail == null ) {
index 53a8f4e..798a262 100644 (file)
@@ -426,18 +426,18 @@ class Linker {
                        # Create a resized image, without the additional thumbnail
                        # features
 
-                       if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
-                               $width = $img->getWidth() * $height / $img->getHeight();
-                       }
+                       if ( $height == false )
+                               $height = -1;
                        if ( $manual_thumb == '') {
-                               $thumb = $img->getThumbnail( $width );
+                               $thumb = $img->getThumbnail( $width, $height );
                                if ( $thumb ) {
-                                       if( $width > $thumb->width ) {
+                                       if( $width > $img->width && ( $height == -1 || $height > $img->height )) {
                                                // Requested a display size larger than the actual image;
                                                // fake it up!
-                                               $height = floor($thumb->height * $width / $thumb->width);
+                                               $height = round($thumb->height * $width / $thumb->width);
                                                wfDebug( "makeImageLinkObj: client-size height set to '$height'\n" );
                                        } else {
+                                               $width = $thumb->width;
                                                $height = $thumb->height;
                                                wfDebug( "makeImageLinkObj: thumb height set to '$height'\n" );
                                        }
@@ -494,20 +494,19 @@ class Linker {
                {
                        // Use image dimensions, don't scale
                        $boxwidth  = $width;
-                       $oboxwidth = $boxwidth + 2;
                        $boxheight = $height;
                        $thumbUrl  = $url;
                } else {
-                       $h  = round( $height/($width/$boxwidth) );
-                       $oboxwidth = $boxwidth + 2;
-                       if ( ( ! $boxheight === false ) &&  ( $h > $boxheight ) )
-                       {
-                               $boxwidth *= $boxheight/$h;
-                       } else {
-                               $boxheight = $h;
+                       if ( $boxheight === false )
+                               $boxheight = -1;
+                       if ( '' == $manual_thumb ) {
+                               $thumb = $img->getThumbnail( $boxwidth, $boxheight );
+                               $thumbUrl = $thumb->getUrl();
+                               $boxwidth = $thumb->width;
+                               $boxheight = $thumb->height;
                        }
-                       if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
                }
+               $oboxwidth = $boxwidth + 2;
 
                if ( $manual_thumb != '' ) # Use manually specified thumbnail
                {