Bug 33674

Summary: Landscape cover images are resized ignoring if image/book cover width > height
Product: Koha Reporter: Steve Cooney <steve>
Component: CatalogingAssignee: Bugs List <koha-bugs>
Status: NEW --- QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: m.de.rooy
Version: 21.11   
Hardware: All   
OS: All   
GIT URL: Change sponsored?: ---
Patch complexity: --- Documentation contact:
Documentation submission: Text to go in the release notes:
Version(s) released in:
Circulation function:

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