1 package Koha::CoverImage;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use base qw(Koha::Object);
28 Koha::CoverImage - Koha CoverImage Object class
36 my $cover_image = Koha::CoverImage->new(
38 biblionumber => $biblionumber,
39 itemnumber => $itemnumber,
41 mimetype => $mimetype,
45 biblionumber and/or itemnumber must be passed, otherwise the image will not be
48 src_image must contain the GD image, the fullsize and thumbnail images will be generated
49 and stored in the database.
54 my ( $class, $params ) = @_;
56 my $src_image = delete $params->{src_image};
59 ; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless...
61 # Check the pixel size of the image we are about to import...
62 my $thumbnail = $class->_scale_image( $src_image, 140, 200 )
63 ; # MAX pixel dims are 140 X 200 for thumbnail...
64 my $fullsize = $class->_scale_image( $src_image, 600, 800 )
65 ; # MAX pixel dims are 600 X 800 for full-size image...
67 $params->{mimetype} = 'image/png';
68 $params->{imagefile} = $fullsize->png();
69 $params->{thumbnail} = $thumbnail->png();
72 return $class->SUPER::new($params);
76 my ( $self, $image, $maxwidth, $maxheight ) = @_;
77 my ( $width, $height ) = $image->getBounds();
78 if ( $width > $maxwidth || $height > $maxheight ) {
80 my $percent_reduce; # Percent we will reduce the image dimensions by...
81 if ( $width > $maxwidth ) {
83 sprintf( "%.5f", ( $maxwidth / $width ) )
84 ; # If the width is oversize, scale based on width overage...
88 sprintf( "%.5f", ( $maxheight / $height ) )
89 ; # otherwise scale based on height overage.
91 my $width_reduce = sprintf( "%.0f", ( $width * $percent_reduce ) );
92 my $height_reduce = sprintf( "%.0f", ( $height * $percent_reduce ) );
93 my $newimage = GD::Image->new( $width_reduce, $height_reduce, 1 )
94 ; #'1' creates true color image...
95 $newimage->copyResampled( $image, 0, 0, 0, 0, $width_reduce,
96 $height_reduce, $width, $height );
105 =head2 Internal methods