|
Lines 24-29
use 5.010;
Link Here
|
| 24 |
|
24 |
|
| 25 |
use C4::Context; |
25 |
use C4::Context; |
| 26 |
use GD; |
26 |
use GD; |
|
|
27 |
use Koha::Upload; |
| 28 |
|
| 29 |
use File::Path qw(make_path); |
| 27 |
|
30 |
|
| 28 |
use vars qw($debug $noimage @ISA @EXPORT); |
31 |
use vars qw($debug $noimage @ISA @EXPORT); |
| 29 |
|
32 |
|
|
Lines 54-60
Stores binary image data and thumbnail in database, optionally replacing existin
Link Here
|
| 54 |
=cut |
57 |
=cut |
| 55 |
|
58 |
|
| 56 |
sub PutImage { |
59 |
sub PutImage { |
| 57 |
my ( $biblionumber, $srcimage, $replace ) = @_; |
60 |
my ( $biblionumber, $srcimage, $replace, $uploadedfileid ) = @_; |
| 58 |
|
61 |
|
| 59 |
return -1 unless defined($srcimage); |
62 |
return -1 unless defined($srcimage); |
| 60 |
|
63 |
|
|
Lines 66-90
sub PutImage {
Link Here
|
| 66 |
|
69 |
|
| 67 |
my $dbh = C4::Context->dbh; |
70 |
my $dbh = C4::Context->dbh; |
| 68 |
my $query = |
71 |
my $query = |
| 69 |
"INSERT INTO biblioimages (biblionumber, mimetype, imagefile, thumbnail) VALUES (?,?,?,?);"; |
72 |
"INSERT INTO biblioimages (biblionumber, mimetype, imagefile, thumbnail, uploadedfileid) VALUES (?,?,?,?,?);"; |
| 70 |
my $sth = $dbh->prepare($query); |
73 |
my $sth = $dbh->prepare($query); |
| 71 |
|
74 |
|
| 72 |
my $mimetype = 'image/png' |
75 |
my $mimetype = 'image/png' |
| 73 |
; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless... |
76 |
; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will convert all to PNG which is lossless... |
|
|
77 |
if( defined $uploadedfileid){ |
| 78 |
$sth->execute( $biblionumber, $mimetype, undef, undef, $uploadedfileid ); |
| 79 |
}else{ |
| 80 |
# Check the pixel size of the image we are about to import... |
| 81 |
my $thumbnail = _scale_image( $srcimage, 140, 200 ) |
| 82 |
; # MAX pixel dims are 140 X 200 for thumbnail... |
| 83 |
my $fullsize = _scale_image( $srcimage, 600, 800 ) |
| 84 |
; # MAX pixel dims are 600 X 800 for full-size image... |
| 85 |
$debug and warn "thumbnail is " . length($thumbnail) . " bytes."; |
| 86 |
|
| 87 |
$sth->execute( $biblionumber, $mimetype, $fullsize->png(), |
| 88 |
$thumbnail->png(), $uploadedfileid ); |
| 89 |
undef $thumbnail; |
| 90 |
undef $fullsize; |
| 91 |
} |
| 74 |
|
92 |
|
| 75 |
# Check the pixel size of the image we are about to import... |
|
|
| 76 |
my $thumbnail = _scale_image( $srcimage, 140, 200 ) |
| 77 |
; # MAX pixel dims are 140 X 200 for thumbnail... |
| 78 |
my $fullsize = _scale_image( $srcimage, 600, 800 ) |
| 79 |
; # MAX pixel dims are 600 X 800 for full-size image... |
| 80 |
$debug and warn "thumbnail is " . length($thumbnail) . " bytes."; |
| 81 |
|
| 82 |
$sth->execute( $biblionumber, $mimetype, $fullsize->png(), |
| 83 |
$thumbnail->png() ); |
| 84 |
my $dberror = $sth->errstr; |
93 |
my $dberror = $sth->errstr; |
| 85 |
warn "Error returned inserting $biblionumber.$mimetype." if $sth->errstr; |
94 |
warn "Error returned inserting $biblionumber.$mimetype." if $sth->errstr; |
| 86 |
undef $thumbnail; |
|
|
| 87 |
undef $fullsize; |
| 88 |
return $dberror; |
95 |
return $dberror; |
| 89 |
} |
96 |
} |
| 90 |
|
97 |
|
|
Lines 100-119
sub RetrieveImage {
Link Here
|
| 100 |
|
107 |
|
| 101 |
my $dbh = C4::Context->dbh; |
108 |
my $dbh = C4::Context->dbh; |
| 102 |
my $query = |
109 |
my $query = |
| 103 |
'SELECT imagenumber, mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?'; |
110 |
'SELECT imagenumber, mimetype, imagefile, thumbnail, uploadedfileid FROM biblioimages WHERE imagenumber = ?'; |
| 104 |
my $sth = $dbh->prepare($query); |
111 |
my $sth = $dbh->prepare($query); |
| 105 |
$sth->execute($imagenumber); |
112 |
$sth->execute($imagenumber); |
| 106 |
my $imagedata = $sth->fetchrow_hashref; |
113 |
my $imagedata = $sth->fetchrow_hashref; |
| 107 |
if ( !$imagedata ) { |
114 |
if ( !$imagedata ) { |
| 108 |
$imagedata->{'thumbnail'} = $noimage; |
115 |
$imagedata->{'thumbnail'} = $noimage; |
| 109 |
$imagedata->{'imagefile'} = $noimage; |
116 |
$imagedata->{'imagefile'} = $noimage; |
|
|
117 |
}else{ |
| 118 |
if (defined $imagedata->{'uploadedfileid'}){ |
| 119 |
my ($thumbnail, $imagefile) = _retrieve_thumbs_images($imagedata->{'uploadedfileid'}); |
| 120 |
$imagedata->{'thumbnail'} = $thumbnail; # MAX pixel dims are 140 X 200 for thumbnail... |
| 121 |
$imagedata->{'imagefile'} = $imagefile; |
| 122 |
|
| 123 |
} |
| 110 |
} |
124 |
} |
|
|
125 |
|
| 111 |
if ( $sth->err ) { |
126 |
if ( $sth->err ) { |
| 112 |
warn "Database error!" if $debug; |
127 |
warn "Database error!" if $debug; |
| 113 |
} |
128 |
} |
| 114 |
return $imagedata; |
129 |
return $imagedata; |
| 115 |
} |
130 |
} |
| 116 |
|
131 |
|
|
|
132 |
sub _retrieve_thumbs_images { |
| 133 |
my ($uploadedfileid) = @_; |
| 134 |
my $thumbnail_name = '140_200.png'; |
| 135 |
my $imagefile_name = '600_800.png'; |
| 136 |
my $temp_directory = "/tmp/koha_thumbs_$uploadedfileid"; |
| 137 |
my $files_exists = 0; |
| 138 |
#Check if the uploaded file id exists on the temp directory. |
| 139 |
#Check if a temp dir exists. |
| 140 |
if ( -d $temp_directory){ |
| 141 |
if ( -e "$temp_directory/$thumbnail_name" && -e "$temp_directory/$imagefile_name"){ |
| 142 |
$files_exists = 1; |
| 143 |
} |
| 144 |
}else{ |
| 145 |
make_path("/tmp/koha_thumbs_$uploadedfileid"); |
| 146 |
} |
| 147 |
my $thumbnail; |
| 148 |
my $imagefile; |
| 149 |
if($files_exists){ |
| 150 |
$thumbnail = GD::Image->new("$temp_directory/$thumbnail_name")->png(); |
| 151 |
$imagefile = GD::Image->new("$temp_directory/$imagefile_name")->png(); |
| 152 |
}else{ |
| 153 |
# There is none temp files, create them. |
| 154 |
my $upload = Koha::Upload->new->get({ id => $uploadedfileid, filehandle => 1 }); |
| 155 |
my $srcimage = GD::Image->new($upload->{fh}); |
| 156 |
$thumbnail = _scale_image( $srcimage, 140, 200 )->png(); |
| 157 |
$imagefile = _scale_image( $srcimage, 600, 800 )->png(); |
| 158 |
warn "$temp_directory/$thumbnail_name"; |
| 159 |
open( IMAGE, ">$temp_directory/$thumbnail_name"); |
| 160 |
binmode( IMAGE ); |
| 161 |
print IMAGE $thumbnail; |
| 162 |
close IMAGE; |
| 163 |
warn "$temp_directory/$imagefile_name"; |
| 164 |
open( IMAGE2, ">$temp_directory/$imagefile_name"); |
| 165 |
binmode( IMAGE2 ); |
| 166 |
print IMAGE2 $imagefile; |
| 167 |
close IMAGE2; |
| 168 |
} |
| 169 |
return ($thumbnail, $imagefile); |
| 170 |
} |
| 171 |
|
| 117 |
=head2 ListImagesForBiblio |
172 |
=head2 ListImagesForBiblio |
| 118 |
my (@images) = ListImagesForBiblio($biblionumber); |
173 |
my (@images) = ListImagesForBiblio($biblionumber); |
| 119 |
|
174 |
|