9 use autouse 'Data::Dumper' => qw(Dumper);
21 my ($template, $loggedinuser, $cookie) = get_template_and_user({
22 template_name => "patroncards/image-manage.tt",
26 flagsrequired => {tools => 'batch_upload_patron_images'}, # FIXME: establish flag for patron card creator
30 my $file_name = $cgi->param('uploadfile') || '';
31 my $image_name = $cgi->param('image_name') || $file_name;
32 my $upload_file = $cgi->upload('uploadfile') || '';
33 my $op = $cgi->param('op') || 'none';
34 my @image_ids = $cgi->multi_param('image_id') if $cgi->param('image_id');
36 my $source_file = "$file_name"; # otherwise we end up with what amounts to a pointer to a filehandle rather than a user-friendly filename
38 my $display_columns = { image => [ #{db column => {label => 'col label', is link? }},
39 {image_id => {label => 'ID', link_field => 0}},
40 {image_name => {label => 'Name', link_field => 0}},
41 {_delete => {label => 'Delete', link_field => 0}},
42 {select => {label => 'Select', value => 'image_id'}},
45 my $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name"));
47 my $image_limit = C4::Context->preference('ImageLimit') || '';
48 my $errstr = ''; # NOTE: For error codes see error-messages.inc
50 if ($op eq 'upload') {
51 # Checking for duplicate image name
53 my $dbh = C4::Context->dbh;
54 my $query = "SELECT COUNT(*) FROM creator_images WHERE image_name=?";
55 my $sth = $dbh->prepare($query);
56 $sth->execute($image_name);
57 my $count = $sth->fetchrow_arrayref;
58 if ( $count->[0] > 0 ) {
60 warn sprintf('Image name already exists.');
63 IMPORT_SUCCESSFUL => 0,
64 SOURCE_FILE => $source_file,
65 IMAGE_NAME => $image_name,
72 warn sprintf('An error occurred while attempting to upload file %s.', $source_file);
75 IMPORT_SUCCESSFUL => 0,
76 SOURCE_FILE => $source_file,
77 IMAGE_NAME => $image_name,
83 my $image = Graphics::Magick->new;
84 eval{$image->Read($cgi->tmpFileName($file_name));};
86 warn sprintf('An error occurred while creating the image object: %s',$@);
89 IMPORT_SUCCESSFUL => 0,
90 SOURCE_FILE => $source_file,
91 IMAGE_NAME => $image_name,
98 my $size = $image->Get('filesize');
99 $errstr = 302 if $size > 500000;
100 $image->Set(magick => 'png'); # convert all images to png as this is a lossless format which is important for resizing operations later on
101 my $err = put_image($image_name, $image->ImageToBlob()) || '0';
102 $errstr = 101 if $err == 1;
103 $errstr = 303 if $err == 202;
106 IMPORT_SUCCESSFUL => 0,
107 SOURCE_FILE => $source_file,
108 IMAGE_NAME => $image_name,
111 image_limit => $image_limit,
115 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing save operation
117 IMPORT_SUCCESSFUL => 1,
118 SOURCE_FILE => $source_file,
119 IMAGE_NAME => $image_name,
127 elsif ($op eq 'delete') {
131 $err = rm_image(\@image_ids);
132 $errstr = 102 if $err;
135 warn sprintf('No image ids passed in to delete.');
140 DELETE_SUCCESSFULL => 0,
141 IMAGE_IDS => join(', ', @image_ids),
144 image_ids => join(',',@image_ids),
148 $table = html_table($display_columns->{'image'}, get_image(undef, "image_id, image_name")); # refresh table data after successfully performing delete operation
150 DELETE_SUCCESSFULL => 1,
155 elsif ($op eq 'none') {
157 IMPORT_SUCCESSFUL => 0,
158 SOURCE_FILE => $source_file,
159 IMAGE_NAME => $image_name,
163 else { # to trap unsupported operations
164 warn sprintf('Image upload interface called an unsupported operation: %s',$op);
167 IMPORT_SUCCESSFUL => 0,
168 SOURCE_FILE => $source_file,
169 IMAGE_NAME => $image_name,
175 output_html_with_http_headers $cgi, $cookie, $template->output;
181 image-upload.pl - Script for handling uploading of single images and importing them into the database.
189 This script is called and presents the user with an interface allowing him/her to upload a single image file. Files greater than 500K will be refused.
193 Chris Nighswonger <cnighswonger AT foundations DOT edu>
197 Copyright 2009 Foundations Bible College.
201 This file is part of Koha.
203 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
204 Foundation; either version 2 of the License, or (at your option) any later version.
206 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
207 Fifth Floor, Boston, MA 02110-1301 USA.
209 =head1 DISCLAIMER OF WARRANTY
211 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
212 A PARTICULAR PURPOSE. See the GNU General Public License for more details.