Lines 1-4
Link Here
|
1 |
package C4::Images; |
1 |
package C4::Images; |
|
|
2 |
|
3 |
# Copyright (C) 2011 C & P Bibliography Services |
4 |
# Jared Camins-Esakov <jcamins@cpbibliograpy.com> |
5 |
# |
6 |
# This file is part of Koha. |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 2 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along |
18 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
2 |
use strict; |
21 |
use strict; |
3 |
use warnings; |
22 |
use warnings; |
4 |
use 5.010; |
23 |
use 5.010; |
Lines 9-25
use GD;
Link Here
|
9 |
use vars qw($debug $VERSION @ISA @EXPORT); |
28 |
use vars qw($debug $VERSION @ISA @EXPORT); |
10 |
|
29 |
|
11 |
BEGIN { |
30 |
BEGIN { |
12 |
# set the version for version checking |
31 |
|
13 |
$VERSION = 3.03; |
32 |
# set the version for version checking |
14 |
require Exporter; |
33 |
$VERSION = 3.03; |
15 |
@ISA = qw(Exporter); |
34 |
require Exporter; |
16 |
@EXPORT = qw( |
35 |
@ISA = qw(Exporter); |
17 |
&PutImage |
36 |
@EXPORT = qw( |
18 |
&RetrieveImage |
37 |
&PutImage |
19 |
&ListImagesForBiblio |
38 |
&RetrieveImage |
20 |
&DelImage |
39 |
&ListImagesForBiblio |
|
|
40 |
&DelImage |
21 |
); |
41 |
); |
22 |
$debug = $ENV{KOHA_DEBUG} || $ENV{DEBUG} || 0; |
42 |
$debug = $ENV{KOHA_DEBUG} || $ENV{DEBUG} || 0; |
23 |
} |
43 |
} |
24 |
|
44 |
|
25 |
=head2 PutImage |
45 |
=head2 PutImage |
Lines 31-57
Stores binary image data and thumbnail in database, optionally replacing existin
Link Here
|
31 |
=cut |
51 |
=cut |
32 |
|
52 |
|
33 |
sub PutImage { |
53 |
sub PutImage { |
34 |
my ($biblionumber, $srcimage, $replace) = @_; |
54 |
my ( $biblionumber, $srcimage, $replace ) = @_; |
35 |
|
55 |
|
36 |
return -1 unless defined($srcimage); |
56 |
return -1 unless defined($srcimage); |
37 |
|
57 |
|
38 |
if ($replace) { |
58 |
if ($replace) { |
39 |
foreach (ListImagesForBiblio($biblionumber)) { |
59 |
foreach ( ListImagesForBiblio($biblionumber) ) { |
40 |
DelImage($_); |
60 |
DelImage($_); |
41 |
} |
61 |
} |
42 |
} |
62 |
} |
43 |
|
63 |
|
44 |
my $dbh = C4::Context->dbh; |
64 |
my $dbh = C4::Context->dbh; |
45 |
my $query = "INSERT INTO biblioimages (biblionumber, mimetype, imagefile, thumbnail) VALUES (?,?,?,?);"; |
65 |
my $query = |
|
|
66 |
"INSERT INTO biblioimages (biblionumber, mimetype, imagefile, thumbnail) VALUES (?,?,?,?);"; |
46 |
my $sth = $dbh->prepare($query); |
67 |
my $sth = $dbh->prepare($query); |
47 |
|
68 |
|
48 |
my $mimetype = 'image/png'; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless... |
69 |
my $mimetype = 'image/png' |
49 |
# Check the pixel size of the image we are about to import... |
70 |
; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless... |
50 |
my $thumbnail = _scale_image($srcimage, 140, 200); # MAX pixel dims are 140 X 200 for thumbnail... |
71 |
|
51 |
my $fullsize = _scale_image($srcimage, 600, 800); # MAX pixel dims are 600 X 800 for full-size image... |
72 |
# Check the pixel size of the image we are about to import... |
|
|
73 |
my $thumbnail = _scale_image( $srcimage, 140, 200 ) |
74 |
; # MAX pixel dims are 140 X 200 for thumbnail... |
75 |
my $fullsize = _scale_image( $srcimage, 600, 800 ) |
76 |
; # MAX pixel dims are 600 X 800 for full-size image... |
52 |
$debug and warn "thumbnail is " . length($thumbnail) . " bytes."; |
77 |
$debug and warn "thumbnail is " . length($thumbnail) . " bytes."; |
53 |
|
78 |
|
54 |
$sth->execute($biblionumber,$mimetype,$fullsize->png(),$thumbnail->png()); |
79 |
$sth->execute( $biblionumber, $mimetype, $fullsize->png(), |
|
|
80 |
$thumbnail->png() ); |
55 |
my $dberror = $sth->errstr; |
81 |
my $dberror = $sth->errstr; |
56 |
warn "Error returned inserting $biblionumber.$mimetype." if $sth->errstr; |
82 |
warn "Error returned inserting $biblionumber.$mimetype." if $sth->errstr; |
57 |
undef $thumbnail; |
83 |
undef $thumbnail; |
Lines 70-83
sub RetrieveImage {
Link Here
|
70 |
my ($imagenumber) = @_; |
96 |
my ($imagenumber) = @_; |
71 |
|
97 |
|
72 |
my $dbh = C4::Context->dbh; |
98 |
my $dbh = C4::Context->dbh; |
73 |
my $query = 'SELECT mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?'; |
99 |
my $query = |
|
|
100 |
'SELECT mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?'; |
74 |
my $sth = $dbh->prepare($query); |
101 |
my $sth = $dbh->prepare($query); |
75 |
$sth->execute($imagenumber); |
102 |
$sth->execute($imagenumber); |
76 |
my $imagedata = $sth->fetchrow_hashref; |
103 |
my $imagedata = $sth->fetchrow_hashref; |
77 |
if ($sth->err) { |
104 |
if ( $sth->err ) { |
78 |
warn "Database error!"; |
105 |
warn "Database error!"; |
79 |
return undef; |
106 |
return undef; |
80 |
} else { |
107 |
} |
|
|
108 |
else { |
81 |
return $imagedata; |
109 |
return $imagedata; |
82 |
} |
110 |
} |
83 |
} |
111 |
} |
Lines 89-110
Gets a list of all images associated with a particular biblio.
Link Here
|
89 |
|
117 |
|
90 |
=cut |
118 |
=cut |
91 |
|
119 |
|
92 |
|
|
|
93 |
sub ListImagesForBiblio { |
120 |
sub ListImagesForBiblio { |
94 |
my ($biblionumber) = @_; |
121 |
my ($biblionumber) = @_; |
95 |
|
122 |
|
96 |
my @imagenumbers; |
123 |
my @imagenumbers; |
97 |
my $dbh = C4::Context->dbh; |
124 |
my $dbh = C4::Context->dbh; |
98 |
my $query = 'SELECT imagenumber FROM biblioimages WHERE biblionumber = ?'; |
125 |
my $query = 'SELECT imagenumber FROM biblioimages WHERE biblionumber = ?'; |
99 |
my $sth = $dbh->prepare($query); |
126 |
my $sth = $dbh->prepare($query); |
100 |
$sth->execute($biblionumber); |
127 |
$sth->execute($biblionumber); |
101 |
warn "Database error!" if $sth->errstr; |
128 |
warn "Database error!" if $sth->errstr; |
102 |
if (!$sth->errstr && $sth->rows > 0) { |
129 |
if ( !$sth->errstr && $sth->rows > 0 ) { |
103 |
while (my $row = $sth->fetchrow_hashref) { |
130 |
while ( my $row = $sth->fetchrow_hashref ) { |
104 |
push @imagenumbers, $row->{'imagenumber'}; |
131 |
push @imagenumbers, $row->{'imagenumber'}; |
105 |
} |
132 |
} |
106 |
return @imagenumbers; |
133 |
return @imagenumbers; |
107 |
} else { |
134 |
} |
|
|
135 |
else { |
108 |
return undef; |
136 |
return undef; |
109 |
} |
137 |
} |
110 |
} |
138 |
} |
Lines 120-128
Removes the image with the supplied imagenumber.
Link Here
|
120 |
sub DelImage { |
148 |
sub DelImage { |
121 |
my ($imagenumber) = @_; |
149 |
my ($imagenumber) = @_; |
122 |
warn "Imagenumber passed to DelImage is $imagenumber" if $debug; |
150 |
warn "Imagenumber passed to DelImage is $imagenumber" if $debug; |
123 |
my $dbh = C4::Context->dbh; |
151 |
my $dbh = C4::Context->dbh; |
124 |
my $query = "DELETE FROM biblioimages WHERE imagenumber = ?;"; |
152 |
my $query = "DELETE FROM biblioimages WHERE imagenumber = ?;"; |
125 |
my $sth = $dbh->prepare($query); |
153 |
my $sth = $dbh->prepare($query); |
126 |
$sth->execute($imagenumber); |
154 |
$sth->execute($imagenumber); |
127 |
my $dberror = $sth->errstr; |
155 |
my $dberror = $sth->errstr; |
128 |
warn "Database error!" if $sth->errstr; |
156 |
warn "Database error!" if $sth->errstr; |
Lines 130-153
sub DelImage {
Link Here
|
130 |
} |
158 |
} |
131 |
|
159 |
|
132 |
sub _scale_image { |
160 |
sub _scale_image { |
133 |
my ($image, $maxwidth, $maxheight) = @_; |
161 |
my ( $image, $maxwidth, $maxheight ) = @_; |
134 |
my ($width, $height) = $image->getBounds(); |
162 |
my ( $width, $height ) = $image->getBounds(); |
135 |
$debug and warn "image is $width pix X $height pix."; |
163 |
$debug and warn "image is $width pix X $height pix."; |
136 |
if ($width > $maxwidth || $height > $maxheight) { |
164 |
if ( $width > $maxwidth || $height > $maxheight ) { |
|
|
165 |
|
137 |
# $debug and warn "$filename exceeds the maximum pixel dimensions of $maxwidth X $maxheight. Resizing..."; |
166 |
# $debug and warn "$filename exceeds the maximum pixel dimensions of $maxwidth X $maxheight. Resizing..."; |
138 |
my $percent_reduce; # Percent we will reduce the image dimensions by... |
167 |
my $percent_reduce; # Percent we will reduce the image dimensions by... |
139 |
if ($width > $maxwidth) { |
168 |
if ( $width > $maxwidth ) { |
140 |
$percent_reduce = sprintf("%.5f",($maxwidth/$width)); # If the width is oversize, scale based on width overage... |
169 |
$percent_reduce = |
141 |
} else { |
170 |
sprintf( "%.5f", ( $maxwidth / $width ) ) |
142 |
$percent_reduce = sprintf("%.5f",($maxheight/$height)); # otherwise scale based on height overage. |
171 |
; # If the width is oversize, scale based on width overage... |
143 |
} |
172 |
} |
144 |
my $width_reduce = sprintf("%.0f", ($width * $percent_reduce)); |
173 |
else { |
145 |
my $height_reduce = sprintf("%.0f", ($height * $percent_reduce)); |
174 |
$percent_reduce = |
146 |
$debug and warn "Reducing image by " . ($percent_reduce * 100) . "\% or to $width_reduce pix X $height_reduce pix"; |
175 |
sprintf( "%.5f", ( $maxheight / $height ) ) |
147 |
my $newimage = GD::Image->new($width_reduce, $height_reduce, 1); #'1' creates true color image... |
176 |
; # otherwise scale based on height overage. |
148 |
$newimage->copyResampled($image,0,0,0,0,$width_reduce,$height_reduce,$width,$height); |
177 |
} |
|
|
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 ); |
149 |
return $newimage; |
188 |
return $newimage; |
150 |
} else { |
189 |
} |
|
|
190 |
else { |
151 |
return $image; |
191 |
return $image; |
152 |
} |
192 |
} |
153 |
} |
193 |
} |