G'day Bod,

GD::Tiler

I can confirm that the tile() method returns PNG. I checked that the first eight characters were "\x{89}PNG\r\n\x{1a}\n"; as per "PNG File Format - Header".

I suspect the original intent was to output raw image data, as you're seeing, not a GD::Image object. If you look down to the Format description, you'll see "Output image format; default is 'PNG'; valid values are 'GIF', 'PNG', 'JPEG'; case insensitive" [my emphasis]. This suggests that this is not a coding problem; just a documentation issue: s/Returns a GD::Image object/Returns raw image data/.

Another thing about the tile() method is that, if you use "Images => \@images", all of the elements in @images will be converted to GD::Image objects. Avoid this with "Images => [@images]". They got it right in the SYNOPSIS but not, in my opinion, under METHODS.

You can raise a bug report but don't hold your breath for any changes to be implemented. This module was last updated 16 years ago (2006): I'd guess it's abandonware (could be wrong).

PDF::API2

I can also confirm what you're seeing. I used Carp::Always and got stack traces like this:

Not a HASH reference at /home/ken/perl5/perlbrew/perls/perl-5.36.0/lib +/site_perl/5.36.0/PDF/API2.pm line 2359. PDF::API2::image_gd(GD::Image=SCALAR(0x800ec1020)) called at / +home/ken/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0/PDF/AP +I2.pm line 2240 PDF::API2::image(PDF::API2=HASH(0x8008589c0), GD::Image=SCALAR +(0x800ec1020)) called at ./test.pl line 32

I concur with ++AM's assessment that this is a bug in the code. At line 2241:

return image_gd($file, %options);

should be

return $self->image_gd($file, %options);

Note that $self is a blessed hashref (PDF::API2=HASH(0x8008589c0)) while $file is a blessed scalarref (GD::Image=SCALAR(0x800ec1020)).

In image_gd(), because the real $self isn't being passed, it's reading $file (a scalarref) as $self (should be a hashref): the "Not a HASH reference" error is due to "GD::Image=SCALAR(0x800ec1020)->{'pdf'}" at line 2360.

I recommend that you do raise a bug report for this. PDF::API2 is updated fairly frequently (last was just a fortnight ago) and I'd imagine there's a good chance this bug will be fixed.

By the way, I've been referring to the "PDF::API2 source code" for version 2.044. I have version 2.043 installed which probably explains why the line numbers in my messages are out by one; it looks like you have the same issue too.

Solution

Given that GD::Tiler::tile() is probably correctly returning image data, and that PDF::API2::image() has (at least currently) a problem with GD::Image objects, just avoid the GD::Image objects altogether. This code works for me:

#!/usr/bin/env perl use strict; use warnings; use autodie; use PDF::API2; use GD::Tiler 'tile'; my $pdf_file = 'tiled_dogs.pdf'; my $png_file = 'tiled_dogs.png'; my @dog_jpegs = qw{husky.jpg labrador.jpg retriever.jpg}; my $tiled_dogs = tile( Images => [@dog_jpegs], Center => 1, ImagesPerRow => 3, ); my $pdf = PDF::API2::->new($pdf_file); my $page = $pdf->page(); open my $png_fh, '>:raw', $png_file; $png_fh->print($tiled_dogs); close $png_fh; my $canine_triptych = $pdf->image($png_file); unlink $png_file; $page->object($canine_triptych, 100, 650, 400); $pdf->save($pdf_file);

— Ken


In reply to Re: Image modules not returning or accepting GD::Image by kcott
in thread Image modules not returning or accepting GD::Image by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.