Bug 26145: Refactoring - Move C4::Images to Koha::CoverImages
[koha.git] / Koha / CoverImage.pm
blob7edb35b582106c4d277d4be52d9576cff3820a1a
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>.
18 use Modern::Perl;
20 use Carp;
21 use GD;
23 use Koha::Database;
25 use base qw(Koha::Object);
27 =head1 NAME
29 Koha::CoverImage - Koha CoverImage Object class
31 =head1 API
33 =head2 Class methods
35 =head3 new
37 my $cover_image = Koha::CoverImage->new(
39 biblionumber => $biblionumber,
40 itemnumber => $itemnumber,
41 src_image => $image,
42 mimetype => $mimetype,
46 biblionumber and/or itemnumber must be passed, otherwise the image will not be
47 linked to anything.
49 src_image must contain the GD image, the fullsize and thumbnail images will be generated
50 and stored in the database.
52 =cut
54 sub new {
55 my ( $class, $params ) = @_;
57 my $src_image = delete $params->{src_image};
59 if ( $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);
76 sub _scale_image {
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 ) {
83 $percent_reduce =
84 sprintf( "%.5f", ( $maxwidth / $width ) )
85 ; # If the width is oversize, scale based on width overage...
87 else {
88 $percent_reduce =
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 );
98 return $newimage;
100 else {
101 return $image;
106 =head2 Internal methods
108 =head3 _type
110 =cut
112 sub _type {
113 return 'CoverImage';