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