Bug 33674 - Landscape cover images are resized ignoring if image/book cover width > height
Summary: Landscape cover images are resized ignoring if image/book cover width > height
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Cataloging (show other bugs)
Version: 21.11
Hardware: All All
: P5 - low enhancement
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-05-04 08:44 UTC by Steve Cooney
Modified: 2025-12-31 21:29 UTC (History)
4 users (show)

See Also:
GIT URL:
Initiative type: ---
Sponsorship status: ---
Comma delimited list of Sponsors:
Crowdfunding goal: 0
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Cover image variations (83.07 KB, image/jpeg)
2025-12-31 20:16 UTC, Esther Melander
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Cooney 2023-05-04 08:44:08 UTC
The code fragments below (/Koha/CoverImage.pm) need to determine if the width is greater than the height before resizing and creating the thumbnails and 'fullsize' image version. Otherwise  wide books are always resized as if they were tall books. As a result the thumbnails and fullsize images of wide books are always scaled smaller than their 'portrait' counterparts. 
Needless to say these hardcoded values should be part of system preferences. They were set before the days of 4K monitors. I think that might be an open item.

sub new {
    my ( $class, $params ) = @_;

    my $src_image = delete $params->{src_image};

    if ( $src_image ) {
          ; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless...

        # Check the pixel size of the image we are about to import...
        my $thumbnail = $class->_scale_image( $src_image, 140, 200 )
          ;    # MAX pixel dims are 140 X 200 for thumbnail...
        my $fullsize = $class->_scale_image( $src_image, 600, 800 )
          ;    # MAX pixel dims are 600 X 800 for full-size image...

        $params->{mimetype} = 'image/png';
        $params->{imagefile} = $fullsize->png();
        $params->{thumbnail} = $thumbnail->png();
    }

    return $class->SUPER::new($params);
}

sub _scale_image {
    my ( $self, $image, $maxwidth, $maxheight ) = @_;
    my ( $width, $height ) = $image->getBounds();
    if ( $width > $maxwidth || $height > $maxheight ) {

        my $percent_reduce;  # Percent we will reduce the image dimensions by...
        if ( $width > $maxwidth ) {
            $percent_reduce =
              sprintf( "%.5f", ( $maxwidth / $width ) )
              ;    # If the width is oversize, scale based on width overage...
        }
        else {
            $percent_reduce =
              sprintf( "%.5f", ( $maxheight / $height ) )
              ;    # otherwise scale based on height overage.
        }
        my $width_reduce  = sprintf( "%.0f", ( $width * $percent_reduce ) );
        my $height_reduce = sprintf( "%.0f", ( $height * $percent_reduce ) );
        my $newimage = GD::Image->new( $width_reduce, $height_reduce, 1 )
          ;        #'1' creates true color image...
        $newimage->copyResampled( $image, 0, 0, 0, 0, $width_reduce,
            $height_reduce, $width, $height );
        return $newimage;
    }
    else {
        return $image;
    }
}
Comment 1 Esther Melander 2025-12-31 20:16:35 UTC
Created attachment 190810 [details]
Cover image variations

Looking at the code, it appears the images are processed so that the dimensions are adjusted by a percentage rather than forcing them into the dimensions of 140 X 200 or 600 x 800 as described in the Koha manual. The percentage adjustment seems correct so that the aspect ratio of the original image is not altered. However, this processing results in an inconsistent display in the OPAC if the original images are all different sizes. It seems like the images should be adjusted to fit within a container that maintains the same space in search result listings and the bibliographic detail view. See the attached image where different image sizes were uploaded resulting in a variation in the space used for the listing. The flexibility of the current code allows for the user to upload images with a variety of dimensions, but should there be consistency in the display regardless?

Also, perhaps the Koha manual should be updated to indicate recommended image dimensions and/or clarify images are processed by percentage rather than fixed dimensions. While this is a bit of a side tangent from the original, the uploaded image example does seem to illustrate the problem of an image in landscape mode with the third listing as it does appear small in comparison to the other images in a portrait orientation.

It's also not clear if this processing applies to individual image uploads or batch image uploads (at least from the manual).
Comment 2 David Nind 2025-12-31 21:29:14 UTC
Happy to update the manual, if we can get some consensus on what the recommended sizes are (and that match up with how Koha actually works).