Subject: | Include images generated by Perl, (and not only from file) |
Ref to 2.25.
I have some images generated by GD I need to insert into a
spreadsheet, and would love to include those without saving to file,
and then import.
I have included code to modify the interface of $ws->insert_image
(...), to allow $file to be a ref to the actual content.
In practice, this can be done by
1) modifying &insert_image() to accept a ref as filename,
croak "Couldn't locate $image: $!" unless ref $image || -e $image;
2) modifying &_process_images() to accept a ref as filename
if (ref $filename) {
$data = $$filename;
} else {
# Open the image file and import the data.
I have also added a fifth sheet "InlineImage" in the images.pl
example, demonstrating inclusion of a GD image.
The pod should be extended with
The $filename may be an actual filename, or ref to the content itself,
e.g. an image generated by GD, or Image::Magick.
my $image = make_gd_image():
$worksheet3->insert_image('A1', \$image);
Subject: | images.pl |
#!/usr/bin/perl -w
#######################################################################
#
# Example of how to insert images into an Excel worksheet using the
# Spreadsheet::WriteExcel insert_image() method.
#
# reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org
#
use lib 'C:\Development\Erm3x\Develop\DNVModule';
use strict;
use Spreadsheet::WriteExcel;
# Create a new workbook called simple.xls and add a worksheet
my $workbook = Spreadsheet::WriteExcel->new("images.xls");
my $worksheet1 = $workbook->add_worksheet('Image 1');
my $worksheet2 = $workbook->add_worksheet('Image 2');
my $worksheet3 = $workbook->add_worksheet('Image 3');
my $worksheet4 = $workbook->add_worksheet('Image 4');
# Insert a basic image
$worksheet1->write('A10', "Image inserted into worksheet.");
$worksheet1->insert_image('A1', 'republic.png');
# Insert an image with an offset
$worksheet2->write('A10', "Image inserted with an offset.");
$worksheet2->insert_image('A1', 'republic.png', 32, 10);
# Insert a scaled image
$worksheet3->write('A10', "Image scaled: width x 2, height x 0.8.");
$worksheet3->insert_image('A1', 'republic.png', 0, 0, 2, 0.8);
# Insert an image over varied column and row sizes
# This does not require any additional work
# Set the cols and row sizes
# NOTE: you must do this before you call insert_image()
$worksheet4->set_column('A:A', 5);
$worksheet4->set_column('B:B', undef, undef, 1); # Hidden
$worksheet4->set_column('C:D', 10);
$worksheet4->set_row(0, 30);
$worksheet4->set_row(3, 5);
$worksheet4->write('A10', "Image inserted over scaled rows and columns.");
$worksheet4->insert_image('A1', 'republic.png');
my $image = make_inline_image();
if ($image) {
my $worksheet5 = $workbook->add_worksheet('InlineImage');
$worksheet5->insert_image('A1', \$image);
}
#from http://search.cpan.org/~lds/GD-2.44/GD.pm
sub make_inline_image {
eval "require GD;";
if ($@) {
return 0;
} else {
# create a new image
my $im = new GD::Image(100,100);
# allocate some colors
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
my $red = $im->colorAllocate(255,0,0);
my $blue = $im->colorAllocate(0,0,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
# Put a black frame around the picture
$im->rectangle(0,0,99,99,$black);
# Draw a blue oval
$im->arc(50,50,95,75,0,360,$blue);
# And fill it with red
$im->fill(50,50,$red);
# return the png image
return $im->png;
}
}
Subject: | Worksheet.pm |
Message body is not shown because it is too large.
Subject: | Workbook.pm |
Message body is not shown because it is too large.