From 32aa5281e852dedd6b01de66fb0cce736d230427 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 1 Apr 2012 22:36:18 -0500 Subject: [PATCH] bug 7865 command line utility to load pictures for borrowers based on card number. Signed-off-by: david --- misc/cronjobs/loadimages.pl | 231 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 231 insertions(+), 0 deletions(-) create mode 100755 misc/cronjobs/loadimages.pl diff --git a/misc/cronjobs/loadimages.pl b/misc/cronjobs/loadimages.pl new file mode 100755 index 0000000..6a349e0 --- /dev/null +++ b/misc/cronjobs/loadimages.pl @@ -0,0 +1,231 @@ +#!/usr/bin/perl +#From the Plano ISD Test server 1/18/2012 - David Schuster# +# This file is part of Koha. +# +# 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 +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# 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 +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation 51 Franklin Street, Suite 500 +# Boston, MA 02110-1335 USA + +use C4::Context; +use Data::Dumper; +use Date::Manip; +use strict; +use Getopt::Long; +use strict; +use warnings; +use Text::CSV; + +use File::Temp; +use File::Copy; +use CGI; +use GD; +use C4::Auth; +use C4::Output; +use C4::Members; +use C4::Debug; +use Pod::Usage; + +my $help = 0; + +my(%count,$input_file,$error_file,$log_file,$sepchar,$imagedirectory,$debug); +$imagedirectory = ""; + +GetOptions( + 'i|in=s' => \$input_file, + 'l|log=s' => \$log_file, + 'd|directory=s' => \$imagedirectory, + 's|sepchar:s' => \$sepchar, + 'debug' => \$debug, + 'help' => \$help, + ); + +=head1 NAME + +loadimages.pl - Loads borrower pictures from a directory into koha from the command line + +=head1 USAGE + +=over + +=item loadimages.pl [--verbose|--help] + +Load pictures and link them to the borrower record using a text file to connect cardnumber and picture. + +back + +head1 PARAMETERS + +=over + +=item B<--IDLINK|-i> + +Must have the file name in capitals and must be a flat text file "IDLINK.txt" + +=item B<--image.log|-l> + +output of information from command line job - number of items read, missing, loaded, and errors + +=item B<--data|-d> + +Where to find the datafile directory with the IDLINK file and pictures + +##example -- > perl loadimages.pl -i IDLINK.txt -l image.log -d "/home/pisd/dataload/pics/" +##default separator is comma + +if (not $sepchar){ + $sepchar =","; +} +my $csv = Text::CSV->new( +{ sep_char => $sepchar}); + + +open LOG,">$log_file" or die "Cannot open log file $log_file\n"; +open IN,"<$input_file" or die "Cannot open input file $input_file\n"; + +use Pod::Usage; + +my $dbh = C4::Context->dbh(); + +my $read_cnt=0; +my $line; +my $inserted = 0; +my $missingimage = 0; +my $inserterr = 0; +my $badmimetype = 0; +my $imagefilecorrupt = 0; + +my ($cardnumber,$imagefile); +my ($err_flag); + +while ( $line = ) +{ + chomp($line); + $line =~ s/\r//; + $read_cnt ++; + $err_flag = "OK"; + $debug and print "$read_cnt\n" unless $read_cnt % 100; + if ($csv->parse($line) ) + { + my @columns = $csv->fields(); + $cardnumber =$columns[0] ; + $imagefile = $columns[1] ; + $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters + $imagefile =~ s/[\"\r\n\s]//g; + + &handle_file($cardnumber, $imagefile); + + } + else + { + print (LOG "Unable to parse line $line\n"); + } +} + +print (LOG "$read_cnt records read\n"); +print (LOG "$inserted records inserted\n"); +print (LOG "$missingimage image records missing\n"); +print (LOG "$inserterr insert errors\n"); +print (LOG "$badmimetype bad mime type errors\n"); +print (LOG "$imagefilecorrupt corrupt image file records\n"); + +sub usage + { + pod2usage (-verbose=>2) + exit(); + } + +sub handle_file +{ + my ($cardnumber, $imagefile, $imgfile) = @_; + my $filename; +usage () if $help; +` + $imagefile = $imagedirectory . $imagefile; + + if(!stat($imagefile)) + { + print(LOG "Cannot find image file $imagefile\n"); + $missingimage++; + return(); + } + + my $size = (stat($imagefile))[7]; + if ($size > 100000) + { # This check is necessary even with image resizing to avoid possible security/performance issues... + print (LOG "Image $imagefile exceeds 100,000 bytes\n"); + return(); + } + + my ($srcimage, $image); + if (open (IMG, "$imagefile")) + { + $srcimage = GD::Image->new(*IMG); + close (IMG); + if (defined $srcimage) + { + my $mimetype = 'image/jpeg'; # GD autodetects three basic image formats: PNG, JPEG, XPM; we will conver + #t all to JPEG... + # Check the pixel size of the image we are about to import... + my ($width, $height) = $srcimage->getBounds(); + if ($width > 140 || $height > 200) + { # MAX pixel dims are 140 X 200... + my $percent_reduce; # Percent we will reduce the image dimensions by... + if ($width > 140) + { + $percent_reduce = sprintf("%.5f",(140/$width)); # If the width is oversize, scale based + # on width overage... + } + else + { + $percent_reduce = sprintf("%.5f",(200/$height)); # otherwise scale based on height over + } + my $width_reduce = sprintf("%.0f", ($width * $percent_reduce)); + my $height_reduce = sprintf("%.0f", ($height * $percent_reduce)); + $image = GD::Image->new($width_reduce, $height_reduce, 1); #'1' creates true color image... + $image->copyResampled($srcimage,0,0,0,0,$width_reduce,$height_reduce,$width,$height); + $imgfile = $image->jpeg(100); + undef $image; + undef $srcimage; # This object can get big... + } + else + { + $image = $srcimage; + $imgfile = $image->jpeg(); + undef $image; + undef $srcimage; # This object can get big... + } + + ##insert cardnumber and image into patronimage table + my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype; + if ( !$dberror && $mimetype ) + { + $inserted++; + } + elsif ( $dberror ) + { + print (LOG "Unable to insert $imagefile\n"); + $inserterr++; + } + elsif ( !$mimetype ) + { + print (LOG "Unable to determine mime type of $imagefile. Please verify mimetype."); + $badmimetype++; + } + else + { + print (LOG "Contents of $filename corrupted!"); + $imagefilecorrupt++; + } + } + } + return (); +} -- 1.7.2.5