BigJoe has asked for the wisdom of the Perl Monks concerning the following question:

I was reading HEX Embedded Images and decided to try some of it. I took this script (from BBQ)
open IMG, "foo.gif" or die "Couldn't open image: $!\n"; binmode(IMG); undef $/; $image = <IMG>; $hex = unpack("H*", $image); close IMG; while ($txt = substr($hex,0,32,'')) { print "'$txt'\n"; }
and made:
use strict; use DBI qw(:sql_types); my $dbh = DBI->connect("DBI:Oracle:host=172.16.21.16;sid=xxxx", 'xxxxx +x', 'xxxxxxxx') or die "Connecting : $DBI::errstr\n"; open IMG, "$ARGV[0]" or die "Couldn't open $ARGV[0]: $!\n"; binmode(IMG); undef $/; $image = <IMG>; $hex = unpack("H*", $image); close IMG; my $sth = $dbh->prepare("INSERT INTO pics (TITLE, HEX_PIC) VALUES (?, +?)")or die "$DBI::errstr\n"; $sth->bind_param( 1, 'pic1'); $sth->bind_param( 2, $hex, SQL_LONGVARCHAR); $sth->execute();
to upload a gif to hex into the database.

Then I looked at this code for outputing:
print "Content-type: image/gif\n\n"; print Image(); sub Image { my $image = '4749463839610f001000a2ff00848684' . 'dfe0dfc6c7c6c0c0c000000000000000' . '000000000021f90401000003002c0000' . '00000f00100040034048baacf3608049' . 'a98824ca0aae78d9b375a4164a92470e' . '016a8dde10721c48dfc029ee528b0ac0' . 'e06717121a8132cd6729b4ed8cc4c891' . '97a45a09b286569100003b00'; return(pack("H*",$image)); }
So I modified it to look like this:
use DBI; use strict; my $dbh = DBI->connect("DBI:Oracle:host=172.16.21.16;sid=xxxxxx", 'xxx +xx', 'xxxxx') or die "Connecting : $DBI::errstr\n"; $dbh->{LongReadLen} = 512 * 1024; my $sth = $dbh->prepare("Select TITLE, HEX_PIC from pics"); $sth->execute(); (my $name, my $image) = $sth->fetchrow_array; my $output = pack("H*", $image); print "Content-type: image/gif\n\n"; print $output; open(FH, ">outfile.gif"); binmode(FH); undef $/; print FH pack("H*",$image); close FH;
When I run this I get a broken link. But if I open the out file it looks correct. It is the same pic I inserted into the DB. I am sure it is probably something really easy that I am not seeing. I am running this with ActiveState's ActivePerl on a WinNT system. And I am trying to view it with IE 5.5 and Opera.

--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

Replies are listed 'Best First'.
Re: Embedded Hex Image problem
by chipmunk (Parson) on Dec 12, 2000 at 23:11 UTC
    Hmm... I don't know if this is the problem, but try using binmode() on STDOUT before you print the image. (I do notice that there is a 0a byte in your GIF.)
      Thanks Chipmunk it work.
      use DBI; use strict; my $dbh = DBI->connect("DBI:Oracle:host=xxx.xxx.xxx.xxx;sid=xxxxx", 'x +xxxx', 'xxxxx') or die "Connecting : $DBI::errstr\n"; $dbh->{LongReadLen} = 512 * 1024; my $sth = $dbh->prepare("Select TITLE, HEX_PIC from pics"); $sth->execute(); (my $name, my $image) = $sth->fetchrow_array; undef $/; print "Content-type: image/gif\n\n"; binmode(STDOUT); my $ImageGIF = pack("H*", $image); print $ImageGIF; $sth->finish; $dbh->disconnect;


      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.