in reply to Image modules not returning or accepting GD::Image
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Image modules not returning or accepting GD::Image
by hippo (Archbishop) on Dec 02, 2022 at 11:02 UTC | |
by Bod (Parson) on Dec 03, 2022 at 00:52 UTC | |
by hippo (Archbishop) on Dec 03, 2022 at 19:01 UTC | |
by Bod (Parson) on Dec 05, 2022 at 22:50 UTC | |
|
Re^2: Image modules not returning or accepting GD::Image
by Bod (Parson) on Dec 03, 2022 at 00:40 UTC | |
by kcott (Archbishop) on Dec 03, 2022 at 02:06 UTC | |
by Bod (Parson) on Dec 05, 2022 at 23:00 UTC | |
by Anonymous Monk on Dec 03, 2022 at 05:25 UTC | |
by Bod (Parson) on Dec 05, 2022 at 22:55 UTC |