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 (vote)
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-05-04 08:44 UTC by Steve Cooney
Modified: 2023-05-04 08:44 UTC (History)
1 user (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments

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;
    }
}