3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 ## NOTE: This is really broken at this point and needs updating... -chris_n
28 use autouse 'Data::Dumper' => qw(Dumper);
31 use autouse 'C4::Members' => qw(GetPatronImage);
32 use C4::Creators::PDF 1.000000;
33 use C4::Patroncards::Patroncard 1.000000;
36 # use_ok('C4::Patroncards::Patroncard');
39 my $borrower_number = 3;
40 my $pdf = C4::Creators::PDF->new(InitVars => 0);
42 # All units are in PostScript points in this script
43 $pdf->Mbox(0, 0, 612, 792);
45 # Convert layout to XML and insert into db
46 #my $query = "INSERT INTO creator_layouts (layout_id,layout_xml) VALUES (1,?) ON DUPLICATE KEY UPDATE layout_xml=?;";
47 #my $sth = C4::Context->dbh->prepare($query);
55 text => 1, # controls text_under_barcode display: 1 => visible, 0 => not visible
61 llx => 63, # llx & lly are relative to the llx & lly of the card NOT the page
65 'Anytown Public Library' => {
82 data_source => {'db' => 'patronimages',
83 'card_number' => '0002120108',
87 Ox => 0, # Ox,Oy should be set to 0 unless you want special effects see http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
89 Tx => 4.5, # Lower left corner of image relative to the lower left corner of the card
91 Dx => 72, # point height of display image
94 data_source => {'db' => 'pcards_images',
99 Ox => 0, # Ox,Oy should be set to 0 unless you want special effects see http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
101 Tx => 100, # Lower left corner of image
108 #$sth->execute(XMLout($layout),XMLout($layout));
110 # Retrieve XML from database
111 my$query = "SELECT layout_xml FROM creator_layouts WHERE layout_id=20;";
112 my $xml = C4::Context->dbh->selectrow_array($query);
114 # Convert back to hash
115 $layout = XMLin($xml);
117 # Do image foo and place binary image data into layout hash
119 my $alt_image_data = {};
121 foreach (keys %{$layout->{'images'}}) {
122 warn "Processing key: $_\n";
123 if (grep{m/db/} keys(%{$layout->{'images'}->{$_}->{'data_source'}})) {
124 warn $layout->{'images'}->{$_}->{'data_source'}->{'db'};
125 if ($layout->{'images'}->{$_}->{'data_source'}->{'db'} eq 'patronimages') {
126 warn "Processing patron image data.\n";
127 ($image_data, $error) = GetPatronImage($layout->{'images'}->{$_}->{'data_source'}->{'card_number'});
128 warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
129 next if !$image_data;
131 elsif ($layout->{'images'}->{$_}->{'data_source'}->{'db'} eq 'pcards_images') {
132 warn "Processing pcards image data for image_id " . $$layout{'images'}{$_}{'data_source'}{'image_id'} . ".\n";
133 my $dbh = C4::Context->dbh();
134 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
135 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM pcards_images WHERE image_id = \'$$layout{'images'}{$_}{'data_source'}{'image_id'}\'");
136 warn sprintf('Image does not exists in db table %s.', $$layout{'images'}{$_}{'data_source'}{'db'}) if !$image_data;
137 next if !$image_data;
138 # $string =~ s/(.)/sprintf("%x",ord($1))/eg;
141 warn sprintf('No retrieval method for table %s.', $$layout{'images'}{$_}{'data_source'}{'db'}) if !$image_data;
143 warn sprintf('Database returned the following error: %s.', $error) if $error;
145 elsif (grep{m/foo/} keys(%{$layout->{'images'}->{$_}->{'data_source'}})) {
146 # some other image storage/retrieval method
149 my $binary_data = $image_data->{'imagefile'};
150 my $image = Graphics::Magick->new;
151 $image->BlobToImage($binary_data);
152 my $alt_image = Graphics::Magick->new;
153 $alt_image->BlobToImage($binary_data);
154 $alt_image->Set(magick => 'jpg', quality => 100);
156 warn "Print Image Dimensions: " . $image->Get('width') . " X " . $image->Get('height') . "\n";
158 my $alt_width = $image->Get('width');
159 my $alt_height = $image->Get('height');
160 my $ratio = $alt_width / $alt_height;
161 my $display_height = $layout->{'images'}->{$_}->{'Dx'};
162 my $display_width = $ratio * $display_height;
163 $image->Resize(width => $display_width, height => $display_height);
164 $image->Set(magick => 'jpg', quality => 100);
166 warn "Display Image dimensions: " . $image->Get('width') . " X " . $image->Get('height') . "\n";
168 # Write params for alt image...
169 $layout->{'images'}->{$_}->{'alt'}->{'Sx'} = $alt_width;
170 $layout->{'images'}->{$_}->{'alt'}->{'Sy'} = $alt_height;
171 $layout->{'images'}->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
172 ## Write params for display image...
173 $layout->{'images'}->{$_}->{'Sx'} = $display_width;
174 $layout->{'images'}->{$_}->{'Sy'} = $display_height;
175 $layout->{'images'}->{$_}->{'data'} = $image->ImageToBlob();
178 # Create a new patroncard object
179 my $patron_card = C4::Patroncards::Patroncard->new(
181 borrower_number => $borrower_number,
182 llx => 36,#13.5 # lower left corner of the card
184 height => 139.5, # of the card
187 text_wrap_cols => 30,
189 $patron_card->draw_guide_box($pdf);
190 $patron_card->draw_barcode($pdf);
191 my $err = $patron_card->draw_image($pdf);
192 $patron_card->draw_text($pdf);