in reply to Re^2: Image modules not returning or accepting GD::Image
in thread Image modules not returning or accepting GD::Image
"I was thinking that writing a file out to disc and then reading it back in was a messy workaround ..."
I agree and had the same thought myself. There is a way to do this with in-memory filehandles; however, there were two reasons why I didn't pursue this.
By the way, the "unlink $png_file;" was intended as optional for you and as housekeeping for me; I omitted to specifically state that.
As well as a filename, and a GD::Image object when that bug gets fixed, PDF::API2::image() can also take a filehandle. The following code produces exactly the same PDF as my previous code.
#!/usr/bin/env perl use strict; use warnings; use autodie; use PDF::API2; use GD::Tiler 'tile'; my $pdf_file = 'tiled_dogs.pdf'; 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(); my $page = $pdf->page(); open my $png_fh, '<:raw', \$tiled_dogs; my $canine_triptych = $pdf->image($png_fh); close $png_fh; $page->object($canine_triptych, 100, 650, 400); $pdf->save($pdf_file);
Assuming you don't want to keep the generated image, and you're not confronted with technical difficulties, this would be a better solution. I would expect this would also be much faster; but do Benchmark if speed is important to you.
Update: Two minor tweaks to the code: removed parts left over from the previous script that were no longer needed here.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Image modules not returning or accepting GD::Image
by Bod (Parson) on Dec 05, 2022 at 23:00 UTC |