7edb35b582106c4d277d4be52d9576cff3820a1a
[koha.git] / Koha / CoverImage.pm
1 package Koha::CoverImage;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Carp;
21 use GD;
22
23 use Koha::Database;
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::CoverImage - Koha CoverImage Object class
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 new
36
37 my $cover_image = Koha::CoverImage->new(
38     {
39         biblionumber => $biblionumber,
40         itemnumber   => $itemnumber,
41         src_image    => $image,
42         mimetype     => $mimetype,
43     }
44 );
45
46 biblionumber and/or itemnumber must be passed, otherwise the image will not be
47 linked to anything.
48
49 src_image must contain the GD image, the fullsize and thumbnail images will be generated
50 and stored in the database.
51
52 =cut
53
54 sub new {
55     my ( $class, $params ) = @_;
56
57     my $src_image = delete $params->{src_image};
58
59     if ( $src_image ) {
60           ; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless...
61
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...
67
68         $params->{mimetype} = 'image/png';
69         $params->{imagefile} = $fullsize->png();
70         $params->{thumbnail} = $thumbnail->png();
71     }
72
73     return $class->SUPER::new($params);
74 }
75
76 sub _scale_image {
77     my ( $self, $image, $maxwidth, $maxheight ) = @_;
78     my ( $width, $height ) = $image->getBounds();
79     if ( $width > $maxwidth || $height > $maxheight ) {
80
81         my $percent_reduce;  # Percent we will reduce the image dimensions by...
82         if ( $width > $maxwidth ) {
83             $percent_reduce =
84               sprintf( "%.5f", ( $maxwidth / $width ) )
85               ;    # If the width is oversize, scale based on width overage...
86         }
87         else {
88             $percent_reduce =
89               sprintf( "%.5f", ( $maxheight / $height ) )
90               ;    # otherwise scale based on height overage.
91         }
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 );
98         return $newimage;
99     }
100     else {
101         return $image;
102     }
103 }
104
105
106 =head2 Internal methods
107
108 =head3 _type
109
110 =cut
111
112 sub _type {
113     return 'CoverImage';
114 }
115
116 1;