Bug 16412: Make sure Koha::Cache->set_in_cache will not warn anymore
[koha.git] / C4 / Images.pm
blob936c4df787af581c1fa6dee35849734ccf058126
1 package C4::Images;
3 # Copyright (C) 2011 C & P Bibliography Services
4 # Jared Camins-Esakov <jcamins@cpbibliograpy.com>
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
23 use 5.010;
25 use C4::Context;
26 use GD;
28 use vars qw($debug $noimage $VERSION @ISA @EXPORT);
30 BEGIN {
32 # set the version for version checking
33 $VERSION = 3.07.00.049;
34 require Exporter;
35 @ISA = qw(Exporter);
36 @EXPORT = qw(
37 &PutImage
38 &RetrieveImage
39 &ListImagesForBiblio
40 &DelImage
42 $debug = $ENV{KOHA_DEBUG} || $ENV{DEBUG} || 0;
44 $noimage = pack( "H*",
45 '47494638396101000100800000FFFFFF'
46 . '00000021F90401000000002C00000000'
47 . '010001000002024401003B' );
50 =head2 PutImage
52 PutImage($biblionumber, $srcimage, $replace);
54 Stores binary image data and thumbnail in database, optionally replacing existing images for the given biblio.
56 =cut
58 sub PutImage {
59 my ( $biblionumber, $srcimage, $replace ) = @_;
61 return -1 unless defined($srcimage);
63 if ($replace) {
64 foreach ( ListImagesForBiblio($biblionumber) ) {
65 DelImage($_);
69 my $dbh = C4::Context->dbh;
70 my $query =
71 "INSERT INTO biblioimages (biblionumber, mimetype, imagefile, thumbnail) VALUES (?,?,?,?);";
72 my $sth = $dbh->prepare($query);
74 my $mimetype = 'image/png'
75 ; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless...
77 # Check the pixel size of the image we are about to import...
78 my $thumbnail = _scale_image( $srcimage, 140, 200 )
79 ; # MAX pixel dims are 140 X 200 for thumbnail...
80 my $fullsize = _scale_image( $srcimage, 600, 800 )
81 ; # MAX pixel dims are 600 X 800 for full-size image...
82 $debug and warn "thumbnail is " . length($thumbnail) . " bytes.";
84 $sth->execute( $biblionumber, $mimetype, $fullsize->png(),
85 $thumbnail->png() );
86 my $dberror = $sth->errstr;
87 warn "Error returned inserting $biblionumber.$mimetype." if $sth->errstr;
88 undef $thumbnail;
89 undef $fullsize;
90 return $dberror;
93 =head2 RetrieveImage
94 my ($imagedata, $error) = RetrieveImage($imagenumber);
96 Retrieves the specified image.
98 =cut
100 sub RetrieveImage {
101 my ($imagenumber) = @_;
103 my $dbh = C4::Context->dbh;
104 my $query =
105 'SELECT imagenumber, mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?';
106 my $sth = $dbh->prepare($query);
107 $sth->execute($imagenumber);
108 my $imagedata = $sth->fetchrow_hashref;
109 if ( !$imagedata ) {
110 $imagedata->{'thumbnail'} = $noimage;
111 $imagedata->{'imagefile'} = $noimage;
113 if ( $sth->err ) {
114 warn "Database error!" if $debug;
116 return $imagedata;
119 =head2 ListImagesForBiblio
120 my (@images) = ListImagesForBiblio($biblionumber);
122 Gets a list of all images associated with a particular biblio.
124 =cut
126 sub ListImagesForBiblio {
127 my ($biblionumber) = @_;
129 my @imagenumbers;
130 my $dbh = C4::Context->dbh;
131 my $query = 'SELECT imagenumber FROM biblioimages WHERE biblionumber = ?';
132 my $sth = $dbh->prepare($query);
133 $sth->execute($biblionumber);
134 while ( my $row = $sth->fetchrow_hashref ) {
135 push @imagenumbers, $row->{'imagenumber'};
137 return @imagenumbers;
140 =head2 DelImage
142 my ($dberror) = DelImage($imagenumber);
144 Removes the image with the supplied imagenumber.
146 =cut
148 sub DelImage {
149 my ($imagenumber) = @_;
150 warn "Imagenumber passed to DelImage is $imagenumber" if $debug;
151 my $dbh = C4::Context->dbh;
152 my $query = "DELETE FROM biblioimages WHERE imagenumber = ?;";
153 my $sth = $dbh->prepare($query);
154 $sth->execute($imagenumber);
155 my $dberror = $sth->errstr;
156 warn "Database error!" if $sth->errstr;
157 return $dberror;
160 sub _scale_image {
161 my ( $image, $maxwidth, $maxheight ) = @_;
162 my ( $width, $height ) = $image->getBounds();
163 $debug and warn "image is $width pix X $height pix.";
164 if ( $width > $maxwidth || $height > $maxheight ) {
166 # $debug and warn "$filename exceeds the maximum pixel dimensions of $maxwidth X $maxheight. Resizing...";
167 my $percent_reduce; # Percent we will reduce the image dimensions by...
168 if ( $width > $maxwidth ) {
169 $percent_reduce =
170 sprintf( "%.5f", ( $maxwidth / $width ) )
171 ; # If the width is oversize, scale based on width overage...
173 else {
174 $percent_reduce =
175 sprintf( "%.5f", ( $maxheight / $height ) )
176 ; # otherwise scale based on height overage.
178 my $width_reduce = sprintf( "%.0f", ( $width * $percent_reduce ) );
179 my $height_reduce = sprintf( "%.0f", ( $height * $percent_reduce ) );
180 $debug
181 and warn "Reducing image by "
182 . ( $percent_reduce * 100 )
183 . "\% or to $width_reduce pix X $height_reduce pix";
184 my $newimage = GD::Image->new( $width_reduce, $height_reduce, 1 )
185 ; #'1' creates true color image...
186 $newimage->copyResampled( $image, 0, 0, 0, 0, $width_reduce,
187 $height_reduce, $width, $height );
188 return $newimage;
190 else {
191 return $image;
195 =head2 NoImage
197 C4::Images->NoImage;
199 Returns the gif to be used when there is no image matching the request, and
200 its mimetype (image/gif).
202 =cut
204 sub NoImage {
205 return $noimage, 'image/gif';