Subject: | Read image from memory |
Thanks for a fine and very useful module, again!
When generating images from e.g. GD that I want to insert into a
spreadsheet, saving to file and then import do make some overhead. I
added a new feature that 'filenames' sent as ref to scalar actually
contains the data itself.
In general, two changes, (line numbers are ref to version 2.37)
Worksheet.pm, row 4906, allow $image to be a ref to SCALAR
sub insert_image
.....
croak "Couldn't locate $image: $!" unless -e $image ||
ref $image eq 'SCALAR';
Workbook.pm, row 1580, test if '$filename' is a ref to 'SCALAR'
sub _process_images {
....
my $data;
if (ref $filename eq 'SCALAR') {
$data = $$filename;
} else {
my $fh = FileHandle->new($filename);
....
$fh->close;
}
Test.pl
-----------------
use strict;
use GD;
use Spreadsheet::WriteExcel;
my $im = GD::Image->new(15,15 );
my $im_bgcolor = $im->colorAllocate(map {hex($_) }
unpack 'a2a2a2', 'CCCCCC');
my $im_color = $im->colorAllocate(map {hex($_) }
unpack 'a2a2a2', '00CCFF');
$im->filledRectangle(3,3,12,12,$im_color);
my $im_png = $im->png();
my $workbook = Spreadsheet::WriteExcel->new('simple.xls');
my $worksheet = $workbook->add_worksheet();
$worksheet->write(0, 0, 'Hi Excel!');
$worksheet->insert_image(1, 1, \$im_png);
Documentation of insert_image
-----------------------------
insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)
....
if $filename is ref to SCALAR, then the parmeter is considered to
contain the image itself. If the same image is used multiple times,
make sure to use the same reference, as same image used multiple times
are stored only once.
(sorry for using manual diff....)