Bug 13954 - Patroncards: images show inadequate rough pixelation
authorMarc Véron <veron@veron.ch>
Mon, 6 Apr 2015 12:06:46 +0000 (14:06 +0200)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Wed, 8 Apr 2015 13:44:50 +0000 (10:44 -0300)
This patch provides a much better quality of images on patron cards.
It inserts images at a higher resolution into the PDF file and then scales it down internally in PDF.

Additionaly, the patch removes the follwing warning:
"my" variable $template masks earlier declaration in same scope at /usr/share/kohaclone/patroncards/create-pdf.pl line 66

To test:

- Create patron cards, save them as PDF and display them with a PDF viewer, e.g. at 400%
- Without patch, the images are very pixelated.
- Apply patch
- Verify that in the PDF the images now display with a much better quality.

Signed-off-by: Chris Nighswonger <cnighswonger@foundations.edu>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
C4/Patroncards/Patroncard.pm
patroncards/create-pdf.pl

index f130a04..4f3c3e0 100644 (file)
@@ -194,6 +194,7 @@ sub draw_image {
         my $intName = $pdf->AltJpeg($images->{$image}->{'data'},$images->{$image}->{'Sx'}, $images->{$image}->{'Sy'}, 1, $images->{$image}->{'alt'}->{'data'},$images->{$image}->{'alt'}->{'Sx'}, $images->{$image}->{'alt'}->{'Sy'}, 1);
         my $obj_stream = "q\n";
         $obj_stream .= "$images->{$image}->{'Sx'} $images->{$image}->{'Ox'} $images->{$image}->{'Oy'} $images->{$image}->{'Sy'} $Tx $Ty cm\n";       # see http://www.adobe.com/devnet/pdf/pdf_reference.html sec 8.3.3 of ISO 32000-1
+        $obj_stream .= "$images->{$image}->{'scale'} 0 0 $images->{$image}->{'scale'} 0 0 cm\n"; #scale to 20%
         $obj_stream .= "/$intName Do\n";
         $obj_stream .= "Q\n";
         $pdf->Add($obj_stream);
index d507722..37df606 100755 (executable)
@@ -63,7 +63,7 @@ print $cgi->header( -type       => 'application/pdf',
 
 my $pdf = C4::Creators::PDF->new(InitVars => 0);
 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
-my $template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
+my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
 
 $| = 1;
@@ -71,14 +71,14 @@ $| = 1;
 # set the paper size
 my $lower_left_x  = 0;
 my $lower_left_y  = 0;
-my $upper_right_x = $template->get_attr('page_width');
-my $upper_right_y = $template->get_attr('page_height');
+my $upper_right_x = $pc_template->get_attr('page_width');
+my $upper_right_y = $pc_template->get_attr('page_height');
 
 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
 
 my ($llx, $lly) = 0,0;
-(undef, undef, $llx, $lly) = $template->get_label_position($start_label);
+(undef, undef, $llx, $lly) = $pc_template->get_label_position($start_label);
 
 if (@label_ids) {
     my $batch_items = $batch->get_attr('items');
@@ -126,8 +126,8 @@ foreach my $item (@{$items}) {
                 borrower_number         => $borrower_number,
                 llx                     => $llx, # lower left corner of the card
                 lly                     => $lly,
-                height                  => $template->get_attr('label_height'), # of the card
-                width                   => $template->get_attr('label_width'),
+                height                  => $pc_template->get_attr('label_height'), # of the card
+                width                   => $pc_template->get_attr('label_width'),
                 layout                  => $layout_xml,
                 text_wrap_cols          => 30, #FIXME: hardcoded
         );
@@ -178,6 +178,11 @@ foreach my $item (@{$items}) {
         $alt_image->BlobToImage($binary_data);
         $alt_image->Set(magick => 'jpg', quality => 100);
 
+        #To avoid pixelation have the image 5 times bigger and
+        #scale it down in PDF itself
+        my $oversize_factor = 8;
+        my $pdf_scale_factor = 1 / $oversize_factor;
+
         my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
         my $alt_height = ceil($image->Get('height'));
         my $ratio = $alt_width / $alt_height;
@@ -185,17 +190,20 @@ foreach my $item (@{$items}) {
         my $display_width = ceil($ratio * $display_height);
 
 
-        $image->Resize(width => $display_width, height => $display_height);
+        $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
         $image->Set(magick => 'jpg', quality => 100);
 
+#       Write param for downsizing in pdf
+            $images->{$_}->{'scale'} = $pdf_scale_factor;
+
 #       Write params for alt image...
-            $images->{$_}->{'alt'}->{'Sx'} = $alt_width;
-            $images->{$_}->{'alt'}->{'Sy'} = $alt_height;
+            $images->{$_}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
+            $images->{$_}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
             $images->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
 
 #       Write params for display image...
-            $images->{$_}->{'Sx'} = $display_width;
-            $images->{$_}->{'Sy'} = $display_height;
+            $images->{$_}->{'Sx'} = $oversize_factor * $display_width;
+            $images->{$_}->{'Sy'} = $oversize_factor * $display_height;
             $images->{$_}->{'data'} = $image->ImageToBlob();
 
             my $err = $patron_card->draw_image($pdf);
@@ -203,7 +211,7 @@ foreach my $item (@{$items}) {
         }
         $patron_card->draw_text($pdf);
     }
-    ($llx, $lly, $new_page) = $template->get_next_label_pos();
+    ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
     $pdf->Page() if $new_page;
 }