- add samples/tk-photo.pl
authorTony Cook <tony@develop=help.com>
Mon, 14 Nov 2005 04:31:30 +0000 (04:31 +0000)
committerTony Cook <tony@develop=help.com>
Mon, 14 Nov 2005 04:31:30 +0000 (04:31 +0000)
Changes
samples/README
samples/tk-photo.pl [new file with mode: 0644]

diff --git a/Changes b/Changes
index 37f7f88..3de1134 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1176,6 +1176,7 @@ Revision history for Perl extension Imager.
   - document parameters
   - more examples
   - add AUTHOR/REVISION/SEE ALSO
+- add samples/tk-photo.pl
 
 =================================================================
 
index e71a210..ff8ce48 100644 (file)
@@ -71,3 +71,6 @@ slant_text.pl
   As part of this it demonstrates calculating the transformed bounding
   box of the text.
 
+tk-photo.pl
+
+  Simple example of making a Tk::Photo object using Imager data.
diff --git a/samples/tk-photo.pl b/samples/tk-photo.pl
new file mode 100644 (file)
index 0000000..4a3021f
--- /dev/null
@@ -0,0 +1,67 @@
+#!perl -w
+use strict;
+use Tk;
+use Tk::Photo;
+use MIME::Base64;
+use Tk::PNG;
+use Imager;
+
+my $image = Imager->new(xsize=>100, ysize=>100);
+
+# draw something simple here, you'll probably do something more complex
+$image->box(filled=>1, color=>'blue');
+$image->box(filled=>1, color=>'red', 
+           xmin=>20, ymin=>20, xmax=>79, ymax=>79);
+
+my $image_data;
+$image->write(data =>\$image_data, type=>'png')
+  or die "Cannot save image: ", $image->errstr;
+
+# supplying binary data didn't work, so we base64 encode it
+$image_data = encode_base64($image_data);
+
+my $main = MainWindow->new;
+my $tk_image = $main->Photo(-data => $image_data);
+$main->Label(-image=>$tk_image)->pack;
+MainLoop;
+
+=head1 NAME
+
+tk-photo.pl - display an Imager image under Tk
+
+=head1 SYNOPSIS
+
+  $ perl tk-photol.pl
+
+=head1 DESCRIPTION
+
+Simple code to make a Tk::Photo object from an Imager image.
+
+This works by:
+
+=over
+
+=item 1.
+
+write the image data to a scalar in PNG format
+
+=item 2.
+
+Base64 decode the data
+
+=item 3.
+
+read() it into the photo object, supplying the Base64 encoded data to
+the C<-data> parameter.
+
+=back
+
+=head1 REVISION
+
+$Revision$
+
+=head1 AUTHOR
+
+Tony Cook <tony@imager.perl.org>
+
+=cut