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>.
25 use base
qw(Koha::Object);
29 Koha::CoverImage - Koha CoverImage Object class
37 my $cover_image = Koha::CoverImage->new(
39 biblionumber => $biblionumber,
40 itemnumber => $itemnumber,
42 mimetype => $mimetype,
46 biblionumber and/or itemnumber must be passed, otherwise the image will not be
49 src_image must contain the GD image, the fullsize and thumbnail images will be generated
50 and stored in the database.
55 my ( $class, $params ) = @_;
57 my $src_image = delete $params->{src_image
};
60 ; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless...
62 # Check the pixel size of the image we are about to import...
63 my $thumbnail = $class->_scale_image( $src_image, 140, 200 )
64 ; # MAX pixel dims are 140 X 200 for thumbnail...
65 my $fullsize = $class->_scale_image( $src_image, 600, 800 )
66 ; # MAX pixel dims are 600 X 800 for full-size image...
68 $params->{mimetype
} = 'image/png';
69 $params->{imagefile
} = $fullsize->png();
70 $params->{thumbnail
} = $thumbnail->png();
73 return $class->SUPER::new
($params);
77 my ( $self, $image, $maxwidth, $maxheight ) = @_;
78 my ( $width, $height ) = $image->getBounds();
79 if ( $width > $maxwidth || $height > $maxheight ) {
81 my $percent_reduce; # Percent we will reduce the image dimensions by...
82 if ( $width > $maxwidth ) {
84 sprintf( "%.5f", ( $maxwidth / $width ) )
85 ; # If the width is oversize, scale based on width overage...
89 sprintf( "%.5f", ( $maxheight / $height ) )
90 ; # otherwise scale based on height overage.
92 my $width_reduce = sprintf( "%.0f", ( $width * $percent_reduce ) );
93 my $height_reduce = sprintf( "%.0f", ( $height * $percent_reduce ) );
94 my $newimage = GD
::Image
->new( $width_reduce, $height_reduce, 1 )
95 ; #'1' creates true color image...
96 $newimage->copyResampled( $image, 0, 0, 0, 0, $width_reduce,
97 $height_reduce, $width, $height );
106 =head2 Internal methods